This content is not available in your preferred language.

The content is shown in another available language. Your browser may include features that can help translate the text.

Using Network Published Shared Variables to Communicate Between Systems

Updated May 4, 2023

Environment

Hardware

  • CompactRIO Controller

Software

  • LabVIEW

LabVIEW can communicate information between systems using Network Shared Variables. By following the steps of this article you will be able to create a Network Shared Variable and deploy it to communicate information between a host and client machine. 

Review the Using the LabVIEW Shared Variable article to better understand the concepts of LabVIEW shared variables and the structure behind. 

Create a LabVIEW Network Shared Variable

For this setup, you can use two computers to communicate the data to each other, or you could use one computer and a controller such as a CompactRIO (cRIO). You need to chose one computer to act as a server and host the variables, with another computer acting as a client. Then, follow the steps below to setup the server and client machines.

Note: If using a cRIO, it can be used as either the server machine hosting the variables, or the client, depending on your application.

On the Server Machine:
  1. Create a LabVIEW Project and add a variable by right-clicking My Computer and selecting New » Variable. Make sure the Variable Type is set to Network Published.
  1. Create a new VI and write or read the Network Published Shared variable normally, as explained in Using the LabVIEW Shared Variable. The server project should look similar to this.
On the Client Machine:
Create a new LabVIEW Project, add a New VI and use the functions of the Shared Variable API to communicate with the Shared Variable hosted in the remote server. The Shared Variable API can be found in the Functions Palette under Data Communication.
  1. Use an ​Open Variable Connection function with a String or Shared Variable Refnum constant or control to specify the Network Published Shared Variable URL as ni.var.psp://<machine_name_or_IP_address>/<library>/<variable>. The Shared Variable Refnum constant or control can be created by right-clicking the shared variale refnum in input of any of the Shared Variable API functions and selecting Create » Constant/Control.
  2. Write or read the variables as required using the Write Variable or Read Variable functions.
  3. Use a Close Variable Connection function to close the reference to the Network Published Shared Variable. The code should look similar to this:

Additional Information

The Shared Variable API, available since LabVIEW 2009, is the recommended method to communicate between Windows systems. However, there is an alternative method binding the network published shared variables to front panel objects, which will make controls or indicators update automatically if there are any changes in the bound variables. The data binding method is not recommended because it obscures the functionality of the front panel objects; since the controls or indicators are not getting their values from a wire, it degrades the readability of the code.

For additional information, refer to the documents in the Related Links and to the Shared Variable.lvproj shipping example. To locate the shipping example, launch LabVIEW and click Help » Find Examples.. to open the Example Finder, and once it loads, select Browse (according to Task) and expand Networking » Shared Variable.

If you are using the above method to read Network Shared Variables on a Client Machine, ensure you are only opening the reference only once. Putting the open VI in a loop will open up a new instance each time the loop is run, and will cause LabVIEW to hang or crash with open references.