Formatted I/O Operations

NI-VISA

Formatted I/O Operations

The main two operations under the formatted I/O services are viPrintf() and viScanf(). Although this section discusses these two operations only, this material also applies to other formatted I/O routines such as viVPrintf() and viVScanf(). These operations derive their names from the standard C string I/O functions. Like printf() and scanf(), these operations let you use special format strings to dynamically create or parse the string. For example, a common command for instruments is the "Fx" command for function X. This could be "F1" for volt measurement, "F2" for ohm measurement, and so on. With formatted I/O, you can select the type of measurement and use only a single operation to send the string. Consider the following code segment.

/* Retrieve user's selections. Assume the variable */
/* X holds the choice from the following menu: */
/* 1) VDC, (2) Ohms, (3) Amps */
status = viPrintf(instr, "F%d", X);

Here, the variable X corresponds to the type of measurement denoted by a number matching the function number for the instrument. Without formatted I/O, the result would have been either:

sprintf(buffer, "F%d", X);
viWrite(instr, buffer, strlen(buffer), &retCount);

or

switch(X) {

case 1:

viWrite(instr, "F1", 2, &retCount);
break;

case 2:

viWrite(instr, "F2", 2, &retCount); break;

.
.

}

In addition, there is an operation viQueryf() that combines the functionality of a viPrintf() followed by a viScanf() operation. viQueryf() is used to query the device for information:

status = viQueryf(instr,"*IDN?\n","%s",buf);