Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Using Shared Variables in VIs

LabVIEW 8.5 Help
August 2007

NI Part Number:
371361D-01

»View Product Info

After you create a shared variable, you can access the shared variable data in LabVIEW in several ways, regardless of the configuration:

  • Front panel data bindingBind front panel objects. You can bind front panel objects only to network-published shared variables.
  • Shared Variable nodeRead and write shared variable data on the block diagram with little to no programming.
  • DataSocket functionsRead and write shared variable data on the block diagram with more options for customization.
  • VI Server interface—Use Variable properties and methods and Control properties and methods to configure shared variables or control shared variable lifetimes. You also can use the Library methods to programmatically create libraries and shared variables in applications where you need to create complicated or large numbers of connections dynamically. You also can use the Variable Reference property with the Variable properties to configure shared variables programmatically.
Note  You can create, configure, and host shared variables only on Windows or RT targets with the LabVIEW Real Time Module. You can use the DataSocket VIs and functions to read or write shared variables from other platforms. If you have a VI with a Shared Variable node that was configured on a Windows system, you also can move that VI to another platform.

Reading and Writing Shared Variable Values on the Front Panel

Use front panel data binding to read or write live data in a front panel object.

Note  You can bind front panel objects only to network-published shared variables.

Drag a shared variable from the Project Explorer window to the front panel of a VI to create a control bound to the shared variable. You also can use the options on the Data Binding page of the control Properties dialog box to bind the control to a shared variable or to an NI Publish-Subscribe Protocol (NI-PSP) data item on the network.

Note  If you are using a network-published Shared Variable node that is configured to read data, you must run each Shared Variable node before the node subscribes and starts receiving buffered values. If a buffer for a network-published shared variable overflows, the data is lost, and LabVIEW does not return an error.
Note  Opening a VI containing a Shared Variable node in a project where the Shared Variable node cannot find its associated shared variable in the Project Explorer window causes the Shared Variable node to break. Any front panel controls associated with the missing shared variable also break. This behavior is specific to Windows and only occurs when you open the VI in a project. If you open the VI in the main application instance, you do not receive notification of missing shared variables.

When you enable data binding for a control, changing the value of the control changes the value of the shared variable to which the control is bound.

Front panel data binding is primarily intended for sharing live data. To read data in local files, FTP servers, or Web servers, use the DataSocket Read function, the File I/O VIs and functions, or the Application Control VIs and functions.

You can set the value of a front panel control directly on the front panel and then write the data, or you can build a block diagram, wire the output of a VI or function to an indicator, and write the data from that indicator. Typical scenarios for using front panel data binding with shared variables or NI-PSP data items include the following:

  • Manipulate a front panel control and write the data for other users to read through a control or indicator. For example, if you place a knob on your front panel that raises or lowers temperature, a user on another computer can view the data in an indicator or read the data and use it in a control wired to a subVI or function.
  • Write a value that appears in a front panel indicator so another user can read and view the data in a control or indicator on the front panel or use the results as data in a control wired to an input in a subVI or function. For example, a VI that calculates the mean temperature and displays the temperature in a thermometer on the front panel can write the temperature data.
  • Read a value that appears in a control or indicator on the front panel of another VI to view data in a front panel indicator on your VI. If you read the data with a control, you can use the data in your VI by wiring the control to an input of a subVI or function.
  • Read and write data from a front panel control so users can manipulate a control on the front panel of a VI from the front panel of another VI. When you run the first VI, the front panel control on that VI retrieves the current value that the second VI or application wrote through a shared variable. When a user changes the control value on the front panel of the first VI, the shared variable writes the new value to the front panel control of the second VI. If you then manipulate the value of that front panel control, the second VI writes the value to the front panel objects in other VIs that are bound to the shared variable. If the front panel control has write-only access, the control writes the initial value when the VI runs or every time the VI resets if the VI runs continually. If the front panel control has read-only or read/write access, the control reads the initial value when the VI runs.

The front panel objects that read data do not have to be the same kind of objects that write the data. However, the front panel objects must be the same data type. If the front panel objects are of the numeric type, the data types must coerce. For example, you can use a digital indicator in your VI to view the data that a thermometer in another VI generates. The thermometer can be a floating-point number, and the digital indicator can be an integer.

Front Panel Data Binding and DataSocket Connections

You can connect front panel objects using the dstp, opc, ftp, http, and file protocols by selecting DataSocket from the Data Binding Selection drop-down list on the Data Binding page of the control Properties dialog box. However, using front panel data binding to control a front panel control remotely through the Shared Variable Engine provides faster and more reliable data transmission as well as seamless connections to shared variables and NI-PSP data items. Select Shared Variable Engine (NI-PSP) from the Data Binding Selection drop-down list on the Data Binding page of the control Properties dialog box to bind the control through the Shared Variable Engine.

Front panel data binding connections through the Shared Variable Engine use NI-PSP, the protocol that transmits shared variable data. dstp connections pass through the DataSocket Server.

Troubleshooting Front Panel Data Binding

While the VI is running, if the connection to the Shared Variable Engine is successful, a small green indicator appears next to the front panel object on the VI. If the connection is not successful, a small red indicator appears. Before LabVIEW attempts to connect to the server, a small transparent indicator appears. A status message also appears if you move the cursor over the indicator. You can hide this indicator by right-clicking the front panel object and selecting Visible Items»PSP LED from the shortcut menu. You also can use the Data Binding:LED Visible property to hide the indicator programmatically.

If you receive errors when attempting to read or write data from the front panel, investigate the following possible causes:

  • If binding to a shared variable in another project, the shared variable must be currently deployed. To deploy the shared variable, right-click the owning project library and select Deploy from the shortcut menu. (RT Module) You also can right-click the computing device and select Deploy All to deploy all project libraries for shared variables on that target. Deploy All also opens and deploys any VIs that contain the shared variables on that target.
  • Ensure that the device for the target on which the shared variable resides is functional and supports front panel data binding. LabVIEW modules and toolkits you have installed might not support front panel data binding.

Reading and Writing Shared Variable Values on the Block Diagram

A Shared Variable node is a block diagram object that points to the corresponding shared variable in the Project Explorer window. Use a Shared Variable node to read and write the value of the shared variable and to read the timestamp for the shared variable data. Drag a shared variable from the Project Explorer window onto the block diagram of a VI in the same project to create a Shared Variable node.

You also can place a Shared Variable node on the block diagram from the Data Communication palette. To bind a Shared Variable node on the block diagram to a shared variable in the active project, double-click the Shared Variable node to display the Select Variable dialog box. You also can right-click the Shared Variable node and select Select Variable from the shortcut menu. Navigate to a shared variable in the Shared Variable list in the Select Variable dialog box and click the OK button.

By default, the Shared Variable node is set to read. To change the Shared Variable node on the block diagram to write, right-click the Shared Variable node and select Change to Write from the shortcut menu. The Shared Variable node in the following figure is set to write.

Use the timestamp indicator to determine if the shared variable value is stale, or if it has been updated since the last read. To record a timestamp value for a single-process shared variable, you must first place a checkmark in the Enable timestamp checkbox on the Variable page of the Shared Variable Properties dialog box. Add a timestamp indicator to a Shared Variable node by right-clicking the Shared Variable node on the block diagram and selecting Show Timestamp from the shortcut menu. If your application requires more than one recent value, configure buffering.

The Shared Variable node uses the configuration you set for the shared variable in the Shared Variable Properties dialog box. When you update the shared variable configuration, LabVIEW updates the Shared Variable node. For example, if you change the Data Type of the shared variable, LabVIEW updates the Shared Variable node to the new data type in the block diagram.

Specifying Shared Variable Node Types

To specify how a Shared Variable node connects to a shared variable, you can set the node to absolute or target-relative.

An absolute Shared Variable node always connects to the shared variable on the target on which you created the shared variable. Shared Variable nodes are absolute by default. If a VI contains an absolute Shared Variable node and you open the VI on a new target, the node attempts to connect to the shared variable on the target on which you created the shared variable. The VI appears with a broken Run button.

A target-relative Shared Variable node always connects to the shared variable on the target on which you run the VI that contains the Shared Variable node. If you move a VI that contains a target-relative Shared Variable node to a new target, you also must move the shared variable to the new target. Use target-relative Shared Variable nodes when you expect to move VIs and variables to other targets.

To change an absolute Shared Variable node to target-relative, right-click the Shared Variable node on the block diagram and select Change to Target Relative from the shortcut menu. Target-relative Shared Variable nodes appear with a double-arrow glyph on the node. The following image shows a target-relative Shared Variable node.

To change a target-relative Shared Variable node to absolute, right-click the Shared Variable node and select Change to Absolute from the shortcut menu. The following image shows an absolute Shared Variable node.

You also can use the DataSocket VI and functions to share shared variable data programmatically using NI-PSP.


Resources


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit