Sharing Data over the Network: Shared
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):
. (On other operating systems, the older
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.
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
New . . .
, and select
from the ensuing dialog).
Then, in your LabVIEW Project, pop up on
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).
Shared Variable Properties
Shared Variable Properties
dialog allows you to specify the following options for your shared variable:
Specify the name of your shared variable.
Specify the data type of your shared variable.
Whether your shared variable is a
variable that can be accessed from remote computers and targets, or a
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
which provides deterministic communication over Ethernet networks.)
Enable data buffering and configure the buffer
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.
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
structure (found on the
palette), shown in Figure 16.16.
structure shown on the
Shared Variable Structure (Unassigned)
When first placed on the block diagram, the
structure will not be assigned to any shared variable. Double-click the
structure (or choose
from its pop-up menu) to
dialog, shown in Figure 16.17.
Figure 16.17. Using the
dialog to assign a variable to a
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
structure will be assigned to the selected variable and change its appearance to reflect the
of the variable's data type and the name of the variable.
You can configure a
structure as a Read Shared Variable or a Write Shared Variable from its pop-up menu using the
Change to read
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
instructions showed you how to assign a new (empty)
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
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
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 allows a control or indicator to be connected, behind the scenes, to a shared variable. When a
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
dialog by right-clicking on the control or indicator and selecting
Properties . . .
from the pop-up menu. Browse to the
tab of the
dialog. You may have to press the
button in the upper-right corner of the tab control to make the
page visible (this right-arrow button is highlighted in Figure 16.20).
Figure 16.20. Use the right-arrow button to find the
tab of the
dialog, where you can configure the binding of a control to a shared variable.
Data Binding Selection
Shared Variable Engine (NI-PSP)
, and then press the
button to select the desired shared variable from the network.
Finally, choose the desired
from the following options:
Updates a control or indicator value whenever the value of the shared variable changes
Write to the shared variable whenever the user changes the value of the control.
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
functions, which can be found on the
palette (see Figure 16.21).
palette) dequeues the
available data value from the client-side buffer associated with the connection you specify in
and returns the data (see Figure 16.22).
data to the connection you specify in
. The connector pane displays the default data types for this polymorphic function (see Figure 16.23).
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:
Or, if your shared variable resides in a sublibrary of a library, then the URL will have the following format:
Reading Shared Variables Programmatically
To read a shared variable's value, you will pass the shared variable's URL to the
function, as shown in Figure 16.24.
Figure 16.24. Using
to read a shared variable's value programmatically
The data returned from the
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
. 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
function, passing it the value to write and the shared variable's URL.
Figure 16.26. Using
to write a shared variable's value programmatically
Maintaining Shared Variable Connections
When you wire a URL into the
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
function to obtain a connection reference (
) that can be passed to
. When you are finished reading and writing data, use
to close the network connection.
palette) opens a data connection you specify in
(see Figure 16.27).
a data connection you specify in
(see Figure 16.28).
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
allow you to wire either a
reference that was created using the
function. If you wire a URL, then they will connect, read or write, and then disconnect. If you wire a
then they will use that connection and leave the connection open after they are finished.