Take Your First Measurement in LabVIEW Real-Time (Data Logging)

Publish Date: Jun 15, 2018 | 23 Ratings | 4.13 out of 5 | Print | Submit your review


This tutorial provides a walkthrough with steps to log data to disk with CompactRIO.

After you've finished detecting your CompactRIO system using LabVIEW, you need to take your first measurement.

This page is part of a walkthrough on Setting Up Your CompactRIO System.

Table of Contents

  1. Starting a New CompactRIO Project in LabVIEW
  2. Select the Appropriate Programming Mode
  3. Create a basic logging application with CompactRIO Real-Time Scan Mode
  4. Accessing the logged data
  5. Additional Resources
  6. Discussion and Feedback

1. 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

2.       Adding your CompactRIO system to the project by right-clicking on the Project item at the top of the tree and select New » Targets and Devices… This dialog allows you to discover systems on your network or add offline systems.

Figure 1. Add new targets and devices to your LabVIEW project

3.       Expand the Real-Time CompactRIO folder and select your system. Click OK to close the dialog window. 

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 Measurement & Automation Explorer. If your system is on a remote subnet, you can also select to manually enter the IP address.

Figure 2. Select a new CompactRIO system

Back to Top

2. Select the Appropriate Programming Mode

CompactRIO systems have two user-selectable modes and the CompactRIO with NI-DAQmx has three mode options for each slot. After adding your target to the project, you will be prompted to select which programming mode you would like to use. 

Note: You can change the programming mode later if needed using the CompactRIO Chassis Properties dialog box. 

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 Understanding NI CompactRIO Scan Mode 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

Note: If your system was not discovered and you choose to add it offline, you will need to add the chassis and C Series I/O manually. This document discusses this process for scan mode and FPGA mode.

Once your system has been added to the LabVIEW Project, proceed to either the CompactRIO Scan Interface Tutorial below or the LabVIEW FPGA Interface Tutorial, depending on which programming mode you selected.

Back to Top

3. Create a basic logging application with CompactRIO Real-Time Scan Mode

This section will walk you through creating a basic logging application on CompactRIO using scan mode. 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 from here.

1.       Save the project by selecting File»Save in the top left corner. 

2.       Enter the name as Basic logging with scan mode and click OK to close the dialog window.

Figure 3. Save LabVIEW Project Basic logging with scan mode

3.       Create a VI by right-clicking on the CompactRIO Controller in the project and selecting New»VI. Save the VI as RT.vi. This project will only contain one VI, which is the LabVIEW Real-Time application that runs on the CompactRIO Controller.

Figure 4. Create a new VI underneath the recently added CompactRIO Controller

4.       Place a flat sequence structure with three frames on your RT.vi block diagram as shown below. The basic operation of this application will include three routines: startup, run, and shutdown. A flat sequence structure is an easy way to enforce this order of operation.

Figure 5. Flat Sequence structure in LabVIEW

5.       Add a timed loop to the Run frame of the sequence structure. Timed loops provide the ability to synchronize code to various time basis, including the NI Scan Engine that reads and writes scan mode I/O.

Figure 6. Flat Sequence structure in LabVIEW with Timed Loop

6.       Double-click on the clock icon on the left input node to configure the timed loop.

Figure 7. Open Timed Loop configuration by double clicking on the top left corner

7.       Select Synchronize to Scan Engine as the Loop Timing Source. 

8.       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.

Figure 8. Configure Timed Loop using Scan Engine as a Timing Source

9.       Right-click the CompactRIO controller in the LabVIEW project and select Properties.

10.     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.

Figure 9: Adjust the Scan Period to control the refresh rate

11.       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. 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 Controller, chassis, and the I/O module you would like to log. 

Figure 10. Drag and Drop C Series I/Os into the Timed Loop

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.


Figure 11. Align Objects using Left Edges and Vertical Compress


12.  Place a normal while loop in the Run frame under the timed loop, which will be used for the file I/O task. Because file I/O takes and undermined amount of time, you must separate the I/O acquisition task and the file I/O task. Neglecting this requirement could lead to losing data, since the file I/O may take longer than the I/O scan, causing a sample to be missed. 

In order write the data to disk in the regular while loop, you need to transfer the I/O values 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 new I/O values to the buffer each time. Then, the regular while loop will read the data out of the buffer and write it to disk. Separating the I/O task and disk access in this way allows your timed loop to run with “real-time” performance, meaning that it will always finish on time.

Figure 12. Using to loops to make sure logging data doesn't slow down I/O sampling rate

Figure 13. Real-Time Task accessing I/Os vs. Low priority task logging data

13.   In the LabVIEW Project, right-click on the CompactRIO Controller and select New»Variable.

Figure 14. Adding new Variable in the LabVIEW project

14.   Name the variable Data buffer, select Single Process as the Variable Type.

15.   Select 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. 

Figure 15. Using Variable Properties to define Variable and Data Types 


16.   Then select RT FIFO from the menu on the left.

17.   With the RT FIFO category selected, select the Enable RT FIFO check box, select Multi-element for the FIFO Type.

18.   Enter 50 for the Number of arrays, and enter 4 for the Number of elements (if you are logging a number of channels other than 4, enter that instead.) This configures the variable to operate as a real-time-safe FIFO, which can serve as the data buffer between our real-time and low priority tasks. The FIFO will hold fifty one dimensional arrays, each of which contain four double precision numbers. Click OK.

Figure 16. Using Variable Properties to enable RT FIFO

19.   Drag and drop the Data buffer RT FIFO into your timed loop and use a Build Array function to build an array from the I/O variables and pass it into the RT FIFO.

Figure 17. Drag and drop the Data buffer and place it in the timed loop

20.   Drag and drop an additional copy of the Data buffer RT FIFO into the regular while loop, where you will read the data out and log it to disk. 

21.   Right-click on the RT FIFO and select Show Timeout and wire a timeout of 100. This will cause the RT FIFO to wait up to 100ms for new data to arrive in the buffer before it times out. If data is present in the buffer, the RT FIFO will return the oldest data in the buffer immediately.

Figure 18. Drag and drop an additional copy of the Data buffer

22.   Place a TDMS Open VI in the Startup frame of your sequence structure. 

23.   Create constants for the file path and operation inputs. Type c:\tempdata.tdms in for the file path and select create or replace for the operation. In case your CompactRIO Controller is running a different Real-Time OS choose the appropriate file path shown below. Please see Real-Time Controllers and Real-Time Operating Systems Compatibility for more information. 

Figure 19. Using the right file path depending on the CompactRIO Controller that is used

24.   Download it here or drag and drop the VI snippet below from your web browser to your diagram to perform the file I/O. 

25.   Place this code in the regular while loop and wire it as shown below. A case structure is used to execute this section of code only when the RT FIFO does not time out, indicating that new data has been returned from the buffer. Then, the data is formatted to be written to the TDMS file and displayed on the waveform chart. A stop button for the while loop is also provided in addition to checking for errors as a stop condition.

Drag this VI Snippet to your VI’s block diagram

Figure 20. Logging Code without a Shutdown


26.   In the LabVIEW Project, create another shared variable by right-clicking on the existing library that was created automatically and contains the Data buffer variable and selecting New»Variable.

27.   Name the variable stop, select Single Process, and select Boolean. Select the RT FIFO category on the left.

Figure 21. Select Single Process and use Boolean as a Data Type

28.   Select Enable RT FIFO, leave Single-element selected and click OK. This creates a variable that is safe to read from in tasks that require real-time performance.

29.   Place a copy of the stop variable in the timed loop and wire it to the stop condition.

30.   Place another copy of the stop variable in the regular while loop and write the result of the OR function to it as shown. This will cause the timed loop to stop when the regular while loop stops.

31.   Place a TDMS Close function in the Shutdown frame and wire the file reference and error cluster through to it. Also, create and indicator from the TDMS Close error out terminal.

Your completed application should look like the image below:

Figure 22. Finished LabVIEW Block Diagram

28.   Click Run 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.

29.   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.

Figure 23. Finished LabVIEW Front Panel showing logging data

30.   Click STOP once you are finished viewing and logging data.

Congratulations! You have successfully created an embedded logging application with LabVIEW and CompactRIO. To continue learning, check out the additional resources on the Embedded Programming with LabVIEW and CompactRIO page.


Back to Top

4. Accessing the logged data

Complete these steps to retrieve and view the data logged on the CompactRIO system. If you don't have an FTP server installed on your system, CompactRIO also supports WebDAV (Web Distributed Authoring and Versioning). It’s a secure alternative to exchange files using a standard web browser. Please see Using WebDAV to Transfer Files to and from your Real-Time Target for more information. 

1.       Using Windows Explorer or a Web browser, navigate to ftp://<ip address> where <ip address> is the IP address of your CompactRIO system.

2.       Download tempdata.tdms (or whatever you named the TDMS file)

3.       If you have Microsoft Excel, you can view TDMS files by selecting the Add-Ins tab and clicking the TDM Importer. The first page of the workbook contains file information and the remaining sheets contain the channel data. The TDM Importer installs with LabVIEW by default; however, if the TDM Importer is not present, you can install it from here.

Figure 26. Install TDM Importer in order to open the tdms files directly in Excel

4.       If you do not have Excel, you can view the TDMS file using the TDMS File Viewer VI in LabVIEW.


Back to Top

5. Additional Resources


Back to Top

6. Discussion and Feedback

We do not regularly monitor Reader Comments posted on this page.

Please submit your feedback through the discussion forum thread so that we can improve this content. 

Please direct support questions to NI Technical Support .

Back to Top

Bookmark & Share


Rate this document

Answered Your Question?
Yes No