Using the LabVIEW Network-Published Shared Variable and OPC With NI-DAQ™mx

Updated Jul 27, 2023



  • OPC Servers
  • LabVIEW


  • NI-DAQmx

NI LabVIEW simplifies the creation of distributed data acquisition and control applications. All NI-DAQmx devices are available for use with the LabVIEW Shared Variable Engine through NI-DAQmx. In addition to compatibility with LabVIEW, NI-DAQmx also supports the connection of NI data acquisition (DAQ) devices to enterprise industrial systems using OPC.

What is the Shared Variable Engine?

The Shared Variable is a simplified programming interface for sharing data that was introduced in LabVIEW 8. Using the network-published Shared Variable, you easily can pass data within a system and between systems.

Once a Shared Variable has been created using the Network-Published scope, all data written to the variable is available to all systems networked with the host computer. Shared Variables are configured through dialogs accessable from the Project Explorer window. You can also add data services to Shared Variables such as logging, alarming, and events, through dialogs using the LabVIEW Datalogging and Supervisory Control Module. 

The Shared Variable Engine manages the use, connectivity, and performance of all Shared Variables on your many data acquisition and control systems, and it does this using the NI Publish-Subscribe Protocol (NI-PSP). An in-depth discussion of the Shared Variable Engine can be found in the tutorial Buffered Network-Published Shared Variables: Components and Architecture, while more information about NI-PSP can be found in the LabVIEW Help: Understanding Shared Variable Technology. For more information on Shared Variable configuration, reference the tutorial Using the LabVIEW Shared Variable.

OPC and the Shared Variable Engine

OPC, originally an abbreviation for Object Linking and Embedding for Process Control, is a standard first developed in 1996 by the OPC Foundation. The standard specified the communication of plant data between control devices from different manufacturers. More information about OPC can be found in the tutorial What is OPC?

Figure 1. OPC is intended to improve enterprise system connectivity.

The NI Shared Variable Engine includes an OPC Server so that Shared Variable data can be made available to OPC client applications. The Shared Variable Engine publishes all variables as OPC tags under the Prog ID National Instruments.Variable Engine.1. The Shared Variable Engine can use the OPC Data Access 2.0 or 3.0 standard, allowing any device compatible with those OPC DA standards to access the data stored in a Network-Published Shared Variable.

Using OPC Connectivity with NI-DAQmx 

In addition to its role as the primary device driver for NI data acquisition devices, NI-DAQmx uses NI-PSP to interact with the Shared Variable Engine and make data read by the driver available to all networked devices. NI-DAQmx automatically produces PSP objects for all NI-DAQmx global virtual channels configured in MAX and all NI-DAQmx local channels created in LabVIEW, though it does not publish data from NI-DAQmx tasks.

As discussed previously, the Shared Variable Engine publishes all Shared Variables as OPC tags; therefore, the Shared Variable Engine makes it possible for an OPC DA client to read and write data to an NI-DAQmx device.

Figure 2. NI-DAQmx works with the Shared Variable Engine to make DAQ hardware available for OPC connectivity.

This functionality makes it easy to perform data acquisition or generation using LabVIEW, the Distributed System Manager, or a third-party OPC client. To access an NI-DAQmx channel via NI-PSP, you must first bind the channel to a Shared Variable. This will be shown later in the tutorial.

Figure 3. A simple data acquisition task using a Shared Variable bound to an NI-DAQmx input channel.

The NI Distributed System Manager can be used to view NI-DAQmx channels, as well as any Shared Variables which are bound to those channels. However, it can not log that data without using the LabVIEW DSC module and a Shared Variable bound to the NI-DAQmx channel. For a description of how to log data to the NI Citadel 5 database, see the tutorial Logging Data with NI Citadel.

Figure 4. A view of the NI-DAQmx channel, TTL Input, and bound Shared Variable, TTL Channel, in the Distributed System Manager.

As previously discussed, an OPC client can access the data published by NI-DAQmx channels and the Shared Variable Engine. OPC Quick Client, shown below, demonstrates this use case. The Quick Client is included with the NI LabVIEW Datalogging and Supervisory Control Module.

Figure 5. The NI-DAQmx channel and bound Shared Variable in the OPC Quick Client

The steps required to view a Shared Variable in the OPC Quick Client can be found below.

How do I bind a Shared Variable to a NI-DAQmx Virtual Channel?

A Shared Variable can be bound to both local (project) and global (MAX) NI-DAQmx Virtual Channels. The scope of the channel determines its behavior. Project NI-DAQmx Channels only broadcast data when the Shared Variable to which they are bound is deployed. That is, NI-DAQmx Channels created in a project must be bound in order to be used. Global NI-DAQmx Virtual Channels, on the other hand, constantly output data.

Create a Project Virtual Channel

  1. In the Project Explorer Window, right-click My Computer.
  2. Select New»NI-DAQmx Channel to open the wizard.


Create a Project Variable

  1. In the Project Explorer Window, right-click My Computer.
  2. Select New»Variable
  1. Bind the Project Variable to the Project Channel
  2. Select Enable Aliasing, choose Project Variable from the drop-down box, select the desired access type, and click Browse. Because Project Variable was selected as the type, the Shared Variable Engine looks only for local variables and NI-DAQmx Virtual Channels.
note: In older versions of LabVIEW, Bind to Source should be selected.
  1. Select the channel created above under My Computer


Create a Global Virtual Channel

  1. Open Measurement & Automation Explorer (MAX) from Programs»National Instruments
  2. Right-click Data Neighborhood and select Create New
  3. Select NI-DAQmx Global Virtual Channel to open the wizard

Create a Project Variable from a MAX Channel

  1. In the Project Explorer Window, right-click My Computer
  2. Select New»Variable

  1. Select Enable Aliasing, choose PSP URL from the drop-down box, select the desired access type, and click Browse
Selecting PSP URL indicates to the Shared Variable Engine that it should look for variables on all known networked locations, rather than simply looking at the local project.
  1. Select the NI-DAQmx channel created above under YourComputerName»System»DAQmx


Using the Shared Variables in a LabVIEW VI

Drag the variables from the Project Explorer windows into the block diagram. In the example below, the Shared Variables will be read with each iteration of the loop. When you are using a Network-Published Shared Variable, you also have the option to create a Shared Variable for the polling rate. The server polling rate determines how often the Shared Variable gets new data from its bound global channel.


Accessing Shared Variables from the OPC Quick Client

To access Shared Variables from the OPC Quick Client that ships with the LabVIEW DSC Module, you must locate the Shared Variable Engine, and then create items for the tags associated with each Shared Variable. 

  1. Load the OPC Quick Client from Start»All Programs»National Instruments»NI OPC Servers.
  2. Press the New Server button located on the task bar.
  1. Under Local Machine, select National Instruments.Variable Engine.1 and press OK.
  1. Select New Group, choose a name and update rate, and press OK.
  1. Select New Item from the task bar
  1. Under National Instruments.Variable Engine.1, you can navigate to your Shared Variable, located under the name of your project.

  1. Alternatively, you can access your NI-DAQmx Channel directly by navigating to System»DAQmx, which is also the location of the Server Polling Rate variable.
  1. Double click on the desired variable, and press OK to add it to the OPC Quick Client. Adding the variables above gives the following result: