Company Events Academic Community Support Solutions Products & Services Contact NI MyNI

Data and String Manipulation Techniques

LabVIEW 2013 Help

Edition Date: June 2013

Part Number: 371361K-01

»View Product Info

Communicating with instruments involves sending data to instruments and retrieving data from instruments. You rarely need to format the data sent to or retrieved from the instrument when using instrument drivers because the instrument driver handles the formatting for you. However, you might need to format the data when writing VIs to communicate with instruments.

When you communicate with a message-based instrument, you must format and build the correct command strings for the instrument to perform the appropriate operation or return a response.

No standards exist for register-based instrument communication. Each device operates differently, and the instrument documentation is the best resource for learning how to program the device.

Typically, a command string, or query, is a combination of text and numeric values. Some instruments require text-only command strings, requiring you to convert the numeric values to text and append them to the command string. Similarly, to use the data an instrument returns in LabVIEW, you must convert the data to a format a VI, function, or indicator can accept.

(Windows) You can use the Instrument I/O Assistant to send queries to an instrument and to format the data an instrument returns. Place the Instrument I/O Assistant Express VI on the block diagram to access the Instrument I/O Assistant.

You can use the Format Into String function to build command strings to send to an instrument or to append other strings or numeric data types to an initial string.

Formatting Data Retrieved from an Instrument

Just as the commands strings you send to an instrument contain headers and trailers, most instruments return data with headers and/or trailers. The header usually contains information such as the number of data points returned or the instrument settings. Trailer information often contains units or other instrument settings at the end of the data string. The instrument documentation should describe what header and trailer information to expect from each data transfer. You must first remove the header and trailer information before you can display or analyze the returned data in LabVIEW.

You can use String Subset functions to parse the header and trailer information in a string and display them in string indicators. You can use the Scan From String function to parse the data from the string.

Waveform Transfers

Instruments also can return data in other formats, such as ASCII, 1-byte binary, and 2-byte binary formats. The instrument documentation describes available formats and how to convert each one to usable data.

ASCII Waveforms

If an instrument returns data in ASCII format, you can view the data as a character string. However, if you need to manipulate the data in numeric format or if you need to graph the data, you must convert the string data to numeric data. As an example, consider a waveform composed of 1,024 points and in which each point has a value between 0 and 255. Using ASCII encoding, you need a maximum of 4 bytes to represent each data value (a maximum of 3 bytes for the value and 1 byte for the separator, such as a comma). You need a maximum of 4,096 bytes (4 bytes 1,024) plus any header and trailer bytes to represent the waveform as an ASCII string. You can use the Fract/Exp String To Number function to convert the ASCII string to a numeric array.

1-Byte Binary Waveforms

Some instruments do not have the option of sending data in ASCII format or send all waveform data in binary format. Because no standard exists for binary format, refer to the instrument documentation to determine exactly how the instrument stores the data values. One common binary format is 1-byte binary. With this type of data encoding, the instrument converts each piece of data to an 8-bit binary value before sending it.

When you read 1-byte binary data from the bus, the instrument returns the data as a character string. However, the characters do not appear to correspond to the expected data. LabVIEW interprets the binary numbers as ASCII character values, and displays the corresponding characters. For example, if you send the value of 65 as one data value, you read the character A from the bus. For a value of 13, no printable ASCII character exists because 13 corresponds to an invisible carriage return character.

You can display these invisible characters in a string indicator by right-clicking the indicator and selecting '\'Codes Display from the shortcut menu. The carriage return character is \r in the string indicator.

To use the numeric data in an ASCII string with the Analysis VI or to display the numeric data in a graph or chart, you must convert the binary string to a numeric array. If the instrument sends a binary string that contains 1,024 1-byte binary encoded values, the waveform requires only 1,024 bytes plus any header information. Using binary encoding, you need only 1 byte to represent each data value, assuming each value is an unsigned 8-bit integer. You can use String Subset functions and the String To Byte Array function to convert the binary string to an array of integers.

Note  When acquiring binary data, extract the data using the data size rather than searching for the first character of the trailer information because that character might be part of the binary values.

2-Byte Binary Waveforms

When data is in 2-byte binary format, it is binary encoded and sent as ASCII characters similar to the 1-byte binary format. However, 16 bits of data, or two ASCII characters, represent each data value. Although this format uses twice as much space as the 1-byte binary format, it is more efficiently packed than ASCII-formatted data.

As an example, consider an oscilloscope that transfers waveform data in binary notation. For this example, assume the waveform consists of 1,024 data points where each value is a 2-byte signed integer. Therefore, the entire waveform requires 2,048 bytes plus a 5-byte header and a 2-byte trailer. After you remove the 5-byte header, use the Unflatten From String function to convert the waveform string to an array of 16-bit integers.

It is important to know the order of the bytes you receive when data is transferred in 2-byte binary format. The 2-byte combination qH has the corresponding integer value of 29,000, but the opposite byte order of Hq has the corresponding integer value of 18,545.

Note  You can configure the byte order for some instruments, and some instruments have a fixed byte order. Refer to the instrument documentation for specific instrument byte information.

If you receive the high byte first, you must reverse the order of the bytes before you convert them to an integer value. Use the Unflatten From String to reverse the byte order.


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit