This example explores ASCII read and write operations with a VISA object. The instrument used was a Tektronix® TDS 210 oscilloscope.
The VISA object supports seven interfaces: serial, GPIB, VXI, GPIB-VXI, TCPIP, USB, and RSIB. This example explores ASCII read and write operations using a VISA-GPIB object. However, ASCII read and write operations for VISA-GPIB, VISA-VXI, VISA-GPIB-VXI, VISA-TCPIP, VISA-SERIAL, and VISA-USB objects are identical to each other. Therefore, you can use the same commands. The only difference is the resource name specified in the VISA constructor.
ASCII read and write operations for the VISA-serial object are identical to ASCII read and write operations for the serial port object. Therefore, to learn how to perform ASCII read and write operations for the VISA-serial object, you should refer to the Serial Port ASCII Read/Write tutorial.
ASCII read and write operations for the VISA-RSIB object are identical to the ASCII read and write operations for the VISA-GPIB, VISA-VXI, VISA-GPIB-VXI, VISA-TCPIP, and VISA-USB objects, except the VISA-RSIB object does not support the EOSCharCode and EOSMode properties.
These functions are used when reading and writing text:
Function | Purpose |
---|---|
fprintf | Write text to an instrument. |
fscanf | Read data from an instrument and format as text. |
These properties are associated with reading and writing text:
Property | Purpose |
---|---|
ValuesReceived | Specifies the total number of values read from the instrument. |
ValuesSent | Specifies the total number of values sent to the instrument. |
InputBufferSize | Specifies the total number of bytes that can be queued in the input buffer at one time. |
OutputBufferSize | Specifies the total number of bytes that can be queued in the output buffer at one time. |
EOSMode | Configures the End-Of-String termination mode. |
EOSCharCode | Specifies the End-Of-String terminator. |
EOIMode | Enables or disables the assertion of the EOI mode at the end of a write operation. |
You need to create a VISA-GPIB object. In this example, an object is created using
the ni
driver and the VISA resource string shown below.
v = visa('ni', 'GPIB0::2::INSTR');
Before you can perform a read or write operation, you must connect the VISA-GPIB
object to the instrument with the fopen
function.
fopen(v);
If the object was successfully connected, its Status
property
is automatically configured to open
.
v.Status ans = open
You use the fprintf
function to write ASCII data to the
instrument. For example, the'Display:Contrast'
command will
change the display contrast of the oscilloscope.
fprintf(v, 'Display:Contrast 45');
By default, the fprintf
function operates in a synchronous
mode. This means that fprintf
blocks the MATLAB® command line until one of the following occurs:
All the data is written
A timeout occurs as specified by the Timeout
property
By default the fprintf
function writes ASCII data using the
%s\n
format. You can also specify the format of the command
written by providing a third input argument to fprintf
. The
accepted format conversion characters include: d, i, o, u, x, X, f, e, E, g, G, c,
and s. For example:
fprintf(v, '%s', 'Display:Contrast 45');
OutputBufferSize
The OutputBufferSize
property specifies the maximum number of
bytes that can be written to the instrument at once. By default,
OutputBufferSize
is 512
.
v.OutputBufferSize ans = 512
If the command specified in fprintf
contains more than 512
bytes, an error is returned and no data is written to the instrument.
EOIMode, EOSMode, and EOSCharCode
By default, the End or Identify (EOI) line is asserted when the last byte is
written to the instrument. This behavior is controlled by the
EOIMode
property. When EOIMode
is set to
on
, the EOI line is asserted when the last byte is written to
the instrument. When EOIMode
is set to off
,
the EOI line is not asserted when the last byte is written to the instrument.
All occurrences of \n
in the command written to the instrument
are replaced with the EOSCharCode
property value if
EOSMode
is set to write
or
read&write
.
ValuesSent
The ValuesSent
property is updated by the number of values
written to the instrument. Note that by default EOSMode
is set to
none. Therefore, EOSCharCode
is not sent as the last byte of the
write.
fprintf(v, 'Display:Contrast 45'); v.ValuesSent ans = 57
Clear any data in the input buffer before moving to the next step.
flushinput(v);
You use the fscanf
function to read ASCII data from the
instrument. For example, the oscilloscope command
'Display:Contrast?'
returns the oscilloscope's display
contrast:
fprintf(v, 'Display:Contrast?'); data = fscanf(v) data = 45
fscanf
blocks until one of the following occurs:
The EOI line is asserted
The terminator is received as specified by the
EOSCharCode
property
A timeout occurs as specified by the Timeout
property
The input buffer is filled
The specified number of values is received
By default, the fscanf
function reads data using the
'%c'
format. You can also specify the format of the data read
by providing a second input argument to fscanf
. The accepted
format conversion characters include: d, i, o, u, x, X, f, e, E, g, G, c, and s. For
example, the following command will return the voltage as a decimal:
fprintf(v, 'Display:Contrast?'); data = fscanf(v, '%d') data = 45 isnumeric(data) ans = 1
InputBufferSize
The InputBufferSize
property specifies the maximum number of
bytes you can read from the instrument. By default,
InputBufferSize
is 512
.
v.InputBufferSize ans = 512
ValuesReceived
The ValuesReceived
property indicates the total number of
values read from the instrument. Note the last value received is a linefeed.
fprintf(v, 'Display:Contrast?'); data = fscanf(v) data = 45 v.ValuesReceived ans = 9
EOSMode and EOSCharCode
To terminate the data transfer based on receiving EOSCharCode
,
you should set the EOSMode
property to read
or
read&write
and the EOSCharCode
property to the ASCII code for which the read operation should terminate. For
example, if you set EOSMode
to read
and
EOSCharCode
to 10
, then one of the ways
that the read terminates is when the linefeed character is received.
The standard response to the vertical gain query is in scientific notation.
fprintf(v, 'CH1:Scale?') data = fscanf(v) data = 1.0E0
Now configure the VISA-GPIB object to terminate the read operation when the 'E' character is received. The first read terminates when the 'E' character is received.
set(v, 'EOSMode', 'read') set(v, 'EOSCharCode', double('E')) fprintf(v, 'CH1:Scale?') data = fscanf(v) data = 1.0E
If you perform a second read operation, it terminates when the EOI line is asserted.
data = fscanf(v) data = 0
If you are finished with the VISA-GPIB object, disconnect it from the instrument, remove it from memory, and remove it from the workspace.
fclose(v); delete(v); clear v