Example Code

Write and Read Binary Files Using 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

This example will show you how to read and write binary files using LabVIEW. It will also teach you how to write and read data structures such as two-dimensional arrays to a binary file. It will also show you what the transfer data packet size is in LabVIEW using the Write to Binary File vi to write to a Network folder. 

How to Use

LabVIEW programs can access binary files with the Write to Binary File and Read from Binary File VIs located in the Programming»File I/O functions palette. There are several examples in the NI Example Finder (Help»Find Examples...) under Fundamentals»File Input and Output»Read Binary File.vi and the Write Binary File.vi that demonstrate how to use these functions. 

Windows and IPv protocols determine the data transfer packet size that is sent from LabVIEW. More specifically the built in Windows SMB protocol is used to transfer the data with a maximum packet size of 64 KB. 

Additional Information

Accessing binary files follows the same code structure as accessing other files. Specifically the user opens the file, calls the read and write functions as needed, and closes the file afterwards. The image below shows a simple case of how one would write a 32-bit signed integer to a binary file:

When reading a binary file, the user must know the format the data was in when it was written to the file. When writing binary files take careful notes of the format in which the data is stored so that it can be properly read back later. It is up to the user to choose the structure of their binary file and there is no universally accepted format for binary files (unlike ASCII) so other applications or operating systems may be unable to read meaningful data from the file. 

When writing integers to a binary file the bits of the integer are mapped to bits in the file. For example if an I32 is written to a binary file it takes 32 bits of space.
Writing an I32 2D Array to a binary file is more complex because the array may have different numbers of rows and columns. To simplify this 2D Array data is often saved with a header that describes these dimensions. To read a 2D array from a binary file LabVIEW requires an 8-byte header. Creating this header can be done in two ways:
  • Manually: The Array Size function can be used to determine these values. A FALSE constant must be wired to the Prepend Array or String Size? terminal of the Write to Binary File function. 
  • Automatically: To generate the header automatically, a TRUE constant can be wired to Prepend Array or String Size? terminal of the Write to Binary File function (but is not required).
In general, it is recommended to let the Write to Binary File function create the data header automaticallybecause the header varies according to the type of data saved to disk. However, there may be times where a user explicitly needs to know the header structure and write to (or read from) the disk. In this case, the Online Reference can be used to determine the necessary headers for different data types. 

The image below shows a simple case of writing a 2D array to a binary file and automatically creating the header:
To read the binary file, wire the expected data type into the Data Type input. For example, if the file contains a 2D array of 32 bit integers connect a 2D Array of 32 bit integers to the Data Type input. LabVIEW will then assume that the appropriate header for the dummy data type is stored in the file, and uses it to extract the binary information. The image below shows a simple case of reading 2D data from a binary file:

Related Links

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