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 constructorvisadev
.
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 toWrite 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 thewriteline
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 theTimeout
property of thevisadev
object.
Check the default ASCII terminator.
v.Terminator
ans = "LF"
Thewriteline
function automatically appends the linefeed (LF) terminator to"Display:Contrast 45"
before it is written to the server, indicating the end of the command.
检查的价值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 theEOIMode
property. WhenEOIMode
is set toon
, the EOI line is asserted when the last byte is written to the instrument. WhenEOIMode
is set tooff
, 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 thereadline
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
Thereadline
读取数据,直到它到达terminat函数or, removes the terminator, and returns the data.
You can also use thewriteread
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.
clearv
See Also
visadev
|readline
|writeline
|writeread