Main Content

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
    NumBytesAvailable: 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.

Confirm the success of the write operation by viewing the NumBytesAvailable property.

v.NumBytesWritten
ans =

    20

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

| | |

Related Topics