10-28-2016 04:16 AM
Hi,
I have made a program to send and recieve data from a microcontroller over a tcp/ip connection. This program sends and receives some bytes in a while loop. I've attached a snippet.
Is it necessary and efficient to open and close the tcpip connection in each iteration or there exists another way to perform this?
Thanks.
10-28-2016 04:39 AM
Yes, there is: Move TCP Open and Close out of the loop and keep the connection reference in a shift register.
However, choosing one implementation option over the other depends on the situation:1
1) How often/fast does that communication iterate? If it is low number and non-performance-critical, repetative open/close is OK.
2) How stable is the connection/data transfer? That effects error handling strategies. Repetative open/close could get rid of errors more easily.
3) What is the expected/required through-put vs. iteration time of the current implementation? If there is plenty of time left, repetive open/close might not be the best choice, but still a working one.
Norbert
10-28-2016 10:47 AM
Many Thanks for answer.
Actually I would like to perform real-time data acquisition. The least iteration time is now 5ms. I ran the program for a long period of time and there was no problem. The CPU usage was also low. As the data acquisition may be performed for long time periods without any supervision, the program must be stable and robust. In current program if we disconnect the LAN cable and connect it again, the program continues to work after a short time.
With 5ms iteration, there is a little drift in time. i.e, 1 ms in every 300 seconds. I mean that the program is not strictly real-time. I think it is reasonable because of non-deterministic nature of Ethernet connection.
10-28-2016 11:25 AM - edited 10-28-2016 11:31 AM
Unlike e.g. UDP, TCP is connection based (both are part of TCP/IP, so you need to be more precise in the problem description). Establishing a TCP connection between two end points required a mutual exchange of packets (three way handshake, look it up (SYN, SYNACK, ACK) as well as several packets to tear down the connection. This is a lot of overhead and if the connection is not perfect (e.g. a few % packet loss), you can get significant stalls because of the various timeouts for retransmission requests and such. In your case, the payload is only a few bytes so most of the trasferred data is connection management. (Note that your IP address is not from a designated private adress range, so I assume that the connection involves quite a few public hops and is not local.)
The correct way is to open the connection once but add special code to reestablish the connection whenever the connection is lost. Trivial to do.
In any case, just looking at the code picture, I see a lot of beginner mistakes and other weird stuff, so I would also recommend to streamline the code overall. Here are some points:
You need to be careful with terminology. "real time" has nothing to do with speed. Who wrote the server program? If you can change it, you could buffer data and send accumulated data with timestamps. You could even use UDP if performance is more important than the occasional missing point.
10-28-2016 01:09 PM
Thanks for long and beautiful answer.
I selected TCP/IP over UDP because the correct sequence of data is more important than performance. The program should transceive only 50 bytes. The data should be sent first so I placed the flat sequence in the while loop.
Thanks for technical advices. I will make the corrections.
Actually I don't need a real-time implementation. And the weird number in the elapsed time express vi is the seconds of 24 hour! I tested the program for a long time.
Thanks again.
10-28-2016 01:49 PM
@iman_h wrote:The data should be sent first so I placed the flat sequence in the while loop.
Dataflow dictates that reading cannot start until writing has completed because there is a data dependency (error wire and connection reference). The sequence structure does not add antything extra except for cluttering the diagram.
10-29-2016 06:40 AM
Hi dear altenbach,
I changed the program to avoid continious connect and disconnect. I attached a snippet. This program does not work. After reading a byte, the inner while loop iterates but it seems that the connection becomes unavailable and error code 66 occures. (The peer closed the connection.) So the inner loop halts and a new connection is made.
I used one of the NI's whitepapers: "Basic TCP/IP communication in LabVIEW".
Thanks.
10-30-2016 09:06 PM
@iman_h wrote:Hi dear altenbach,
I changed the program to avoid continious connect and disconnect. I attached a snippet. This program does not work. After reading a byte, the inner while loop iterates but it seems that the connection becomes unavailable and error code 66 occures. (The peer closed the connection.) So the inner loop halts and a new connection is made.
I used one of the NI's whitepapers: "Basic TCP/IP communication in LabVIEW".
Thanks.
Why all the error indicators? Troubleshooting?
And that's not a snippet. It's a picture. Just like this isn't a code snippet.
LabVIEW snippets are png files with metadata that allows the picture to insert code into a block diagram when it is dropped on one. You create one by selecting code on your block diagram and selecting "Create VI Snippet from Selection" from the "Edit" menu.
10-31-2016 09:19 AM
Thanks for your note.
10-31-2016 10:57 AM
Look into state machines. You only need one while loop.
Currently, your program cannot be stopped if there is no error. You can also wire the error directly to the stop terminal, no need to unbundle. Instead of "not"-ing the status, just change the stop condition to "stop if true".