Keysight VISA.NET Help
Using Read Methods

Using Read Methods

Formatted I/O Read methods provide an easy way to read an individual item from an instrument.

Whereas Scanf methods are designed to allow flexibility in reading and parsing multiple formatted items, Formatted I/O Read methods are designed to read and parse one item of data in a particular format. While there are just over 20 Scanf methods and overloads, there are just over 130 for Read; there is a Read method for each data type and each category of Read method.

In general, each Read method corresponds to a Scanf method with a particular scanf format specifier and corresponding argument. For example, the ReadString() method corresponds to Scanf(String, Int64[], T) with a format specifier of "%s". Each Read method reads a single string and returns it, without reading a trailing newline or END (although in both cases, an END will be handled if the argument ends in a newline and END is enabled).

Refer to the reference documentation for each Read method to see the corresponding Scanf method and format specifier.

Since many Read methods differ only in their return types, and since methods cannot be overloaded based only on differing return types, the method names must be decorated with the type name of the return type. For example, Int16 ReadList(Int64 count) and Int32 ReadList(Int64 count) differ only in their return types, and cannot coexist as overloads of ReadList. Therefore, in VISA.NET, the names are decorated with the return type. Int16 ReadList(Int64 count) is actually Int16 ReadListOfInt16(Int64 count) and Int32 ReadList(Int64 count) is actually Int32 ReadListOfInt32(Int64 count).

In order to understand the variety of Read methods, it is convenient to categorize them by method name prefixes – the part of the Read method name before the return type. There are six method name prefixes listed in the table below, with a description of the data types supported for each name.

Method Name

Description

Read{type}

Read{type} parses a single item of data from the formatted read buffer. It does not process a newline or END.

There are five type suffixes for Read{type} methods, one each for the types Char, String, Int64, UInt64, and Double. Values of smaller integer or real types are cast up if needed.

Note that there are four overloads to the ReadString method. These allow for returning String or StringBuilder results, and for specifying the number of characters to be read into the string.

ReadLine{type}

ReadLine{type} starts at the beginning of the formatted read buffer and reads characters that can be converted to the type specified by the return type of the method. Characters from the first character that cannot be converted to the type specified (if any), up to but not including the first EOL character, are read and ignored. All characters up to and including the EOL character are removed from the formatted read buffer. (If the return type is Char, one byte is read from the formatted read buffer and is converted to a UNICODE character before it is returned.)

There are five type suffixes for ReadLine{type} methods, one each for the types Char, String, Int64, UInt64, and Double. Values of smaller integer or real types are cast up if needed.

Note that there are two overloads to the ReadLine method that return a String or StringBuilder result.

ReadListOf{type}

ReadListOf{type} formats an array of data as a comma-separated list and adds it to the formatted write buffer. It does not add a newline or END, and it does not send any data to the instrument.

ReadListOf{type} has 20 methods and overloads. It supports 10 data types: Byte, SByte, Int64, UInt64, Int32, UInt32, Int16, UInt16, Single, and Double. The smaller integer and real types are supported in order to avoid the performance penalty that would otherwise be incurred to upcast those types to arrays of the larger types when making the method call.

Char is not supported because ReadString can easily read the same data.

For each of the type-specific methods, there are two overloads:

  • One overload takes no arguments and returns the array that was read.
  • The other takes three arguments: the array elements to be returned (an out parameter), an index that indicates where to start returning elements from the array returned by the resource, and a count that indicates how many elements to parse, starting with index.

ReadLineListOf{type}

ReadLineListOf{type} performs the same tasks as ReadListOf{type} but characters from the first character that cannot be converted to the type specified, if any, up to but not including the first EOL character, are read and ignored. All characters up to and including the EOL character are removed from the formatted read buffer.

ReadLineListOf{type} has 20 methods and overloads, which correspond exactly to the ReadListOf{type} overloads.

ReadBinaryBlockOf{type}

ReadBinaryBlockOf{type} starts at the beginning of the formatted read buffer and reads characters from an IEEE 488 block or raw binary array that can be converted to an array of the type specified by the return type of the method, until the end of the block is reached. The block is removed from the formatted read buffer.

ReadBinaryBlockOf{type} has 40 methods and overloads. It supports the same 10 data types as ReadListOf{type}.

For each of the type-specific methods, there are four overloads:

  • One overload takes no arguments and returns the array that was read.
  • The second overload takes a Boolean that indicates whether the method should expect the block to start with the next byte in the read buffer or not. If not, the method reads and discards characters from the read buffer until it finds the first character in the block, or a termination character, or an END. If it finds a block, it continues to read and parse the block.
  • The third overload takes three arguments: a reference to the array to be returned, an index that indicates where to start returning elements from the block returned by the resource, and a count that indicates how many elements to parse, starting with index.
  • The fourth overload takes four arguments, combining the arguments of the second and third overloads, all with the same meanings.

ReadLineBinaryBlockOf{type}

ReadLineBinaryBlockOf{type} performs the same tasks as ReadBinaryBlockOf{type} but does append a newline (and END, if END is enabled) to the formatted data, sends whatever is in the write buffer to the instrument, and flushes the buffer.

ReadLineBinaryBlockOf{type} has 40 methods and overloads, which correspond closely, but not exactly, to the ReadBinaryBlockOf{type} overloads. In particular, none of the ReadLineBinaryBlockOf{type} methods or overloads has a count parameter.

Miscellaneous Read Methods

There are a few Read methods that do not fit neatly into any of the categories above:

Refer to the linked reference pages for more information about these methods.

Note that the Skip methods are included with the Read methods because they operate on the Formatted I/O read buffer and complement the set of Read methods.

Scanf Equivalents to Read{type} Methods

Each ReadString method exhibits the same behavior as the corresponding call to Scanf shown in the following table:

ReadString Method Equivalent Scanf Call
String ReadString(); Scanf("%s", out result);
(returns result)
Int32 ReadString(StringBuilder data); Scanf("%s", out data);
(returns data.Length)
String ReadString(Int32 count); Scanf("%#s", count, out result);
(returns result)
Int32 ReadString(StringBuilder data, Int32 count); Scanf("%#s", count, out data);
(returns data.Length)

 

 

 

 


© Keysight Technologies 2015-2025