Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
LabVIEW-Measurements.pdf
Скачиваний:
61
Добавлен:
29.05.2015
Размер:
3.37 Mб
Скачать

Chapter 20 VISA in LabVIEW

must find a way to convert the numeric values to text and append them onto the rest of the command string. The Format Into String function can be used to build the command strings that you need to send to your instrument. This function allows you to take an initial string and append other strings or numeric data types to it.

Removing Headers

This section describes how you read the information returned from a message-based instrument. The instrument user manual describes what header and trailer information to expect from each data transfer.

Most instruments return data with extra information attached as a header or a trailer. The header usually contains information such as the number of data points returned or the instrument settings. In some cases, some trailer information containing units or other instrument settings is at the end of the data string. You must first remove the header and trailer information before you can display or analyze the returned data.

Consider the example shown above in which the string contains a 6-byte header, the data points, and a 2-byte trailer. You can use the String Subset function, available on the Functions»String palette to remove the header as shown above. String Subset returns a substring of the string input beginning at offset and containing length number of characters. Length and offset must be scalar. The offset of 6 above removes the header of CURVE<space>, and the length of the string subset is the total length of the instrument response string minus the length of the header.

Note For strings, the offset value starts at zero, just as the index for arrays starts at zero.

Waveform Transfers

In addition to header and trailer information, instrument data can be returned in various formats. The instrument user manual describes what formats are available and how you can convert each one to usable data. The formats discussed in this section include ASCII, 1-byte binary, and 2-byte binary formats.

ASCII Waveforms

If data from an instrument is returned in ASCII format, you can view it as a character string. However, if numeric manipulation of the data is necessary or you need to graph the data, you must first convert the string data to numeric data. As an example, consider a waveform composed of

1,024 points, each point having a value between 0 and 255. Using ASCII

LabVIEW Measurements Manual

20-10

www.ni.com

Chapter 20 VISA in LabVIEW

encoding, you would 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 would 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.

Another example is shown above with the header information already removed —10.2, 18.3, 8.91, 1.0, 5.5. You can use the Extract Numbers VI to convert that ASCII string to a numeric array. The Extract Numbers VI is an example VI available in the Search Examples Help. It finds all numbers in the given ASCII string and puts them into a Single Precision Array of numbers. A non-numeric delimiter such as a comma, colon, line feed, etc. is assumed and all formats listed above are recognized. Any characters at the beginning of the ASCII string are ignored, so you do not need to strip off header information when you use the Extract Numbers VI. The example above shows how you can extract the five values from the string and place them into an array of numbers. You can now plot those values or use them in data analysis algorithms.

1-Byte Binary Waveforms

Some instruments do not have the option of sending data in ASCII format, or for performance reasons all waveform data is sent in binary format. No standard binary format exists, so you need to find out exactly how the data values are stored from the instrument user manual. One common binary format is 1-byte binary. With this type of data encoding, each data value is converted to an 8-bit binary value before being sent.

When you read 1-byte binary data from the bus, it is returned as a character string. However, the characters do not appear to have any correspondence to the expected data. The binary numbers are interpreted as ASCII character values and the corresponding characters are displayed. Some examples are shown in the table above. If a value of 65 is sent as one data value, you would read the character A from the bus. Notice that for a value of 13, there is no printable ASCII character; 13 corresponds to an invisible carriage return character.

You can display these invisible characters in a string indicator in LabVIEW if you view the ‘\’Codes Display. For example, the top string indicator shown above displays the values in the default Normal Display and you cannot see the third character. However, if you right-click that front panel string indicator and choose ‘\’ Codes Display from the shortcut menu, you will see the carriage return character as a \r, as shown above in the second string indicator.

© National Instruments Corporation

20-11

LabVIEW Measurements Manual

Chapter 20 VISA in LabVIEW

By enabling the ‘\’ Codes Display on the LabVIEW string, you can now see characters that may have been invisible before. However, you still must convert the binary string to a numeric array to graph or do mathematical operations with the data.

Suppose the instrument sends a binary string containing 1,024 1-byte binary encoded values as shown above. That waveform would require only 1,024 bytes plus any header and trailer information. Using binary encoding, you would need only 1 byte to represent the data value, assuming each value was an unsigned 8-bit integer.

Converting the binary string shown above to a numeric array is a little more complex than converting an ASCII string. You must first remove all header and trailer information using the String Subset function as previously described. Then you convert the remaining data string to an array of integers using the String To Byte Array function, available on the

Functions»String»String/Array/Path Conversion palette.

Note Using binary data, it is better to extract the data using the data size rather than searching for the first character of the trailer information, because it is possible that the search character might also be contained as part of the binary values.

2-Byte Binary Waveforms

A third data format is 2-byte binary. When data is in 2-byte binary format, it is binary encoded and sent as ASCII characters just like the 1-byte binary. 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 data, it is still more efficiently packed than ASCII formatted data.

As an example, consider an oscilloscope that transfers waveform data in binary notation. For this example, 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. Remove the 5-byte header and take the next 2,048 bytes. Then use the Type Cast function, available on the Fuctions»Advanced»Data Manipulation palette, to convert the waveform string to an array of 16-bit integers.

Byte Order

When data is transferred in 2-byte binary format, it is important to know the order of the bytes you receive. 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.

LabVIEW Measurements Manual

20-12

www.ni.com

Chapter 20 VISA in LabVIEW

If you receive the high byte first, you must reverse the order of the bytes before converting them to an integer value. Consider the example shown above. This 2-byte binary waveform data is the same size and contains the same header and trailer information as shown on the previous slide, but the data is sent with the high byte first. The diagram above shows that you still strip off the header and use the Type Cast function to convert the binary string to 16-bit integers. However, the Swap Bytes function, available on the Functions»Advanced»Data Manipulation palette, is needed to swap the high-order 8 bits and the low-order 8 bits for every element.

© National Instruments Corporation

20-13

LabVIEW Measurements Manual

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]