LabView Tutorial 11 - TCP/IP
In this tutorial, we begin to learn about how to use the TCP/IP communications facilities within Labview.
With these communications facilities, we'll be able to send initial data to a server program (written in 'C' and incorporating the physics which defines our simulation), as well as receive output data from this program for display on our Front Panel.
First, it should be noted that this tutorial was constructed using LabView 6.1;
under version 7.X and 8.X the pull down menus exist in the same order, except
the top level is now under classic icons.
Figure 1: The Communications menu...
three icons at the top, in the middle denote the TCP/IP functions in LabView that we'll be using;
- open connection
All three can be found under the Communication/TCP menu (see the figure below).
Figure 2: ...and the TCP sub-menu within it.
Take the following steps to create a Connection:
- First, select the TCP Open Connection option on the TCP menu. The menus will disappear, and an Open Connection icon will appear in the Block Diagram.
- Switch to the wiring cursor and hover
over the Open Connection Icon. When you're over the icon, the wiring terminals are
- This function will accept eight connections, four input and four output.
In practice, however, only two inputs are used, and one output.
The two inputs that need to be wired are the address
and remote port.
- The address is a character string; it's the name of the machine that is running the server.
- The port is an integer. It denotes which port on the machine designated by the address field Labview will be connecting to.
Figure 3: The TCP Connection node, with Address and Remote Port input wires
The Front panel at this point shows two controls, the address of the server and the port that the sever is listening on.
Figure 4: The Front Panel, with Address and Port Inputs
In nearly all projects there are variables that are static; these are set at the onset of the program and the user is unable to modify them until the program is restarted.
Writing of all static variables is done immediately after the open connection call.
- Return to the TCP/IP menus and select the write icon.
- Next, create a control on the front panel.
TCP/IP is a byte stream, this means that floats, doubles, integers all must be converted into as individual bytes.
- Since we have created a double to be written, we have to convert this 8 byte item into 8 individual bytes.
- To do this, select the type cast icon under the Advanced > Data Manipulation menu.
Figure 5: The type cast option from the Advanced > Data Manipulation menu
The type cast function converts from one data type into another; in this case, we need to convert a double into a byte stream.
- Wire the double as the input to the type cast,
- then, wire a byte as the data type (if you right click over the center of the type cast icon and select create constant, byte is the default).
- Wire the output of the cast to the data in terminal on the write icon.
- Finally, wire the connection ID from the open connection icon to the write icon's connection ID.
The connection id is the file descriptor for the socket; this will be covered in greater detail in the Server section, where we explore in detail the operation of sockets.
Figure 6: Completing the wiring
The LabView client can now connect to the server and write a single static variable.
For interactive control the user must be able to modify the scene. This is accomplished through the use of dynamic variables. From the communication perspective, these variables look and function just as the static variables.
- Create a Control
- use the type cast icon to convert the Control into a byte stream
- write the stream to the socket with the write icon.
- Since this is a dynamic control, the value of the Control must be written constantly to the server. In order to repeatedly write, wrap the dynamic variable and the write associated with it in a While loop.
Figure 7: Wrapping the dynamic variable and write icon in a While loop.
This is the basic look and order of all projects;
- connect to the server,
- write the static variables
- write the dynamic variables
- continue until the user decides to end the loop.
The LabView client will become more complicated when physical systems are attached to the client.
Figure 8: The Front Panel after all settings and wirings
Proceed to the Review Section ->