TCP_read buffer problem

elmester
2021-08-05
2021-08-12
  • elmester - 2021-08-05

    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

     
  • dFx

    dFx - 2021-08-05

    What about zeroing your buffer once you copied it (TCPRead2_MessageRX) ?

     
  • Ton - 2021-08-12

    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;
    ....

     

Log in to post a comment.