write and read ascii data using visa
this example explores ascii read and write operations with a visa object using a tektronix® tds210 oscilloscope.
the visa object supports seven interfaces: serial, gpib, vxi, pxi, usb, serial,
tcp/ip, and socket. this example explores ascii read and write operations using a
visa-gpib object. however, ascii read and write operations for all interfaces are
identical to each other. therefore, you can use the same commands. the only difference
is the resource name specified in the visa constructor
visadev
.
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, refer to write and read serial port data.
connect to instrument
create a visa-gpib object using the visa resource string shown below.
v = visadev("gpib0::2::instr")
v = gpib with properties: resourcename: "gpib0::2::instr" alias: "oscope" vendor: "tektronix" model: "tds 210" boardindex: 0 primaryaddress: 1 secondaryaddress: 0 show all properties, functions
write ascii data
use the writeline
function to write ascii data to the
instrument. for example, the "display:contrast"
command changes
the display contrast of the oscilloscope.
writeline(v,"display:contrast 45")
the function suspends matlab® execution until all the data is written or a timeout occurs as
specified by the timeout
property of the
visadev
object.
check the default ascii terminator.
v.terminator
ans = "lf"
the writeline
function automatically appends the linefeed (lf)
terminator to "display:contrast 45"
before it is written to the
server, indicating the end of the command.
check the value of the eoimode
property. this property is
only available for visa-gpib, visa-vxi, and visa-pxi interfaces.
v.eoimode
ans = onoffswitchstate enumeration on
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.
clear any data in the buffer before moving to the next step.
flush(v)
read ascii data
use the readline
function to read ascii data from the
instrument. for example, the oscilloscope command
"display:contrast?"
returns the oscilloscope's display
contrast.
writeline(v,"display:contrast?")
data = readline(v)
data = 45
the readline
function reads data until it reaches a terminator,
removes the terminator, and returns the data.
you can also use the writeread
function to perform the same
operation. write an ascii command to your instrument and read the response.
data = writeread(v,"display:contrast?")
data = 45
clean up
when you are finished with the visa-gpib object, clear it.
clear v
see also
| | |