LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle threading for TCP communication?

Solved!
Go to solution

Hi,

I've a project to control two separate applications (C++ and LabView) and both of them are implemented as server. Another client program (C++) is used to control those application. The communication among them are implemented using TCP socket. The client program sends message to start or stop some tasks on the servers. The client program also sends time (hh:mm:ss) with the start and stop message to determine when to start or stop.

 

In C++ application (server): The application listens for connection and when gets  one, it creates a communication thread to handle that and again listen for another connection. The communication thread handles message passing and when it receives start or stop message, it creates a timer thread with the received time to trigger a task at specified time. And after that it waits for message from the client. So, here, when thread (communication, timer) is needed, it is created.

 

In LabView (server): I've tried to create same thing as the C++ server. But, from LabView manual and others forum threads, I got that LabView is multithreading and it can be done using independent loop. So, I'd to create four loops in a diagram:

 

1. Listen for new connection

2. Handle communication for already received connection

3. Start timer

4. Stop timer

 

and they are run at the beginning of the execution and communication among them are managed using local variables. But, 2,3 and 4th loop can handle only one connection and it can handle another if the current is closed. But, the C++ application can handle more than one connection by creating thread when it is necessary but not at the beginning of the execution of the application.

 

Is there a better way to implement this in LabView?

Is it possible to handle multiple connections and create diagram node/block (like thread) dynamically like C++?

 

Thanks.

Using LabVIEW 2010, Visual C#, Visual C++ (2005, 2008, 2010)
0 Kudos
Message 1 of 4
(3,100 Views)
Solution
Accepted by topic author MARK002-MAB

There are several ways to do this in modern LabVIEW and you should probably search the Example Finder for some TCP examples. The classic way is to transfer the connection refnum from the listen loop to a communication loop that adds it to an array of connection IDs and then continously iterates over this array to do the communication. It works since about LabVIEW 4.0 perfectly for me even for applications with HTTP based communication protocol. But you need to make sure of course that the communciation handling for one connection isn't delaying its work for some reasons as that would delay the handling of the other connections too, as they are really worked on sequentially. If you encounter an error, the connection ID is closed and removed from the array.

 

The other is that you create a VI that does your whole communication and terminates itself on an error or when getting the quit command. Make this VI to be reentrant and then launch it through VI server as reentrant instance, passing it the newly received connection refnum form the listen loop. Then use the Run method to let it start and operate like an independant thread.

 

For all of these you should be able to find an example in the Example Finder when searching for TCP.

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 4
(3,090 Views)

@rolfk wrote:

you should be able to find an example in the Example Finder when searching for TCP.


In LabVIEW 2011 there is a useful example: "DateServerUsingStartAsynchronousCall.vi".

0 Kudos
Message 3 of 4
(3,070 Views)

Hi rolfk,

 

I've already seen the example "Multiple Connections - Server.vi". It uses array to iterate through connections. I was looking for a better way. I read a little about VI server but thought they are used only to connect to remote LabView instance. There is an example named "DateServerUsingReentrantRun.vi" which is similar to what you said about reentrant vi and VI server. That's like what I'm looking for. I'll give it a try.

 

Thanks.

Using LabVIEW 2010, Visual C#, Visual C++ (2005, 2008, 2010)
0 Kudos
Message 4 of 4
(3,043 Views)