Example Code

Loop Slows Down While Acquiring and Logging Data to a File

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

    Driver

  • NI-DAQ™mx

Code and Documents

Attachment

Description

This article provides a solution for common problems when analyzing and saving data after acquisition from a DAQ device, such as

  • Slowdown of a loop due to analysis and logging
  • Data not looping on the correct speed
  • Slow code with DAQ assistant in a while loop
  • Data skipping or data that appears out of order

 

How to Use

The best practice when you need to process and log data, but don't want to slow down your data acquisition, is to use a Multiple Loop Design Pattern. We can divide our task into two loops, an Acquisition (Producer) Loop and an Analyze/Result (Consumer) Loop, and send data between them using FIFO Queues. 

Since the data acquisition is in its own loop, it can run faster than the loop that is analyzing and logging data. This ensures us that we receive data with desired frequency and have all the data analyzed and presented using slower Consumer Loop. An example of such an application is attached. 

In this example code, the lower consumer loop receives the data from the queue and displays it on a waveform chart. You would place any slower data processing and logging code in the consumer loop.

Also, having a DAQ Assistant in a while loop can cause code to run slow and is not advised for long term solutions. The DAQ Assistant is a bulky express VI that will perform a lot of initialization tasks every time the loop is run. If you are trying to run at fast speeds, this can cause slower than expected data acquisition. It is recommended to just do the initialization once, as is shown in the attached example, and just perform your read or write function in a while loop. You can also check your sampling rate in NI MAX to ensure that your card is function properly, and isolate issues from the LabVIEW environment.

 

Additional Information

Get Queue Status.vi in the example VI ensures that the consumer loop dequeues and processes all data that was inserted in the queue before the application stops.

For more information on using this type of design pattern, refer to Application Design Patterns: Producer/Consumer.

Related Links

 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.