TCP/ Client convert String DataReceived

laurentl38
2023-03-05
2023-03-07
  • laurentl38 - 2023-03-05

    hello,

    I am beginner with codesys and I have some problem to create a TCP client
    I am using hercule setup to simulate the server.
    when I send "good morning world" at first, I receive this string in DataRecieved.
    after if I send a shorter string "hello", I receive in DataRecieved "helloMorning World" with the end of my old message
    I tried to put a Len(DataRecieved) instead of the SizeOF. but it doesn't work.
    Do you have any Leads to solve my problem?

    Many thanks in advance!

     
  • TimvH

    TimvH - 2023-03-07

    The data which is received is written in the STRING (memory). A string needs a 0 terminating byte to see the end of the string. In your case hello is just written to the first 5 characters of the string. The rest is not changed, it doesn't write a 0 in the 6th byte of the string (as it shouldn't).

    So what you could do is write some additional code to check the size of the message your received and then in the next byte write 0 in the string.

    You could see the string as an array of bytes. So you could do something like (in this case):

    IF TCP_READ_0.xReady THEN
        DataReceived[TCP_READ_0.szCount] := 0;
    END_IF
    

    In CFC you could do this with "MOVE" block with EN/ENO.

     
  • laurentl38 - 2023-03-07

    Thank you very much for this info, I will dig this track

     

Log in to post a comment.