by 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