Send or Receive Binary or Hexadecimal Data in LabVIEW

Updated Aug 28, 2023

Reported In

Software

  • LabVIEW

Driver

  • NI-VISA

Issue Details

  • My application requires me to send and receive binary or hexadecimal values, rather than ASCII characters, but the VISA Read and VISA Write functions in LabVIEW only accept strings as valid data types. How can I use binary or hexadecimal data with the LabVIEW VISA functions?
  • I would like to stream hexadecimal data to a third-party instrument using LabVIEW. How can I do this?
  • I am trying to connect and communicate with a equipment over TCP. I need to send a set of Bytes using the TCP Write function. I simply connected a string constant to the TCP write function and and enter the Bytes manually, but it did not work as expected. How is the right way to pass Bytes over TCP?

Solution

To send binary or hex data, you have to first convert the data to string format, so that it can be passed to VISA, TCP or UDP Write function. Likewise, the string value provided by VISA, TCP or UDP Read can be converted into the desired output format. While those communication functions in require a string value to be used as the data input and output, the character values in a string are not restricted to standard ASCII characters. Each character is an unsigned 8-bit integer (U8), with possible values ranging from 0 to 255.

There are several methods that can be used to transmit binary values, depending on the format of your source data. Below we present some examples using NI-VISA Functions, but the very same concept can also be applied to TCP and UDP:
  • Transmit constant hexadecimal values, and display received data in HEX format.

The simplest way to transmit a constant hexadecimal (hex) value is to use a string constant, with the constant configured for Hex Display (configurable by right-clicking on the constant and selecting Display Format...). This will allow you to type the hex values of non-ASCII characters in a string control or constant, which can then be passed directly to the VISA Write function. When reading data with VISA Read, the string can be wired directly to a string indicator in hex display mode in order to display the hex values to the front panel.

Transmit & Receive constant Hex values: 
  • Transmit U8 integers (single value or array).

In order to transmit a single byte of data (U8 integer), the Build Array function should be used to form an array with a single element. The Byte Array to String operator can then be used to create a string which is compatible with VISA Write. The same procedure can be used to transmit an array of bytes, with the exception that Build Array is not needed.
 
Transmit & Receive a single U8 integer:

Transmit & Receive an array of U8 integers:
  • Transmit a non-U8 value or an array of values.

When the binary data to be transferred is larger than a single byte, it is necessary to first typecast the value or array to a U8 Array, then convert this array to a string which is acceptable to VISA Write. The same operations are performed in reverse order to convert the string acquired from VISA Read into a binary data type. This may be used with single data elements, or with arrays of elements.

Transmit & Receive a multi-byte value:

Note: When working with binary data types larger than one byte (I16, U32, DBL, etc...), the endianness (byte order) of the remote device can become an issue. LabVIEW is natively Big Endian, so communication with Little Endian devices will require changes to the byte ordering of data elements.
 

Additional Information

In orde to visualize or enter an hexadecimal value in a U8 control, indicator or constant, you have to change their radix to hexadecimal. There are other options such as binary, decimal, octal, or SI notation. More detailed instructions can be found in this article from LabVIEW Help.