ReadComm( )

ReadComm( )

Postby TimStone » Thu Oct 21, 2010 8:12 pm

I'm trying to read a response on a Comm Port from a pin pad reader. I know the comm port is open and I can send to it. I can't find an example of ReadComm( ) but this code should apply correctly.

Code: Select all  Expand view  RUN

PRIVATE abc := SPACE(100)
...
...

nBytes := ReadComm( hPort, @abc )

MsgInfo( nBytes )     // Returns 10
MsgInfo( abc )         // Shows nothing
 


As defined, ReadComm should place that 10 character string into @abc and I should be able to read it.

Any thoughts on why I can't read those 10 bytes that were returned ?
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2944
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: ReadComm( )

Postby Otto » Fri Oct 22, 2010 6:38 am

Hello Tim, I use the same code as you. The only difference is that I declare
PRIVATE abc := SPACE(100)
as local.

Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6336
Joined: Fri Oct 07, 2005 7:07 pm

Re: ReadComm( )

Postby Robert Frank » Fri Oct 22, 2010 9:55 am

Space "chr(32)" isn't good . I prefer Chr(255) is beter to recognize space received from RS.

STATIC cSPEED:=":9600,N,8,1"
STATIC hPORT // handler portu
STATIC nPORT // numer portu

Function COM_OPEN(xPORT)
Local pNAME
Local nError,cDCB
Local xRESZTA:=.T.
Do Case
Case xPORT=1
pNAME:="COM1"
Case xPORT=2
pNAME:="COM2"
Case xPORT=3
pNAME:="COM3"
Case xPORT=4
pNAME:="COM4"
Case xPORT=5
pNAME:="COM5"
Case xPORT=6
pNAME:="COM6"
Case xPORT=7
pNAME:="COM7"
Case xPORT=8
pNAME:="COM8"
Case xPORT=9
pNAME:="COM9"
EndCase

hPORT:= OpenComm( pNAME, 2048, 2048 )
If ! BuildCommDcb( pNAME+cSPEED, @cDcb )
nError = GetCommError( hPORT )
MsgInfo( "BuildCommDcb Error: " + Str( nError ) )
xRESZTA:=.F.
EndIf
If ! SetCommState( hPORT, cDcb )
nError = GetCommError( hPORT )
MsgInfo( "SetCommState Error: " + Str( nError ) )
xRESZTA:=.F.
EndIf
If xRESZTA
oWND:oMSGBAR:cMSGDEF:=PNAME+" : OTWARTY"
oWND:oMSGBAR:Refresh(.T.)
Else
oWND:oMSGBAR:cMSGDEF:=PNAME+" : NIE UDANE OTWARCIE"
oWND:oMSGBAR:Refresh(.T.)
EndIf
Return xRESZTA

Function RF_REC_READ()
Local XXX,xBUFFER:=Replicate(Chr(255),1024)
//xRTIMER:DeActivate()
READCOMM(hCOMM,@xBUFFER)
xBUFFER:=StrTran(xBUFFER,Chr(255),'')
If Len(xBUFFER)>0
AADD(xTABREC,xBUFFER) // add to my table with received
TRACE_ZAPIS(sHANDLE,"-RR- "+xBUFFER+sCRLF)
EndIf
//xRTIMER:Activate()
Return


another example

Local xRECT
LOCAL cDcb1
LOCAL nError
cPORT1:="COM1"
pCOMM1:=OpenComm(cPORT1, 1024, 1024 )
xRECT := GetClientRect( oWND:hWnd )
//If ! BuildCommDcb( cPORT1+":9600,N,8,1", @cDcb1 )
If ! BuildCommDcb( cPORT1+":4800,N,8,2", @cDcb1 ) //SYSMEX
nError = GetCommError( pCOMM1 )
MsgInfo( "BuildCommDcb Com1 Error: " + Str( nError ) )
endif
If ! SetCommState( cDcb1 )
nError = GetCommError( pCOMM1 )
MsgInfo( "SetCommState Com1 Error: " + Str( nError ) )
EndIf
EnableCommNotification( nCOMM, oWND1TRX:hWnd, 1, -1 )
...
//
oWND1TRX:bCommNotify := {| nCOMM,nSTATUS| ODBIERZ1(nCOMM,nSTATUS)}
//
...
ACTIVATE WINDOW oWND1TRX ;
VALID(ZAMKNIJ1()) ;
ON INIT (EnableCommNotification( pCOMM1, oWND1TRX:hWnd, 1, -1 ))

If you want to cancel notofication use EnableCommNotification( nCOMM, oWND1TRX:hWnd, -1, -1 )
Robert Frank
User avatar
Robert Frank
 
Posts: 95
Joined: Fri Nov 23, 2007 4:43 am
Location: Gdynia-Poland

Re: ReadComm( )

Postby TimStone » Fri Oct 22, 2010 9:08 pm

Thanks everyone. I wanted to be sure the code was working even though I wasn't getting responses. I made two discoveries:

1) The device was slow to respond and I was trying to read the buffer before the device transmitted any data to it. A "port monitor" is helpful to see this unfold !

2) The device won't respond to other commands I was sending because it is the wrong one for the processing method they want me to use ! That clears up the mess.

So ... I can affirm that ReadComm is working correctly ... if only the device would do the same.
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2944
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: ReadComm( )

Postby Robert Frank » Tue Oct 26, 2010 10:52 pm

TimStone wrote:1) The device was slow to respond and I was trying to read the buffer before the device transmitted any data to it. A "port monitor" is helpful to see this unfold !

In my app I use 0.1sek and more (it depend how fast device can answer) beetween packets. Old equipment require up to 0.5 sek. If device have to do something special after "our" pocket, you have to wait few seconds sometimes.
Robert Frank
User avatar
Robert Frank
 
Posts: 95
Joined: Fri Nov 23, 2007 4:43 am
Location: Gdynia-Poland


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 72 guests