Send and receive data to serial port

HMG en Español

Moderator: Rathinagiri

Post Reply
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Send and receive data to serial port

Post by jairpinho »

Staff need to send data to serial port with data type decimal hexadecimal binary I'm using the lib hbcomm wonder if this lib sends the data as only want to send or sends data to type character if the character lib hbcomm only someone could send me help as hmg use to send data via serial port with binary data type decimal and hexadecimal through the serial port

thanks to all
Last edited by jairpinho on Mon Dec 12, 2011 11:19 am, edited 1 time in total.
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
mol
Posts: 3721
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Send data to serial port with data type decimal hexadeci

Post by mol »

Please try to write it again with some dots and commas :lol:
It's strong incomprehensible what are you looking for

I'f you are using translator (it looks like a google translator), please try to use shorter sentences.

Best regards, Marek
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: Send data to serial port with data type decimal hexadeci

Post by jairpinho »

mol wrote:Please try to write it again with some dots and commas :lol:
It's strong incomprehensible what are you looking for

I'f you are using translator (it looks like a google translator), please try to use shorter sentences.

Best regards, Marek

need to send data of type hexadecimal or binary serial port, I'm using the lib hbcomm but not working.
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
mol
Posts: 3721
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Send data to serial port with data type decimal hexadeci

Post by mol »

you can watch this 100% working code:

in attachment...
Attachments
MOL-DF.zip
(2.17 KiB) Downloaded 472 times
User avatar
mol
Posts: 3721
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Send data to serial port with data type decimal hexadeci

Post by mol »

here is the function sendind data
Data is passed as a parameter

Code: Select all


function SendDataViaComPort
	param cDataToSend
	local lRet := .t.
    oWinPort := win_com():Init(cPortName,CBR_9600, EVENPARITY , 7, ONESTOPBIT)
    if !oWinPort:Open
		lRet := .f.
        MsgBox("Error while opening COM Port" )
		return lRet
    else
        // "Open succeeded"
        oWinPort:RTSFlow(.F.)
        oWinPort:DTRFlow(.F.)
        oWinPort:XonXoffFlow(.t.)
        oWinPort:SetDTR(.t.)
        if !(oWinPort:Write(cDataToSend) == len(cDataToSend))
			lRet := .f.
			MsgBox("Unsuccessfully :-(")
		endif
	ENDIF
	//oWinPort:QueueStatus(@lCTSHold, @lDSRHold, @lDCDHold, @lXoffHold, @lXoffSent, @nInQueue, @nOutQueue)
	oWinPort:Close()
 return lRet

User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: Send data to serial port with data type decimal hexadeci

Post by jairpinho »

where I get the syntax of win_com ()
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
mol
Posts: 3721
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Send data to serial port with data type decimal hexadeci

Post by mol »

If I remember, Iwas reading sources of harbour ant I found win_com() there...
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: Send and receive data to serial port

Post by jairpinho »

"Mol" of the group or someone can help me how to receive data through the function wincom
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
mol
Posts: 3721
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Send and receive data to serial port

Post by mol »

I think, you can use something like that:

Code: Select all

	
	cPortName := "COM1"
	nSpeed := 9600
	
	oWinPort := win_com():Init(cPortName, nSpeed, NOPARITY, 8, ONESTOPBIT)
    if !oWinPort:Open
        msgstop("Error while opening COM Port" )
		return .f.
    else
        // Opened successfully
		// set queue size and timeouts
		oWinPort:QueueSize( 1000, 1000 )
		oWinPort:Purge()
		//TimeOuts( nReadInterval, nReadMultiplier, nReadConstant, nWriteMultiplier, nWriteConstant )
		oWinPort:Timeouts(1500,1000,20000,1000,20000)
		
		//set COM lines and flow control
        oWinPort:RTSFlow(.t.)
        oWinPort:DTRFlow(.t.)
        oWinPort:XonXoffFlow(.t.)
        oWinPort:SetDTR(.t.)
        oWinPort:SetDTR(.t.)
        oWinPort:SetRTS(.t.)
    endif
	
	
	// you can read answer from COM Port byte by byte
	
	cAnswer := ""
	cCharFromCOM := ""
	nBytesToRead := 1
	do while .t.
		oWinPort:Read(@cCharFromCOM, nBytesToRead)
		cAnswer += cCharFromCOM
		if len(cCharFromCOM) < nBytesToRead
			// end of data ...
			exit
		endif
	enddo
	
	Msginfo("Message from COM Port: "+ cAnswer)
	

User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: Send and receive data to serial port

Post by jairpinho »

The function is not presenting data

Code: Select all

*************************************************************
Function SendDataViaComPort2()

   cPortName := "COM2"
   nSpeed := 19200
   
   oWinPort := win_com():Init(cPortName, nSpeed, NOPARITY, 8, ONESTOPBIT)
    if !oWinPort:Open
        msgstop("Error while opening COM Port" )
      return .f.
    else
        // Opened successfully
      // set queue size and timeouts
      oWinPort:QueueSize( 1000, 1000 )
      oWinPort:Purge()
      //TimeOuts( nReadInterval, nReadMultiplier, nReadConstant, nWriteMultiplier, nWriteConstant )
      oWinPort:Timeouts(15,10,20,10,20)
      
      //set COM lines and flow control
        oWinPort:RTSFlow(.t.)
        oWinPort:DTRFlow(.t.)
        oWinPort:XonXoffFlow(.t.)
        oWinPort:SetDTR(.t.)
        oWinPort:SetDTR(.t.)
        oWinPort:SetRTS(.t.)
    endif
 
 cDataToSend  := CHR(1)+CHR(1)+CHR(0)+CHR(0)+CHR(0)+CHR(100)+CHR(61) + CHR(225)
 
	   IF !(oWinPort:Write(cDataToSend) == len(cDataToSend))
         lRet := .f.
         MsgBox("sem sucesso :-(")
       endif 
   
   // you can read answer from COM Port byte by byte
   
   cAnswer := ""
   cCharFromCOM := ""
   nBytesToRead := 1
   do while .t.
      oWinPort:Read(@cCharFromCOM, nBytesToRead)
      cAnswer += cCharFromCOM
      if len(cCharFromCOM) < nBytesToRead
         // end of data ...
         exit
      endif
   enddo
   
   Msginfo("Message from COM Port: "+ cAnswer)
   
   oWinPort:Close()
RETURN 
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
Post Reply