Developing a Simple Driver

Most message-based instruments are controlled programmatically by a series of writes and reads to and from the instrument. For most simple instrument drivers only four VISA functions are needed: VISA Open, VISA Write, VISA Read, and VISA Close.

The simple instrument driver VI shown in the following illustration makes just one write and one read from an instrument. This VI first opens resources to the instrument using the VISA Open function. Then, it sends the MEAS:DC? command, as described in the documentation for the instrument, to return a DC measurement from the instrument. The VISA Read function returns the measurement in the form of a string. To use the measurement with other numeric functions, the string is converted to a numeric using the Scan From String function. After completing the last read or write to the instrument, the VISA Close function is called. This is followed by the Simple Error Handler VI to process any errors that might have occurred with the Instrument I/O functions.

For a more modular instrument driver, you might try to break up the reads and writes to the instrument according to the type of operation you are trying to accomplish. You might want to combine the reads and writes necessary to set up configuration into one VI, while measurement reading is in another VI. For repeated measurements, you could place your measurement VIs in a loop. If you know exactly what the configuration of your setup is, you probably could include all the configuration commands into one string constant, as shown below.

If, on the other hand, you want a user to be able to select different configurations, then you will need to programmatically build command strings. You can use the Pick Line function to choose from a selection of strings and concatenate it to another string in a single step. This procedure is easier than using a Case structure and the Concatenate Strings function.

The block diagram on the left in the following illustration is easier than the one on the right.

By using the Append True/False String function you can select a string constant and concatenate it to another string in a single step. This procedure is easier than using a Select function followed by a Concatenate Strings function.

The block diagram on the left in the following illustration is easier than the one on the right.

Another useful string function for building command strings is the Format Into String function. This function converts one or more numerics into a string with a variety of formatting options. Both the Pick Line and the Format Into String functions are demonstrated in the following block diagram.

After you read the response from the instrument, you should parse the measurement into a numeric value. The Scan From String function is useful for converting ASCII numbers to numerics. The following code strips off the VOLTS DC part of the string input and converts the +1.28E+2 to a double precision numeric. The string input is typical of a response from a multimeter.

If your instrument returns binary data, use the Type Cast function. This function changes the data type of a wire, but not how the data is stored in memory. VISA Reads return string data, regardless of whether it is encoded as ASCII or binary. Therefore, to convert the binary string to a numeric or numeric array, you need to type case the string to a different data type. The following example strips off a 4-byte header from a 1,000-byte response string and then converts the remaining values to a single precision array.