02-01-2018 04:39 AM
The only way to clear the buffer of a TCP/IP stream is by reading it. You will have to provide some form of frame termination. One easy possibility is to add a carriage return/line feed to each command you send and on the receiving side use the CRLF read mode on TCP Read with a higher byte count to read than what you expect. That will make sure that you always read an entire frame up to the end and not just partial frames leaving some data in the buffer that is then returned in your next read.
02-01-2018 04:39 AM
Not sure the order is wrong. I thing the read is just falling behind on the writes. In other words, your writing faster then you're reading. Put a wait in the write loop.
Clearing the buffer is possible by a) reading all data or b) close and re-open the connection. b) is a hack. a) is what you're failing to do.
I'd read all data in the read loop. Then get the last 4 bytes from that string. That is, if you're interested in the latest value. If you need all values, don't do that.
02-01-2018 04:41 AM
Ok, then, I have to concatenate a carriage return in the sent command, and select the CRLF mode in TCP/read?
02-01-2018 04:42 AM
How can I know how much time i have to put?
02-01-2018 04:48 AM
@cristina.lopez wrote:
How can I know how much time i have to put?
As much as you can afford to. It must be more then it takes the read loop to read, or the buffer will simply fail until it overflows.
I'd say 100 ms should be enough, but it all depends on the read loop. If you send the data to serial devices, the read loop might take a lot longer.
A two way stream might be useful. The write loop sends some data, the read loop processes it and let's the write loop it's done. Or the reverse: the read loop posts a request, the write loop sends the data on request.
Bottom line is the loops need to be\stay in sync somehow.
02-01-2018 05:02 AM
Do you know how i can do that the tcp knows automatically how much byte it will be recived?
02-01-2018 05:22 AM
that is what the stop character is for, you just have to set the buffer at least to the amount you expect (including stop character)
02-01-2018 05:31 AM
@cristina.lopez wrote:
Do you know how i can do that the tcp knows automatically how much byte it will be recived?
There's no "bytes at port" for TCP\IP (like there is for serial).
Although \r\n is useful sometime, it won't solve the problem of the buffer being filled faster then reading it. Writing 4 bytes, and reading (multiples of) 4 bytes should work as long as you write slower then you can read.