Accessing Shared Variables From a LabVIEW Web Service

Updated Sep 15, 2023

Environment

Software

  • LabVIEW

This article demonstrates how to read and write Network Published Shared Variables (NPSVs) from a LabVIEW Web Service using HTTP Methods. The steps shown throughout this article allow you to access the value of a Shared Variable through a URL.



Prerequisites

  • The host PC must have installed:
    • LabVIEW 
    • NI System Web Server.
    • NI Web Server.
    • NI Web-Based Configuration and Monitoring.
  • If communicating with a Real-Time target, the target must have installed:
    • LabVIEW Real-Time (the version must match the version of the LabVIEW Development Environment).
    • Application Web Server.
    • I/O Variable Remote Configuration Web Service.
    • Network Variable Engine.
    • NI System Configuration Remote Support.
    • NI Web-Based Configuration and Monitoring.
    • Run-Time Engine for Web Services.
    • Variable Client Support for LabVIEW RT.
    • Variable Web Service.
    • Note: refer to Adding or Removing Software on Linux RT Targets for installation instructions.

The steps below describe how to develop a Web Service on a host PC that reads and writes NPSVs. The same steps can be applied if using a Real-Time target.

1. Create a new LabVIEW project containing a Web Service.
  1. Launch LabVIEW and click File >> Create Project >> Blank Project. Save the project as "Shared Variable Web Service.lvproj".
  2. Right-click My Computer and select New >> Web Service. Name the Web Service " WebServiceTest".

WS Project Hierarchy.PNG

2. Create a Shared Variable Library.
  1. Right-click My Computer and click New >> Variable.
  2. In the Shared Variable Properties window, select the Variable tab. Define a Name and Data Type, and ensure that Variable Type is set to Network-Published. Ensure that Enable Aliasing is unchecked.
  3. The example below shows a Double called Variable1.
  4. Press OK to save changes.
  5. Save the project (if prompted to save new files, click Yes). Save the lvlib file as "Shared Variables.lvlib".

New Shared Variable.PNG

3. Create a Method VI that writes the NPSV.
  1. Expand the LabVIEW Web Service, right-click on Web Resources and select New VI.
  2. Save the new VI as "Write Variable.vi".
  3. Drag the NPSV from the project window onto the VI's Block Diagram.
  4. Right-click NPSV on the Block Diagram and select Access Mode >> Write.
  5. Create a Numeric Control and name it "WriteValue".
  6. Connect the Numeric Control to the NPSV on the Block Diagram.
  7. Connect the Numeric Control to the VI's Connector Pane.
  8. Save the VI.

Connector Pane Write.PNG

Write Variable.png
Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.

4. Create a Method VI that reads the NPSV.
  1. Repeat the steps in step 3, but change the Access Mode to Read. Connect a Numeric Indicator to the NPSV and then link it on the Connector Pane.
  2. Save the VI as "Read Variable.vi".

Connector Pane Read.PNG

Read Variable 2.png
Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.

5. Check the Web Service Properties.
  1. From the project window, right-click the Web Service and select Properties.
  2. In the Web Service Properties window, select the HTTP Method VI Settings tab.
  3. Select a Method VI from the tab and ensure that:
    • In the URL Mapping tab: Method is set to GET, Use standard URL mapping and Include VI name in the URL are checked.
    • In the Output Type tab: Terminal is selected and Output format is set to JSON.
  4. Repeat the previous step for the other Method VI.
  5. Click OK to save changes.
  6. The project hierarchy should now resemble the following image.

VI hierarchy.PNG

6. Publish the Web Service and deploy the NPSV.
  1. Right-click the Web Service and select Application Web Server >> Publish. This hosts the Web Service on the Application Web Server.
  2. Right-click the Shared Variable Library and select Deploy. The Web Service cannot access the NPSV until the library has been deployed. The Shared Variable Library must be redeployed after making any changes to the NPSV.
7. Access the NPSV.
  1. To write the NPSV:
    1. Right-click "Write Variable.vi" and select Show Method URL.
    2. In the HTTP Method URL window, select 8080 - Application from the Available Servers drop-down menu.
    3. Click Copy URL.
    4. In a browser, paste the URL and replace {value} with a value to write to the NPSV. For example, the URL for this project takes the format of http://127.0.0.1:8080/WebServiceTest/Write_Variable?WriteValue=5
  2. To read back the NPSV value:
    1. Right-click "Read Variable.vi" and select Show Method URL.
    2. In the HTTP Method URL window, select 8080 - Application from the Available Servers drop-drop menu.
    3. Click Copy URL.
    4. In a browser, paste the URL. The image below shows that the URL returns a value of "5" when {value} was set to 5.

WS browser.PNG

Download the attachment to run this example in LabVIEW 2022 Q3 or newer.