Example Code

Read a binary file in a producer-consumer design pattern in LabVIEW

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

Code and Documents

Attachment

Description

1. Overview

This example VI shows how to perform reading from a binary file in a producer-consumer design pattern, so that the data can be processed in smaller pieces, as determined by the user in the front panel.

 

2. Description

When you need to stream a great deal of data, more than you can store in memory, many times you will need to take the data from a file on disk. Now, if you want to loop this data the solution is rather simple.

All you need to do is determine how many reads will be required to iterate through the file given the file's size, the data type being read and the number of elements to read with each iteration. With this information you can simply perform the necessary number of iterations with a for loop. By placing the for loop inside a while loop and reseting the file position each time you enter the for loop you will be able to loop through the file.

The attached file does more than simply loop through the file, there is also logic for determining the number of iterations and a basic producer-consumer (data) design pattern.

This example reads the file in a loop indefinitely until the user stops the VI. For a more practical development, consider removing the producer while loop.

NOTE: You must change the data type of the elements to read in order to suit your needs. Go to the front panel, right click the Data Type to Read control, and select "Replace".

 

3. Requirements

  • NI LabVIEW Base Development System 2012 or compatible.

 

4. Steps to Implement or Execute Code

  1. Download the compressed file and open the VI "Read Binary File in a Producer-Consumer pattern LabVIEW 2012 NIVerified.vi"
  2. Select the file to read in File Path (you can use a provided example Binary Example DBL.bin). NOTE: Write the full path to where the file is located.
  3. Replace with the data type you want to read from the binary file. Right click in the control and select Replace.
  4. Select the Elements to Read per chunk.
  5. Run the VI.

 

5. Additional Information or References

 

Read Binary File in a Producer-Consumer pattern Screenshot.PNG

 

**The code for this example has been edited to meet the new Community Example Style Guidelines.**

Regards,
Dan King

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