DAQmx in Measurement Studio: Part 3

The last two months we have covered how to do data acquisition in .NET. This month we will go over how to integrate LabVIEW and .NET together in a hybrid application. I am going to demonstrate how to use a LabVIEW server application to acquire data using DAQmx and send it to a .NET client application. The client can then display the data using more visually appealing Microsoft controls and then send the data back to the server to demonstrate two-way TCP communication.

Finnegan1

The first application we will need is a LabVIEW server application. It will listen for a TCP connection on a port (I chose 255) and then open two parallel while loops. One of the loops will write to that TCP connection and one of them will read from it.

As you can see in the top loop, I am reading an analog voltage from a DAQ device named Dev1. This is just a simulated device set up in NI-MAX. I am simulating a USB-6001 NI device. With no additional parameters configured, it should just continually generate a sine wave for demonstration. As the data is read from the device, the length of the data is calculated and a TCP message is sent containing the length plus the data. We need to send the length so that the client knows how much data to expect to read on the other end. This loop will just continue executing and feeding data to the .NET client until the application is stopped.

In the bottom loop we are reading from the TCP connection. Similarly to the top loop, we need to read first the length of the data, followed by the actual data. I have sent this data both to a string indicator and a waveform chart to show that the data is being received back and graphs it correctly.

Finnegan2

The next application needed is a C# client application. It will run three separate threads – one to read from TCP, one to write to TCP, and one to update the graphical interface.

Finnegan3

First, we will set up our TCP connection. You will need to use the System.Net namespace to gain access to the TCP objects and methods needed. Start by creating a client and connecting to the port you used in the server application (255). Then start up a NetworkStream. After that, we create a few local variables for use in the threads.

Finnegan4

Next, create a thread to read the TCP data from the LabVIEW server. If there is data available on the stream, read first the length and then the actual data and store it in the TCPString local variable.

Finnegan6

Next, create a thread to write the TCP data back to LabVIEW. First calculate the length and write that, followed by the actual data.

Finnegan7

The last thread is used to update the graphical form. It creates the form and then while the application is running, writes the data received via TCP to that form’s writetext() method.

Finnegan8

Don’t forget to start your threads.

Finnegan9

Not much code is needed to actually display the data. There are a couple textboxes to show the data and its length, and the data points received are also fed into the chart one by one. The chart control automatically takes care of scaling and displaying the data.

Finnegan10

As you can see, the .NET application is graphing the sine wave data as it is received, and the LabVIEW server is receiving the data back and updating a chart of its own. A TCP client-server architecture is one good example of a way to utilize the display capabilities of .NET and the data acquisition capabilities of LabVIEW in one application.