Remotely Monitoring I/O with CompactRIO

Updated Apr 11, 2023

Environment

Hardware

  • CompactRIO Chassis

Software

  • LabVIEW
  • LabVIEW Real-Time Module
  • LabVIEW FPGA Module

Driver

  • NI CompactRIO

This tutorial walks you through steps to remotely monitor and control I/O in a CompactRIO application.

Starting a New CompactRIO Project in LabVIEW

Begin by creating a new project in LabVIEW, where you will manage your code and hardware resources.

  1. Create a new project in LabVIEW by selecting File » New Project. The default project tree includes My Computer, which is where you can write code that runs on the Windows machine you’re currently developing on. Remember that a real-time target has a processor running a real-time OS, so it’s effectively another computer.
  2. To add your CompactRIO system to the project, right-click on the Project item at the top of the tree and select New » Targets and Devices…
 
  1. This dialog allows you to discover existing systems on your network or add offline systems. Expand the Real-Time CompactRIO folder, select your system, and click OK

Note: If your system is not listed, LabVIEW could not detect it on the network. Ensure that your system is properly configured with a valid IP address in NI MAX. If your system is on a remote subnet, you can also select to manually enter the IP address.

If you don't have hardware yet but want to start programming, Select New target or device, then expand Real-Time CompactRIO, and pick a model with specifications to fit your requirements.


 

Select the Appropriate Programming Mode

To select the appropriate programming model, it is important to understand how the data is transported from the physical input and output ports on the target to the processing components. When using a CompactRIO there are two ways to access I/O, and when using a CompactRIO with NI-DAQmx, there are three ways to access I/O:

  • For Real-Time (NI-DAQmx) mode, the data is read through the processor via NI-DAQmx VI’s.
  • For Real-Time Scan (IO Variable) mode, the data is sent through the FPGA, but is ultimately accessed on the processor by dragging and dropping IO Nodes to the Real-Time VI.
  • For LabVIEW FPGA mode, the I/O is read from directly from within the FPGA by dragging and dropping IO Nodes to the FPGA VI.

The programming mode is indicated by the text next to the module in the LabVIEW project.

Real-Time (NI-DAQmx) Mode – CompactRIO with NI-DAQmx is the latest addition to the CompactRIO controller family. It brings two software experiences into one by combining the ease of use of NI-DAQmx and the low-level functionality of LabVIEW FPGA. It also simplifies system architectures by bringing the latest in synchronization and control technologies to the CompactRIO platform. To program a C Series module in this mode, place it under the Real-Time Resources folder in the LabVIEW project.

Real-Time Scan (I/O Variable) Mode – this option allows you to program the real-time processor of your CompactRIO system, but not the FPGA. In this mode, NI provides a pre-defined personality for the FPGA that periodically scans the I/O and places it in a memory map, making it available to LabVIEW Real-Time. CompactRIO Real-Time Scan Mode is sufficient for applications that require single-point access to I/O at rates of a few hundred hertz. To program a C Series module in this mode, place it under the Real-Time Scan Resources folder in the LabVIEW project. To learn more about scan mode, read the Using CompactRIO Scan Mode with NI LabVIEW  white paper and view the benchmarks.

LabVIEW FPGA Interface Mode – this option allows you to unlock the real power of CompactRIO by customizing the FPGA personality in addition to programming the real-time processor, achieving performance that would typically require custom hardware. Using LabVIEW FPGA, you can implement custom timing and triggering, off-load signal processing and analysis, create custom protocols, and access I/O at its maximum rate. To program a C Series module in this mode, place it under the FPGA target in the LabVIEW project.

LabVIEW will now attempt to detect the chassis and C Series I/O modules present in your system and automatically add them to the LabVIEW Project. Once your system has been added to the LabVIEW Project, proceed to either the Real-Time Scan Mode Tutorial or LabVIEW FPGA Tutorial below, depending on which programming mode you selected.

CompactRIO Real-Time Scan Mode Tutorial

This section will walk you through creating a basic monitoring application on CompactRIO using scan mode. If you chose to use the LabVIEW FPGA Interface, see the LabVIEW FPGA Tutorial below. You should now have a new LabVIEW Project that contains your CompactRIO system, including the controller, chassis, and C Series I/O modules. In this tutorial we will be using an NI 9211 Thermocouple input module; however, the process can be followed for any analog input module. You can also download the solution on this page

  1. Save the project by selecting File»Save and entering Basic logging with scan mode. Click OK.
  2. This project will only contain two VIs. First, you will create the real-time VI, which is the LabVIEW Real-Time application that runs embedded on the CompactRIO controller. Create this the VI by right-clicking on the CompactRIO real-time controller in the project and selecting New»VI. Save the VI as RT.vi.

 
  1. The basic operation of this application will include initialization and then two parallel tasks. A flat sequence structure is an easy way to enforce that initialization happens first in dataflow. Place a flat sequence structure with one frames on your RT.vi block diagram.
  2. Now add a timed loop to the right of the sequence structure. Timed loops provide the ability to deterministically synchronize the code inside to various time basis, including the NI Scan Engine that reads and writes scan mode I/O.
Drag with your mouse the rectangle where you want the loop to appear, then release.
  1. To configure the timed loop, double-click on the clock icon on the left input node.


 
  1. Select Synchronize to Scan Engine as the Loop Timing Source. Click OK. This will cause the code in the timed loop to execute once, immediately after each I/O scan, ensuring that any I/O values used in this timed loop are the most recent values.
 
  1. The previous step configured the timed loop to run synchronized to the scan engine. Now configure the rate of the scan engine itself by right-clicking on the CompactRIO real-time controller in the LabVIEW Project and selecting Properties.
  1. Select Scan Engine from the categories on the left and enter 100ms as the Scan Period. This will cause all the I/O in the CompactRIO system to be updated every 100ms (10Hz). The Network Publishing Period can also be set from this page, which controls how often the I/O values are published to the network for remote monitoring and debugging. Click OK.

 
  1. Now that you have configured the I/O scan rate, it is time to add the I/O reads to your application for monitoring. When using CompactRIO Scan Mode, you can simply drag and drop the I/O variables from the LabVIEW Project to the block diagram. Expand the CompactRIO real-time controller, chassis, and the I/O module(s) you would like to access. Select all the channels below the module by clicking on them and using the shift key, then drag and drop them into the timed loop on your RT.vi diagram as shown below.

 
Tip: Use the Align Objects » Left Edges and Distribute Objects » Vertical Compress items on the LabVIEW toolbar to organize the I/O variables on your diagram.
                
 
  1. In addition to simply monitoring the I/O values remotely, we will add some analysis that runs onboard the CompactRIO controller. Place four Mean PtByPt VIs in the timed loop and wire the I/O variables to their inputs. Create a constant with a value of 100 for the sample length input. Wire the results of the mean functions to a Build Array function.
  1. The next logical step would be to write the data to the network using a shared variable; however, because network communication takes an undermined amount of time, it is necessary to separate the I/O acquisition task and the network communication. Neglecting this requirement could lead to losing data, since writing a data packet to the network may take longer than the I/O scan, causing a sample to be missed. Place a normal while loop in the Run frame under the timed loop, which will be used for the network communication.
In order write the data to the network in the regular while loop, you need to transfer the latest result of the mean calculation from the timed loop using a real-time FIFO. This will provide a buffer between the two loops. The timed loop will run, synchronized the I/O scan, and write the latest result of the mean calculation to the buffer each time. Then, the regular while loop will read the data out of the buffer and write it to the network. Separating the I/O task and network communication in this way allows your timed loop to run with “real-time” performance, meaning that it will always finish on time.
 
  1. In the LabVIEW Project, right-click on the CompactRIO real-time controller and select New»Variable.
  1. Name the variable Data buffer, select Single Process as the Variable Type, and Array of Double as the Data Type. This will create a locally scoped variable (no network publishing) that contains and array of double precision floating point numbers. Then select RT FIFO from the menu on the left.
  1. With the RT FIFO category selected, select the Enable RT FIFO check box, select Single Element for the FIFO Type, and enter 4 for the Number of elements (if you are monitoring several channels other than 4, enter that instead.) This configures the variable to operate as a real-time-safe global variable, which can serve as the data buffer between our real-time and low priority tasks. The variable will hold one array that contains four double precision numbers. Therefore, we are only keeping track of the latest result of the mean calculation, not every result calculated. Click OK.
  1. Place a copy of the Data buffer variable in the timed loop and one in the regular while loop.
  2. Create another variable to perform the network communication. Use the following configuration:
  • Name: Temp Averages
  • Data Type: Array of Double
  • Variable Type: Network-Published
  1. Place the Temp Averages variable in the regular while loop and wire the output of the Data buffer variable to its input.
  2. Also, place a Waveform Chart on the front panel. Use an Array to Cluster function to format the data for the chart. Right-click on the Array to Cluster function and select Cluster Size... Enter for the size, since there are four elements in the array. Wire the diagram as shown.
  1. To control the stop condition of the while loops, create a shared variable with the following properties:          
  • Name: stop
  • Variable Type: Single Process
  • Data Type: Boolean
  • RT FIFO: Single Element
  1. Place the stop variable in each loop and create a Boolean stop button on the front panel and place it in the regular while loop.
  2. To complete the real-time application, place a copy of each variable in the startup frame with default values wired to them as shown. Also place a Wait (ms) function in the regular while loop with a value of 100 to time the network publishing.
  1. Click the Run arrow on RT.vi, click Save for any unsaved items, and click OK on any dialogs or warnings about applying changes to the CompactRIO system. LabVIEW will now deploy your VI over Ethernet to run embedded on the CompactRIO system.
  2. Once the VI deploys and begins running, view the front panel of your VI to see the current I/O values plotted on the waveform chart. Stop the VI once you verify it is working.

Now, we will create a host VI that can run on a remote machine and communicate with this embedded application.

  1. Create a new VI under the My Computer item in the LabVIEW Project. Save the VI as HMI.vi.
  1. Create a simple VI that reads from the I/O variables and plots them on a waveform chart, using a Build Array function and an Array to Cluster function, configured with a Cluster Size of 4. Note that I/O variables can be used directly on the Windows host VI, since they are automatically network published by the NI Scan Engine.
  2. Use an Index Array function to unpack the Temp Averages and place them on indicators on the front panel. The completed diagram might look like as shown below. Also, add timing to the loop with a Wait (ms) function.
  1. You have completed this application and can now run it. Run RT.vi first, so that it is running on the CompactRIO controller. Then run HMI.vi, which will connect to the shared variables that are hosted on the CompactRIO controller and display the data. 

The host VI in this example could be placed on any computer on the network to monitor the embedded application running on CompactRIO.

Congratulations! You have successfully created an embedded monitoring application with LabVIEW and CompactRIO. To continue learning, check out the additional resources at Getting Started with LabVIEW FPGA .

LabVIEW FPGA Tutorial

This section will walk you through creating a basic high-speed remote monitoring application on CompactRIO using LabVIEW FPGA. You should now have a new LabVIEW Project that contains your CompactRIO system, including the controller, chassis, FPGA, and C Series I/O modules. In this tutorial we will be using an NI 9205 analog input module; however, the process can be followed for any analog input module. You can also download the solution from this page.

  1. Save the project by selecting File»Save and entering Basic logging with LabVIEW FPGA for the name. Click OK.
  2. In the LabVIEW Project, expand the CompactRIO real-time controller and chassis to find the FPGA item. Right-click on the FPGA item and select New»VI. This VI will perform the high-speed analog acquisition.

 

  1. Save the VI as FPGA.vi
  2. To add an I/O node that will acquire a sample from the analog input module, expand the folder named Mod1 and drag Mod1/AI0 to the FPGA VI block diagram.
  1. Expand the I/O node by clicking and dragging downward as shown, so that channels AI0 through AI3 are visible. This will cause the I/O node to sample all four channels each time it executes.
  1. Create indicators for each of the analog input channels.
  2. Place four instances of the DC and RMS Measurements VI on the diagram and configure them for a DC measurement, calculated every 500ms at an acquisition rate of 2kS/s. This configures the VI to calculate the average value of the input samples each time a data set of 1000 samples are acquired.
 
  1. The DC and RMS Measurement function will return a True value for the output valid? output each time an average is calculated and returned (when a data set of 1000 samples has been acquired). Place four Case Structures on the diagram, create indicators for each DC result output, and use the output valid? output to control the case structures as shown. This will cause the indicators to only be updated when a valid result is available.
 
  1. Enclose the code written so far in a Flat Sequence structure and add one frame after the code. Place a Loop Timer VI in the second frame and configure it for uSec timing. Wire a constant value of 500 to the Loop Timer. Finally, place a While Loop around the flat sequence structure and create False constant for its stop condition.

 
  1. You have completed the FPGA VI, which needs to be compiled into a bit stream that will be downloaded onto the FPGA. To begin the compilation process, click the Run Arrow which will prompt you to save the VI and then select your compile server. NI recommends setting up a free LabVIEW FPGA Compile Cloud Service account, to free up more space on your development machine. If you have the Xilinx Compile tools installed locally or on a machine on your network, you can select one of the other options.

 
  1. Once LabVIEW finishes generating intermediate files, you can minimize the compilation process and begin writing the LabVIEW Real-Time VI. In the LabVIEW Project, right-click on the CompactRIO controller and select New»VI.

This VI will run on the real-time controller and communicate with the network to publish the acquired data.

  1. Save the VI as RT.vi
  2. From the FPGA Interface palette, place an Open FPGA VI Reference on the RT.vi block diagram and double-click on it. Click the browse button and select FPGA.vi. This function will download and run the FPGA VI when it executes. You can then interact with the FPGA VI from your real-time VI using the reference returned from this function. Click OK.
 
  1. Place a While Loop on the diagram and a 100ms wait and a stop button. From the FPGA Interface functions palette, select Read/Write Control and place it on the diagram. Wire the outputs of the Open FPGA VI Reference to the inputs of the Read/Write Control.
  2. Left-click on Unselected on the Read/Write Control and select AI0. Expand the Read/Write Control downward to show AI0 through AI3 and AI0 Average through AI3 Average as shown below. These are indicators from the FPGA VI that provide single-point data updates.
 
  1. Now we will create a network-published shared variable to send the data over the network to a host VI running on your PC. In the LabVIEW Project, right-click on the CompactRIO real-time controller and select New»Variable.

 
  1. Configure the variable with the following configuration:
  • Name: Analog Values
  • Variable Type: Network-Published
  • Data Type: Array of Double   

 
  1. Create a second variable with the following configuration:
  • Name: Analog Averages
  • Variable Type: Network-Published
  • Data Type: Array of Double
  1. Use the Build Array and To Double Precision Float functions to wire the values from the Read/Write Control into the network-published shared variables you created.
  2. Place a Waveform Chart on the front panel and use an Array To Cluster function on the diagram to connect the analog input values to the chart as shown. Also, right-click on the Array To Cluster function and select Cluster Size... Set it to 4, since there are four elements.
  3. Finish the real-time VI by placing a Close FPGA VI Reference VI outside the while loop and wiring it.

 

Now, we will create a host VI that can run on your development PC or a remote machine and communicate with this CompactRIO application.

  1. Create a new VI under the My Computer item in the LabVIEW Project. Save the VI as HMI.vi.

 
  1. Create a simple VI that reads from the network-published variables and plots them on a waveform chart or displays the values with numeric indicators. Use an Array to Cluster function, configured with a Cluster Size of 4, to put the multi-channel data on a waveform chart. 
  2. Use an Index Array function to unpack the Analog Averages and place them on indicators on the front panel. The completed diagram might look like the one shown. Also, add timing to the loop with a Wait (ms) function.

 
  1. You have completed this application and can now run it. Run RT.vi first, so that it is running on the CompactRIO controller. Then run HMI.vi, which will connect to the shared variables that are hosted on the CompactRIO controller and display the data. 

The host VI in this example could be placed on any computer on the network to monitor the embedded application running on CompactRIO.