Page 1 of 1

Send and receive data to serial port

Posted: Mon Dec 05, 2011 3:04 pm
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

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

Posted: Mon Dec 05, 2011 4:05 pm
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

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

Posted: Mon Dec 05, 2011 4:12 pm
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.

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

Posted: Mon Dec 05, 2011 4:18 pm
by mol
you can watch this 100% working code:

in attachment...

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

Posted: Mon Dec 05, 2011 4:23 pm
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


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

Posted: Mon Dec 05, 2011 6:17 pm
by jairpinho
where I get the syntax of win_com ()

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

Posted: Mon Dec 05, 2011 7:14 pm
by mol
If I remember, Iwas reading sources of harbour ant I found win_com() there...

Re: Send and receive data to serial port

Posted: Mon Dec 12, 2011 11:22 am
by jairpinho
"Mol" of the group or someone can help me how to receive data through the function wincom

Re: Send and receive data to serial port

Posted: Mon Dec 12, 2011 12:30 pm
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)
	


Re: Send and receive data to serial port

Posted: Mon Dec 12, 2011 2:45 pm
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