Using
TCPIP Connections In LabView
TCP/IP is a way of sending and receiving data reliably over a network.
TCP/IP functions are built into LabView, and give the instrumentation and
control engineer a way of transmitting information across the network reliably.
Here are the some general comments on using TCP/IP.
-
TCP/IP gives you a way
to use measurement data at locations remote from where the measurements
are performed.
-
You might take measurements
on water quality in a stream - or in many streams - and transmit data over
a wireless network to a central point for analysis. In many cases
the volume of data might exceed what a floppy disk can store, and the limitations
on data storage are considerably easier to work with when data is transmitted
easily.
-
In a production facility,
you might want to do control from a central location, and "keep tabs" on
a number of controlled processes from one place. That would be especially
true if the processes interact - as they would if they were different parts
of a production line.
TCP/IP is an Internet
Protocol (IP). The TCP part of the name refers to Transmission
Control Protocol. When you send something over the net it
is subdivided into packets, and each packet could take a different route
from one computer to the other over the net. Those packets could
arrive at different times and be out of order. Some might not make
it at all, and when that happens, TCP will request a re-send to be sure
that is has all of the packets. Then, TCP reassembles the packets.
The nice thing is that you don't have to worry about all of those things.
TCP makes it all happen reliably.
If you want to use TCP/IP
you need to do the following in LabView.
-
Establish a TCP/IP Listen.
That's on the Functions - Communication -
TCP palette. Here is the icon for the
TCP/IP Listen.
-

-
The TCP/IP Listen listens over the network for another
computer trying to connect over the port specified in the integer input
at the left side of the icon. That port number must be a number not
used by other parts of the system, and we have used 2055, arbitrarily.
-
When the TCP/IP Listen hears another computer trying
to connect over the correct port, it establishes a Connection
ID, and an Error
Signal, and those outputs are used in later
TCP/IP vi blocks.
-
You cannot use another TCIP/IP Listen with the same
port number once the connection has been established.
-
After a connection is established and the Connection
ID is available, you can sent and recieve data over the network, and the
connection is a reliable connection between the two computers involved.
You use either of the following vis.
-
TCP/IP Write - which
writes data to the other computer over the network. Here is the icon.
-

-
The TCP/IP Write uses the Connection ID, which it
also passes to the next vi, and the Error signal which it also passes to
the next vi.
-
The input is a string.
It's much harder to use numerical information directly, so you will have
to convert your data to a string format.
-
There are other inputs and outputs which we have
ignored, but you can specify a timeout, and you can get information on
the number of bytes written over the network with this vi.
-
TCP/IP Read - which reads bytes from the remote computer.
Here is the icon.
-

-
There are the usual inputs, the Connection ID and
the Error Signal.
-
The integer input (blue line at left) is the number
of bytes that you want to read.
-
The output is the string composed of the bytes that
are read.
Examples
E1 - Using
TCP/IP
Write. This is the diagram of a sub-vi
we have developed that will write a string to a remote computer.
Click
here for a more detailed look at this vi.

Note the following steps that are needed to send
a single string.
-
Assuming that you have a string input and an open
connection, you must first compute the length of the string.
-
The length of the string is a number, and that number
must be converted to a string.
-
That String-Length string
is sent using a TCP/IP Write.
-
Then, the string itself is sent.
-
The Connection ID and Error Signal are passed to
further TCP/IP blocks, and eventually the connection should be closed.
E2 -
Using
TCP/IP Read.
This is the diagram of a sub-vi we have developed that will receive a string
send from a remote computer. Click
here for a more detailed look at this vi.

Note the following steps that are needed to receive
a single string.
-
Assuming that you have an open connection, you assume
that the length of the first string received will be 2. The first
string you receive has the data for the string length of the string you
want. For example, the string could be 17 bytes long, and you need
to know that value - 17. You use a TCP/IP
Read to get the string length.
-
The length of the string is a string, and that number
must be converted to a number, and that number is used in the second TCP/IP
Read.
-
If you want to perform numerical calculations on
the data you received you will need to convert that data to a number.
As it stands, the data out is in string format.
There is one final thing
that you will need. When you are done doing whatever you want to
do over the network, you need to close the connection. Use the TCP/IP
Close Connection vi, with the icon shown below.
And that is just about all you need to do.