Monitor and Log Data With LabVIEW Real-Time

Updated Jul 27, 2023

Environment

Software

  • LabVIEW
  • LabVIEW Real-Time Module

This tutorial builds on what is learned in the tutorial Take Your First Measurement in LabVIEW Real-Time (Data Logging).
In this tutorial, we will learn how to locally log data to disk on real-time targets. This is a necessary task for many stand-alone applications. This tutorial will use a CompactRIO as the real-time target, but the methods described apply to all NI real-time targets. 

Review the first tutorial before working through this one because the first tutorial covers many of the fundamental tasks for creating your project. Download the Sample Code .

Adding Data Logging to a LabVIEW Real-Time Project

  1. Open your completed solution from the first tutorial, Take Your First Measurement in LabVIEW Real-Time (Data Logging), or download the .zip file from this page. Double-click to open Real-Time Evaluation.lvproj.
  2. 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. RT.vi is the code that runs on your real-time target. Since you installed the NI-RIO driver with your evaluation software, the target saved in this project is a real-time CompactRIO target, shown as the cRIO-9068 chassis. Note that you communicate with your target by entering its valid IP address once you have properly configured it on the same network as your development computer.
  1. Double-click to open RT.vi and press <Ctrl-E> to open the block diagram. A VI is composed of two windows, the gray front panel, which serves as the user interface, and the white block diagram, where code is developed. The code completed thus far acquires data in a high-priority Timed Loop, stores it in a buffer, and deterministically transfers it to a lower priority loop to display to the user interface. In addition to displaying to the user interface, this tutorial covers logging data locally to the real-time target. Error handling and comments are added in the screenshot below to the first tutorial solution. Propagating errors is especially important in embedded processors to ensure a safe shutdown.

 

It’s a best practice to separate high-priority DAQ from presenting or logging the data. This minimizes jitter and allows your Timed Loop to run with “real-time” performance, meaning that it always finishes on time. By running tasks in separate loops at distinct rates, you maximize CPU efficiency by executing each task only as often as necessary. In the low-priority loop, you pull data from the shared variable buffer to not only display on the user interface but also log to disk.
 

  1. Place a TDMS Open VI in the initialization sequence structure (search the Functions palette or right-click in the blank space to bring up the Functions palette and then navigate to Programming»File I/O»TDM Streaming»TDMS Open). With the NI TDMS file format, you can organize data into groups and channels.
  1. Right-click the file path input terminal and Create»Constant. Type in the file path as /c/test1.txt, which is the proper syntax for the cRIO-9068 running the NI Linux RTOS. For targets running another RTOS, like VxWorks or Phar Lap ETS, you can type in c:\test1.txt. LabVIEW should coerce it to the correct syntax. Create another constant for the operation input and select replace or create for the value.

 

Note: You can also write to connected USB or SD Card storage entering the file path as the U: drive.

  1. Wire the Error Out from the Data Buffer variable to the Error In terminal of the TDMS Open VI. Then wire the Error Out and the tdms file out outputs through the sequence structure to the border of the While Loop as shown below.
  1. Create a branch from the Error wire up to the Error input node on the Timed Loop to propagate errors to both loops.
  1. To write to the file just created, place down an instance of the TDMS Write VI (Programming»File I/O»TDM Streaming»TDMS Write) inside the While Loop and wire the error and file reference inputs appropriately.
  1. To write data from the first channel to file, place down an Array Subset VI (Programming»Array»Array Subset). Branch a wire from the Data Buffer shared variable into the array input. Right-click on the index and length inputs and Create»Constant. The index for the first channel is and the length 1. Then wire the subarray output into the data input of the TDMS Write VI.

 
  1. Place down an Or (Programming»Boolean»Or) on the block diagram next to the Stop Control. Then wire a branch from the Error Out terminal of the TDMS Write VI into one input and delete and rewire the Stop Button value into the other input, as shown below. This ensures that the program stops either from the button or if an error is encountered.
  1. Outside the While Loop to the right, place down a TDMS Close VI (Programming»File I/O»TDM Streaming»TDMS Close) to ensure the reference and file on disk are properly closed. Wire the error and file reference inputs accordingly.
  1. Finally, right-click on each of the error and file reference tunnels on the left border of the While Loop and select Replace with Shift Register from the drop-down menu. A shift register enables the passing of data from one loop iteration to the next. LabVIEW replaces the tunnel you right-clicked with a shift register terminal, and the cursor becomes a shift register icon (). Hover over the tunnel on the opposite side of the loop until it flashes and then click the tunnel to replace it with a shift register.
  1. When completed, your While Loop should look like this:
  1. Finally complete wiring the diagram by adding meaningful group and channel names for your data. Right-click on the group name in and channel name in inputs of the TDMS Write VI and Create»Constant. An example is shown below.
 
 

Your basic acquisition, analysis, and logging application is now complete. Once you connect to CompactRIO hardware, you can run the code on the target.

 

Choosing the Right Programming Method for your I/O

In addition to FPGA access to I/O modules, it is also possible to combine FPGA programming with other programming approaches in new NI embedded devices. 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. Connect to any sensor on any bus with 100+ C Series modules; modular I/O with built-in signal conditioning and high accuracy A/D converters.