LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Opening and closing TCP/IP connection in each iteration

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.

0 Kudos
Message 1 of 10
(6,895 Views)

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

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 2 of 10
(6,885 Views)

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.

0 Kudos
Message 3 of 10
(6,830 Views)

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:

  • The squence structure serves no purpose. Execution order is already fully determined by dataflow alone.
  • Why in the world would you branch the blue wire inside the sequence and index one element inside and one outside? That makes no sense! Index array is resizeable and you don't even need to wire the indices.
  • "insert into array" is the wrong function to prepend some elements to an existing array and building an array that always has the same size. In fact you should initialize an array of four elements and replace two, keeping the operations in place or simply use "build array" with four inputs (recommended).
  • The elapsed time express VI is not the most efficient way to determine loop rate and why are you wiring a large seemingly random constant to the time input? Makes no sense!
  • Some of your error wires are missing. It makes no sense to send data if the connection cannot be established, for example.

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.

Message 4 of 10
(6,824 Views)

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.

0 Kudos
Message 5 of 10
(6,808 Views)

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

Message 6 of 10
(6,802 Views)

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.

0 Kudos
Message 7 of 10
(6,769 Views)

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

not helpful.PNG

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.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 8 of 10
(6,731 Views)

Thanks for your note.

0 Kudos
Message 9 of 10
(6,705 Views)

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

Message 10 of 10
(6,686 Views)