To download NI software, including the products shown below, visit ni.com/downloads.
This article discusses how to discover a local or remote OPC Server, connect to that OPC Server with an OPC Client, and create shared variables bound to the OPC Server Tags at application run-time using LabVIEW and the LabVIEW Datalogging and Supervisory Control (DSC) Module.
1.Download the reference example and unzip it to an empty directory.
2.Open the example named OPC Client Connection and Variable Creation_Main VI.
LabVIEW 2009 Datalogging and Supervisory Control Module
An OPC Server is a standard way to centralize data from multiple and/or different devices on a network. Applications needing this centralized data can connect to the OPC Server with an OPC client. Some systems are static and this connection can be established and deployed once. A common example of this would be a manufacture plant floor. Multiple devices, typically PLCs or PACs, are distributed through out the plant and networked together. A central computer is also on the network, which contains the OPC Server. This OPC Server receives data from all the devices and publishes that data to any connected OPC Clients. The plant could have multiple HMI devices with OPC Clients that display the entire plant status in multiple locations throughout the facility. To see how to create a static OPC Client, please refer to Connect LabVIEW to Any PLC Using OPC (http://zone.ni.com/devzone/cda/tut/p/id/7450).
Some systems however, are more dynamic and need to establish a connection to the OPC Server at application run-time. How to accomplish this using LabVIEW and the LabVIEW DSC Module is discussed below.
Connecting to an OPC Server with LabVIEW is accomplished through the shared variable engine. The shared variable engine is a separate program from LabVIEW named tagger.exe. Items, such as shared variables or IO Servers are created and deployed inside of the shared variable engine through various APIs and commands available in LabVIEW. These items are contained inside of processes, which is typically known as a libraries. Once a process is created and deployed in the shared variable engine, its items are published using the NI Publish and Subscribe Protocol (NI-PSP). This is a network protocol designed for shared variable.
This reference example will create an IO Server item of type OPC Client inside of a process in the shared variable engine. Once this is accomplished, the OPC Server tags will be available for reading and writing. In some applications this may be all that is required. For most applications, the additional features provided by shared variable items (such as data logging or alarming) is still necessary. Therefore, this examples also creates shared variables bound to the OPC Server tags and deploys them into the same process as the OPC Client. Please see the picture below for a visual description of how the processes and items created by this example will exist inside of the shared variable engine.
Relationships Inside of the Shared Variable Engine
For more information on the shared variable engine and PSP, please refer to the document Using the LabVIEW Shared Variable.
This example named OPC Client Connection and Variable Creation_Main VI has two subVIs. The first is Create OPC Client Connection and Variables_Main VI, which is responsible for discovering the OPC Servers, creating the OPC Client, and creating the bound variables. The second is User Interface_Main and it provides a simple user interface for viewing the newly created shared variables.
The basic flow of Create OPC Client Connection and Variables_Main VI is as follows:
Each action is paired with an individual VI (discussed below). Create OPC Client Connection and Variables_Main VI has two inputs of interest, Machine and OPC Server. If the Machine input contains text, the search and selection of a machine is skipped with the text provided being used as the machine name. The OPC Server input functions in the same fashion.
Create OPC Client Connection and Variables_Main VI Block Diagram
The discovery and selection of a networked machine is accomplished using a VI named opc_BrowseMachine. This VI is installed with the LabVIEW DSC Module and is therefore locked (password protected). It is not on the functions palette and therefore could change without notice when upgrading LabVIEW.
opc_BrowseMachine VI Front Panel
The discovery and selection of the desired OPC Server occurs in the Select OPC Server VI. This VI wraps a user interface around the VI GetOPCServerList, which discovers the OPC servers on the selected machine. GetOPCServerList VI is installed with LabVIEW DSC and is therefore locked. It is not on the functions palette and is subject to change when upgrading.
Note: Make sure your DCOM settings are correct as this can lead to your OPC Server not being discovered.
Select OPC Server VI Front Panel
Once the machine and OPC Server are selected, a process containing an IO Server item of OPC Client can be created and deployed in the shared variable engine. Once deployed, the OPC Client will connect to the selected OPC Server on the selected machine and publish all OPC Server tags locally as PSP items. The Create Process and OPC Client Connection VI accomplishes this using two VIs. The Create Process VI creates and deploys a new process in the shared variable engine and the Create and Configure IO Server Express VI creates and deploys an IO Server item of type OPC Client in the shared variable engine.
Create Process and OPC Client Connection VI Block Diagram
As all the OPC Server tags are now published locally as PSP items, they can be discovered. The Search Local Processes for OPC Client Connection Tags VI accomplishes this using shared variable engine property nodes.
Search Local Processes for OPC Client Connection Tags VI Block Diagram
After obtaining the OPC Server tag list, the shared variable engine property nodes can be used to get the tag attributes. These attributes are used in conjunction with the LabVIEW DSC Engine Control functions to create bound shared variables. To speed up the creation process, the variables are created and buffered (but not deployed) in memory. This functionality is accomplished in the Create Shared Variables from List VI.
Create Shared Variables from List VI Block Diagram
Last, the newly created shared variables are deployed using the Commit Shared Variables VI.
A simple example user interface (UI) is provided to show that the shared variables were created and deployed.
User Interface_Main VI Front Panel
The shared variables can be bound to the front panel indicators by right clicking on the desired indicator and selecting Change Binding. This will bring up a drop down menu which will allow the operator to select the OPC Server tag of interest.
Select Shared Variable for Binding VI Front Panel
Note: There is an indicator per data type. The operator can only choose an OPC Server Tag of the same data type as the indicator selected.
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.