You do not need to use the DataSocket Write and DataSocket Read functions to publish and subscribe data using the DataSocket Server. You can establish a DataSocket connection from a front panel object. Right-click any front panel object and select Data Operations»DataSocket
Connection to display the DataSocket Connection dialog box.
In the Connect To field, enter a URL and select whether you want to publish, subscribe, or both. Then check the Enabled box.
When you click the Attach button, the front panel object is now available at the specified URL in the DataSocket Server. A small rectangle in the top right of the control or indicator appears, indicating the status of the DataSocket connection. If the rectangle is green as shown below, the connection is good.
If the rectangle is gray, there is no connection to the server at that time. If the rectangle is red, the connection has an error.
Now build two VIs that transfer data using DataSocket.
Exercise 3-5 DS Generate Data VI and DS Read Data VI
Objective: To build two VIs that use DataSocket to transfer data.
Build a VI that generates a random number and displays this value in a meter and sends the data to a DataSocket URL. Then build a second VI that reads the DataSocket URL and displays the value in a slide. Last, use the
automatic publish and subscribe features to send the data through a DataSocket connection.
Front Panel
1. Open a new VI and build the front panel shown above. The URL object is a string control.
Block Diagram
2. Open and build the block diagram shown above using the following components:
a. While Loop, available on the Functions»Structures
palette—Structures the VI to continue running until you press the Stop button. Right-click the Conditional terminal and select Stop If True.
b. Wait Until Next ms Multiple function, available on the Functions»
Time & Dialog palette—Causes the While Loop to execute once per
c. Random Number (0-1) function, available on the Functions»
Numeric palette—Creates a random number between zero and one.
d. Multiply function, available on the Functions»Numeric
palette—Multiplies two numbers together and is used here to scale the random number to be between zero and 10.
e. DataSocket Write function, available on the Functions»
Communication»DataSocket palette—Writes the random data value to the specified URL.
f. Simple Error Handler VI, available on the Functions»Time &
Dialog palette—Opens a dialog box if an error occurs and displays the error information.
3. Save this VI asDS Generate Data.vi.
4. Now build the second VI to read the random value.
Front Panel
1. Open a new VI and build the front panel shown above. The URL object is a string control. To show the scale on the slide indicator, right-click the slide and select Scale»Style.
Block Diagram
2. Open and build the block diagram shown above using the following components:
Stop button. Right-click the Conditional terminal and select Stop If True.
b. Wait Until Next ms Multiple function, available on the Functions»
Time & Dialog palette—Causes the While Loop to execute once per second. Create the constant by right-clicking the input terminal and selecting Create»Constant.
c. Numeric Constant, available on the Functions»Numeric palette—Creates the correct data type to read the value through DataSocket. Make sure this constant is a DBL by right-clicking it and selecting Representation»Double Precision from the shortcut menu.
d. DataSocket Read function, available on the Functions»
Communication»DataSocket palette—Reads the random data value from the specified URL.
e. Simple Error Handler VI, available on the Functions»Time &
Dialog palette—Opens a dialog box if an error occurs and displays the error information.
3. Save this VI asDS Read Data.vi.
4. Position the front panels of the DS Generate Data and DS Read Data VIs so that you can see both front panels.
5. Start the DataSocket Server by going to the Start menu and selecting Programs»National Instruments»DataSocket»DataSocket Server.
The DataSocket Server window, similar to the one below, appears.
6. Return to the two VI front panels and make sure that the URLs have been entered the same for both VIs.
• dstp—The DataSocket transfer protocol.
• localhost—The current computer you are using.
• data1—The name given to the random number you will be sending.
7. Run the DS Generate Data and the DS Read Data VIs.
The DataSocket Server window shows one process connected, and the Number of packets value increments each second as the meter and slide show the same random numbers.
8. Stop both VIs when you are finished. Now modify the VIs to use the automatic publish and subscribe capabilities of front panel objects.
Front Panel
9. Place a digital indicator on the front panel of the DS Read Data VI as shown above.
10. Right-click the new digital indicator and select Data Operations»
DataSocket Connection from the shortcut menu. Enter the following values.
11. Click the Attach button and a little gray rectangle appears to the top right side of the digital indicator. This indicates that the DataSocket connection is not active.
12. Run the DS Generate Data and DS Read Data VIs again.
The rectangle next to the Random Number indicator turns green, and the value matches the values shown in the meter and the slide.
Note If your computer is on a network with the other computers used in class, you can type in URLs for other machines on the network and transfer the values between classroom computers. Remember that you can use any programming language or operating system with DataSocket connections. Go to the National Instruments
13. Stop and close both VIs and the DataSocket Server when you are finished.