Hi Forum
We are having some strange problem with the nbs.tcp_read function.
Everything is connected, and we recieve data as expected.
But somehow the internal buffer of the read function is not deleted after the function copies the buffer to the output pointer.
First time we send it is working, but second time it is messing up the recieved data.
We can not se any function for resetting the internal buffer, anyone??
Below code snipped, very straighforward
The recieved string is just cutted at every line feed
// Connect to TCP server
fbTcpClient2(
xEnable:=TCPClient2_xConnect,
xDone=>TCPClient2_xDone,
xBusy=>TCPClient2_xBusy,
xError=>,
udiTimeOut:=1000000,
ipAddr:=gvlSetting.gc_stIpAddr,
uiPort:=gvlSetting.gc_uiPort2,
eError=>,
xActive=>TCPClient2_xActive,
hConnection=>);
/ Buffer telegram on recieving
IF fbTcpRead2.xReady THEN
TCPRead2_loop:=TRUE;
TCPRead2_MessageTemp:=TCPRead2_MessageRX;
WHILE TCPRead2_loop DO
// Look for carriage return / linefeed
EndStringMarkerPosition := FIND(TCPRead2_MessageTemp, '$R$N');
IF EndStringMarkerPosition <> 0 THEN
TCPRead2_arrMessageRx[TCPRead2_Index]:=LEFT(TCPRead2_MessageTemp,(EndStringMarkerPosition-4));
TCPRead2_MessageTemp:=DELETE(TCPRead2_MessageTemp,(EndStringMarkerPosition+5),0);
TCPRead2_Index:=TCPRead2_Index+1;
ELSE
TCPRead2_loop:=FALSE;
TCPRead2_MessageTemp:='';
TCPRead2_Index:=0;
END_IF
END_WHILE
END_IF
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Forum
We are having some strange problem with the nbs.tcp_read function.
Everything is connected, and we recieve data as expected.
But somehow the internal buffer of the read function is not deleted after the function copies the buffer to the output pointer.
First time we send it is working, but second time it is messing up the recieved data.
We can not se any function for resetting the internal buffer, anyone??
Below code snipped, very straighforward
The recieved string is just cutted at every line feed
// Connect to TCP server
fbTcpClient2(
xEnable:=TCPClient2_xConnect,
xDone=>TCPClient2_xDone,
xBusy=>TCPClient2_xBusy,
xError=>,
udiTimeOut:=1000000,
ipAddr:=gvlSetting.gc_stIpAddr,
uiPort:=gvlSetting.gc_uiPort2,
eError=>,
xActive=>TCPClient2_xActive,
hConnection=>);
// Read from TCP buffer
fbTcpRead2(
xEnable:=TCPClient2_xActive,
hConnection:=fbTcpClient2.hConnection,
szSize:=SIZEOF(TCPRead2_MessageRX),
pData:=ADR(TCPRead2_MessageRX),
szCount=>TCPRead2_Len);
/ Buffer telegram on recieving
IF fbTcpRead2.xReady THEN
TCPRead2_loop:=TRUE;
TCPRead2_MessageTemp:=TCPRead2_MessageRX;
WHILE TCPRead2_loop DO
// Look for carriage return / linefeed
EndStringMarkerPosition := FIND(TCPRead2_MessageTemp, '$R$N');
IF EndStringMarkerPosition <> 0 THEN
TCPRead2_arrMessageRx[TCPRead2_Index]:=LEFT(TCPRead2_MessageTemp,(EndStringMarkerPosition-4));
TCPRead2_MessageTemp:=DELETE(TCPRead2_MessageTemp,(EndStringMarkerPosition+5),0);
TCPRead2_Index:=TCPRead2_Index+1;
ELSE
TCPRead2_loop:=FALSE;
TCPRead2_MessageTemp:='';
TCPRead2_Index:=0;
END_IF
END_WHILE
END_IF
What about zeroing your buffer once you copied it (TCPRead2_MessageRX) ?
If TCPRead2_MessageRX is a STRING you have to end it with a null terminator.
Try:
IF fbTcpRead2.xReady THEN
//Add this line
TCPRead2_MessageRX[fbTcpRead2.szCount]:= 0;//End terminator
TCPRead2_loop:=TRUE;
TCPRead2_MessageTemp:=TCPRead2_MessageRX;
....