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?
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!
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):
In CFC you could do this with "MOVE" block with EN/ENO.
Thank you very much for this info, I will dig this track