Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
15 ratings:
 3.20 out of 5     Rate this Document

Send or Receive Binary / Hex Data Using NI-VISA in LabVIEW

Primary Software: LabVIEW Development Systems>>Full Development System
Primary Software Version: 8.2
Primary Software Fixed Version: N/A
Secondary Software: Driver Software>>NI-VISA
Hardware: Serial

Problem:
The VISA Read and VISA Write functions in LabVIEW only accept strings as valid data types. My application requires me to send and receive binary or hexadecimal values, rather than ASCII characters which would normally be present in a string. How can I use binary or hexadecimal data with the LabVIEW VISA functions?

Solution:
While the LabVIEW VISA functions 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. The key to sending binary data is to first convert the data to string format, so that it can be passed to the VISA Write function. Likewise, the string value provided by VISA Read can be converted into the desired output format.

There are several methods which can be used to transmit binary values, depending on the format of your source data:

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



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



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


Related Links:
KnowledgeBase 3OIFGTLA: Writing Bits to the Serial Port Instead of Writing ASCII Strings

Attachments:


singleu8.jpg - singleu8.jpg
hexarray.jpg - hexarray.jpg
hexdisplay.jpg - hexdisplay.jpg
multibyte.jpg - multibyte.jpg


Report Date: 11/16/2006
Last Updated: 11/29/2006
Document ID: 43FDH92W

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit