Hello Everybody,
I'm trying to read serial data that is comming from an Arduino over RS-232.
The Arduino is supposed to send data in a format like this :
[PortNumber1];[Data1]; [PortNumber2];[Data2]; [PortNumber3];[Data3]; followed by a CR
In fact it's a comma delimited string that should be processed by my program.
My program isn't sending any data to the device, just reading.
So I wrote this code to initialize my com-port :
IF sOpen(gnComPort,128,128,2000,True) = True THEN
IF sParameter(gnComPort,9600,0,8,1,False,False,False) = False THEN
Error("Cannot initialize")
ELSE
sEvent(gnComPort,sEveCharReceived,Incoming2)
END
ELSE
Error("Cannot initialize")
END
This part of the code is working fine (it should), but my problem is in the processing of the data that I'm receiving.
The data has no fixed lenght, and also, sometimes the string isn't perfectly according to the format.
As a first step I want to be able to have the incomming data processed into separate lines for every piece of data that ends with a CR+LF. But I can't get it working. This is my actual code :
Procedure Incoming2(ComPort,EventNum)
nSize is int
bFlagOk is boolean
bFlagOne is boolean
nSize2 is int
sBuffer is string
nLengte is int
nWaarde is int
sKarakter is string
sTekstNieuw is string
sLijn is string
bFlagOk = False
bFlagOne = False
nSize = sInEntryQueue(ComPort)
IF nSize<>0 THEN
WHILE NOT bFlagOk
nSize = sInEntryQueue(ComPort)
IF nSize > nSize2 THEN
nSize2 = nSize
bFlagOne = False
ELSE
IF bFlagOne THEN
bFlagOk = True
ELSE
bFlagOne = True
END
END
END
sBuffer = sRead(ComPort,nSize)
END
IF bFlagOk THEN
nLengte = Length(sBuffer)
FOR nTeller = 1 TO nLengte
sKarakter = Middle(sBuffer,nTeller,1)
nWaarde = Asc(sKarakter)
IF nWaarde = 13 THEN
Trace("CR")
sLijn = Left(sBuffer,nTeller -2)
Trace(sLijn)
sLijn = ""
END
END
Wait(10,waitTimer)
END
this is what the device is send (log extracted from terraterm) :
P1;blala;P2;@0RM0120122;P3;@0RM0090105
P1;@0RM0121047;P2;@0RM0008321;P3;(
P1;@0RM0001047;P2;@0RM0000321;P3;@0RM0231521
P1;blala;P2;@0RM0120122;P3;@0RM0090105
P1;@0RM0121047;P2;@0RM0008321;P3;(
P1;@0RM0001047;P2;@0RM0000321;P3;@0RM0231521
P1;blala;P2;@0RM0120122;P3;@0RM0090105
P1;@0RM0121047;P2;@0RM0008321;P3;(
P1;@0RM0001047;P2;@0RM0000321;P3;@0RM0231521
P1;blala;P2;@0RM0120122;P3;@0RM0090105
P1;@0RM0121047;P2;@0RM0008321;P3;(
P1;@0RM0001047;P2;@0RM0000321;P3;@0RM0231521
P1;blala;P2;@0RM0120122;P3;@0RM0090105
P1;@0RM0121047;P2;@0RM0008321;P3;(
P1;@0RM0001047;P2;@0RM0000321;P3;@0RM0231521
P1;blala;P2;@0RM0120122;P3;@0RM0090105
P1;@0RM0121047;P2;@0RM0008321;P3;(
P1;@0RM0001047;P2;@0RM0000321;P3;@0RM0231521
P1;blala;P2;@0RM0120122;P3;@0RM0090105
P1;@0RM0121047;P2;@0RM0008321;P3;(
P1;@0RM0001047;P2;@0RM0000321;P3;@0RM0231521
And this is what my code creates :
1;@0RM0121047;P2;@0RM0008321;P3;(<\r><\n>P1;@0RM0001047;P2;@0RM0000321;P3;@0RM023152
CR
1;blala;P2;@0RM0120122;P3;@0RM009010
CR
047;P2;@0RM0008321;P3;
CR
M0000321;P3;@0RM023152
CR
;@0RM009010
CR
;@0RM0090105<\r><\n>P1;@0RM0121047;P2;@0RM0008321;P3;
CR
P1;@0RM0001047;P2;@0RM0000321;P3;@0RM023152
CR
P1;blala;P2;@0RM0120122;P3;@0RM009010
CR
;@0RM0121047;P2;@0RM0008321;P3;
CR
47;P2;@0RM0000321;P3;@0RM023152
CR
2;P3;@0RM009010
The "CR" is just tracecode to see when a CR is detected inside the buffer
Any suggestions on how to get this working properly ?
grtz
Patrick