LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

String order - TCP

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.

Rolf Kalbermatter
My Blog
0 Kudos
Message 11 of 18
(744 Views)

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.

0 Kudos
Message 12 of 18
(740 Views)

Ok, then, I have to concatenate a carriage return in the sent command, and select the CRLF mode in TCP/read?

 

 

0 Kudos
Message 13 of 18
(737 Views)

How can I know how much time i have to put?

0 Kudos
Message 14 of 18
(735 Views)

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

0 Kudos
Message 15 of 18
(727 Views)

Do you know how i can do that the tcp knows automatically how much byte it will be recived?

0 Kudos
Message 16 of 18
(717 Views)

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)


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 17 of 18
(711 Views)

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

0 Kudos
Message 18 of 18
(702 Views)