RIO Developer Essentials Guide for Academia
RT PC code example
Programmatically access a network-published shared variable (NPSV)
Use the NPSV programmatic API (application programmer's interface) VIs as an alternative method to a shared variable node.
NOTE: Refer to the NPSV code example page for a detailed introduction to NPSVs; the sample code presented here is a modified version of that example.
- Host PC controls the RT application and monitors its status via a network connection
- Multiple PCs require access to variables hosted on a single RT target
- Efficiently work with multiple network-published shared variables (NPSVs); an alternative to the shared variable node approach to access NPSVs
- Open a reference to a NPSV and then use the “Read Variable” and “Write Variable” VIs to access the NPSV by reference instead of by shared variable nodes
LabVIEW block diagram elements
Locate these elements with "Quick Drop" (press Ctrl+Space and start typing the name); click on an icon to see more sample code that uses that element:
- Connect your Academic RIO Device to your PC using USBLAN, Ethernet, or Wi-Fi. NOTE: Not all Academic RIO Devices have Ethernet and Wi-Fi connectivity options.
Download and unpack the
rt-pc_npsv-programmatic-api.zip (for use with NI myRIO 1900)
NIELVISIII-rt-pc_npsv-programmatic-api.zip (for use with NI ELVIS III)
archive, and then double-click the ".lvproj" file to open the project. NOTE: This project was written for a NI myRIO 1900 or NI ELVIS III connected by USBLAN at IP address 172.22.11.2.
If you are using a different IP address or another Academic RIO Device (Example: NI myRIO 1950 or NI RIO Control Module) do the following:
- If using the NI myRIO 1950 or NI RIO Control Module start with the NI myRIO 1900 Archive.
- Different IP address: Right-click on the "NI myRIO 1900" Device, choose "Properties", and then enter the new IP address
- Different device:
- Right-click on the top of the project hierarchy, select "New Targets and Devices", keep the "Existing target or device" option, and then find and select your particular device
- Select all of the components under the "NI myRIO 1900" device: click the first one and then shift+click the last one
- Drag the selected components to the new device
- Right-click the "NI myRIO 1900" device and select "Remove from project"
- Run the “RT Main” VI:
- This is a “headless” VI – once started, the PC host monitors and controls the VI, including initiating a controlled stop
- The VI contains one non-deterministic loop and one deterministic loop
- Polls the “LED0” NPSV every 100ms and sets the states of LEDs 0 and 3
- Writes the “Button” NPSV according to the state of the Academic RIO Device onboard pushbutton
- Reads the onboard accelerometer Z-axis every 10ms in the deterministic loop, and writes the “Accelerometer-Z” NPSV
- Run the “PC Main” VI:
- Writes the “LED0” NPSV every 10ms according to the LED0 front-panel button
- Reads the “Button” NPSV and displays its state
- Reads the “Accelerometer-Z” NPSV and displays its value as a chart; tip the Device to see changes
- Review overall structure:
- “PC Main” VI
- Opens connections to network-published shared variables (NPSVs) hosted on RT
- Reads NPSVs: accelerometer Z-axis and onboard button state
- Writes NPSVs: “LED0” and “Stop”
- Loop stops on click of the “Stop” button or error condition
- Closes connections to NPSVs as last step
- “RT Main” VI
- Two process loops
- Non-deterministic (conventional) loop handles most of the network interaction with “PC Main” VI with NPSVs
- Deterministic (timed) loop reads accelerometer precision 100-Hz rate; NPSV in this loop must have “RT FIFO” option enabled to minimize jitter
- Single-process shared-variable with FIFO enabled links the two loops without disrupting the deterministic loop
- Locate the function subpalette
- Specify a shared variable