Sharing Data over the Network: Shared Variables


In applications where you have several computers that need to read or write from some common data variables, LabVIEW provides you with an elegant solution (that is, for the most part, only supported on Windows machines): shared variables. (On other operating systems, the older DataSocket technology provides a similar but far more limited capability.) With shared variables, VIs on different machines on a network can read from or write to these variables, without your having to worry about how to program or handle the communication.

Shared variables also allow you to send data between VIs located in the same application, not just across a network. They allow you to specify options for buffering and single-writer restriction. As you can see, this is a very powerful feature that makes it very easy to create distributed networked applications.

Shared Variables in LabVIEW Projects

To create a shared variable, create a new LabVIEW project (from the File menu, select New . . ., and select Empty Project from the ensuing dialog).

Then, in your LabVIEW Project, pop up on My Computer and select New>> Variable, as shown in Figure 16.14.

Figure 16.14. Creating a new variable in a LabVIEW project


You will be presented with the Shared Variable Properties dialog (see Figure 16.15).

Figure 16.15. Shared Variable Properties dialog


The Shared Variable Properties dialog allows you to specify the following options for your shared variable:

  • Name Specify the name of your shared variable.

  • Data Type Specify the data type of your shared variable.

  • Variable Type Whether your shared variable is a Network-Published variable that can be accessed from remote computers and targets, or a Single Process variable that can only be read on the local computer. LabVIEW modules and toolkits you have installed might provide additional shared variable types, configuration options, and limitationssee the documentation for the specific module or toolkit for more information. (For example, the LabVIEW Real-Time Module installs the Time-triggered variable, which provides deterministic communication over Ethernet networks.)

  • Buffering Enable data buffering and configure the buffer size.

  • Bind to Source Configure the data source of the shared variable. You can choose from DAQ channels, other shared variables, and data items from I/O servers outside the active project.

  • Single Writer This option specifies whether to allow writing to the variable from only one location. If you choose this option, all other locations can only read from the shared variable.

Once you have configured your shared variable settings, you can read or write to it from within your LabVIEW VIs using the Shared Variable structure (found on the Programming>>Structures palette), shown in Figure 16.16.

Figure 16.16. Shared Variable structure shown on the Programming>>Structures palette


Shared Variable Structure (Unassigned)

When first placed on the block diagram, the Shared Variable structure will not be assigned to any shared variable. Double-click the Shared Variable structure (or choose Select Variable from its pop-up menu) to open the Select Variable dialog, shown in Figure 16.17.

Figure 16.17. Using the Select Variable dialog to assign a variable to a Shared Variable structure


Shared Variable Structure (Assigned)

Use this dialog to select a shared variable. After you select the desired variable from the tree control and press OK, the Shared Variable structure will be assigned to the selected variable and change its appearance to reflect the color of the variable's data type and the name of the variable.

You can configure a Shared Variable structure as a Read Shared Variable or a Write Shared Variable from its pop-up menu using the Change to read and Change to write options, respectively. Figures 16.18 and 16.19 show the differences in appearance between the two. Notice that the Read Shared Variable has a thicker border (because it is a data source) and has an arrow showing that data flows out from its right side. Likewise, the Write Shared Variable has a thinner border (because it is a data sink) and has an arrow showing that data flows into it on its left side.

Figure 16.18. Read Shared Variable


Figure 16.19. Write Shared Variable


The preceding instructions showed you how to assign a new (empty) Shared Variable structure to a shared variable in your LabVIEW project. However, an easier way to achieve the same result is to drag and drop a shared variable from the Project Explorer window onto the block diagram of any VI. This will create a Shared Variable structure on the block diagram that is associated with the one that was dragged from the Project Explorer window.


In some ways, shared variables are a lot like global variables; whereas globals share data within a set of VIs on a single computer, shared variables can share data across multiple computers on a network.

Unlike global variables, shared variables have special synchronization capabilities built into the model that can help you avoid the type of race conditions that often occur from improper use of regular globals. Because of these intricacies, you will definitely want to take the time to understand how they work, before using them casually. For considerations and detailed instructions related to deploying applications using shared variables, you will want to refer to the Fundamentals>>Networking in LabVIEW>>Concepts>>Sharing Live Data Using Shared Variables section of the LabVIEW Help.

Binding Controls and Indicators to Shared Variables

A very simple way to write and read to shared variablesit requires no programming at allis data binding. Data binding allows a control or indicator to be connected, behind the scenes, to a shared variable. When a user changes the value of a control that is bound to a shared variable, the value is written to the shared variable. When a shared variable's value changes, any indicator that is bound to the shared variable will update its value to that of the shared variable. It's that easy, and it doesn't require any code on your block diagram!

To bind a control or indicator to a shared variable, first open its Properties dialog by right-clicking on the control or indicator and selecting Properties . . . from the pop-up menu. Browse to the Data Binding tab of the Properties dialog. You may have to press the right-arrow button in the upper-right corner of the tab control to make the Data Binding page visible (this right-arrow button is highlighted in Figure 16.20).

Figure 16.20. Use the right-arrow button to find the Data Binding tab of the Properties dialog, where you can configure the binding of a control to a shared variable.


Set Data Binding Selection to Shared Variable Engine (NI-PSP), set Network-Published Source to Network Items, and then press the Browse button to select the desired shared variable from the network.

Finally, choose the desired Mode from the following options:

  • Read Updates a control or indicator value whenever the value of the shared variable changes

  • Write Write to the shared variable whenever the user changes the value of the control.

  • Read/Write Allows both the Read and Write mode behaviors. The control will update when the shared variable value changes, and the control will update to reflect changes in the value of the shared variable.

Programmatically Accessing Shared Variables Using DataSocket

You can read and write to shared variables programmatically using the DataSocket Read and DataSocket Write functions, which can be found on the Data Communication>>DataSocket palette (see Figure 16.21).

Figure 16.21. DataSocket palette


DataSocket Read (Data Communication>>DataSocket palette) dequeues the next available data value from the client-side buffer associated with the connection you specify in connection in and returns the data (see Figure 16.22).

Figure 16.22. DataSocket Read


DataSocket Write (Data Communication>>DataSocket palette) writes data to the connection you specify in connection in. The connector pane displays the default data types for this polymorphic function (see Figure 16.23).

Figure 16.23. DataSocket Write


Shared Variable URLs

In order to use the DataSocket VIs to read or write to shared variables, you will need to know the computer name, project library name, and shared variable name where the shared variable resides.

Shared variables must be created inside LabVIEW Project Libraries. You will need to know the library name, in order to access it programmatically using the DataSocket functions.


With this information, you will construct a URL (which stands for Uniform Resource Locator) string that has the following format:

 psp://computer/library/shared_variable 


Or, if your shared variable resides in a sublibrary of a library, then the URL will have the following format:

 psp://computer/library/sub_library/shared_variable 


Reading Shared Variables Programmatically

To read a shared variable's value, you will pass the shared variable's URL to the DataSocket Read function, as shown in Figure 16.24.

Figure 16.24. Using DataSocket Read to read a shared variable's value programmatically


The data returned from the DataSocket Read function is the shared variable's value as a variant data type. This is where all of the skills you learned in Chapter 14, "Advanced LabVIEW Data Concepts," for operating on variants will come in handy. Use the Variant To Data function, as shown in Figure 16.25, to convert the variant into a DBL numeric.

Figure 16.25. Converting the variant returned by DataSocket Read to a numeric type


You probably also noticed in Figure 16.25 that there are attribute data inside the variant that is returned by DataSocket Read. These attributes can be accessed using the Get Variant Attribute function that you learned about in Chapter 14.

Writing Shared Variables Programmatically

In order to write to a shared variable's value programmatically, you will use the DataSocket Write function, passing it the value to write and the shared variable's URL.

Figure 16.26. Using DataSocket Write to write a shared variable's value programmatically


Maintaining Shared Variable Connections

When you wire a URL into the DataSocket Read and DataSocket Write functions, they establish a network connection, perform the read or write operation, and then disconnect. If you are going to read or write many times, you may wish to stay connected in order to avoid spending time establishing the connection each time you want to read or write the data. In this case, you will want to use the DataSocket Open function to obtain a connection reference (connection id) that can be passed to DataSocket Read and DataSocket Write. When you are finished reading and writing data, use DataSocket Close to close the network connection.

DataSocket Open (Data Communication>>DataSocket palette) opens a data connection you specify in URL (see Figure 16.27).

Figure 16.27. DataSocket Open


DataSocket Close (Data Communication>>DataSocket palette) closes a data connection you specify in connection id (see Figure 16.28).

Figure 16.28. DataSocket Close


Figure 16.29 shows an example of how to open a persistent connection to a shared variable and read its value multiple times before finally closing the connection.

Figure 16.29. Opening a persistent connection to a shared variable and reading its value multiple times while connected


DataSocket Read and DataSocket Write allow you to wire either a URL or a connection id reference that was created using the DataSocket Open function. If you wire a URL, then they will connect, read or write, and then disconnect. If you wire a connection id, then they will use that connection and leave the connection open after they are finished.





LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net