Main Content

Communicate with Your Instrument

Instrument Control Session Examples

Each example illustrates a typical instrument control session. The instrument control session comprises all the steps you are likely to take when communicating with a supported instrument. You should keep these steps in mind when constructing your own instrument control applications.

The examples also use specific instrument addresses, SCPI commands, and so on. If your instrument requires different parameters, or if it does not support the SCPI language, you should modify the examples accordingly. For more information, see Using SCPI Commands.

If you want detailed information about any functions that are used, refer to the functions documentation. If you want detailed information about any properties that are used, refer to the properties documentation.

Communicating with a GPIB Instrument

This example illustrates how to communicate with a GPIB instrument. The GPIB controller is a National Instruments™ AT-GPIB card. The instrument is a Keysight® 33120A Function Generator, which is generating a 2 volt peak-to-peak signal.

You should modify this example to suit your specific instrument control application needs. If you want detailed information about communicating with an instrument via GPIB, refer to GPIB Overview.

  1. Create an interface object — Create the GPIB object g associated with a National Instruments GPIB board with board index 0, and an instrument with primary address 1.

    g = gpib('ni',0,1);
  2. Connect to the instrument — Connect g to the instrument.

    fopen(g)
  3. Configure property values — Configure g to assert the EOI line when the line feed character is written to the instrument, and to complete read operations when the line feed character is read from the instrument.

    g.EOSMode = 'read&write'
    g.EOSCharCode = 'LF'
  4. Write and read data — Change the instrument's peak-to-peak voltage to three volts by writing the Volt 3 command, query the peak-to-peak voltage value, and then read the voltage value.

    fprintf(g,'Volt 3')
    fprintf(g,'Volt?')
    data = fscanf(g)
    data =
    +3.00000E+00
  5. Disconnect and clean up — When you no longer need g, you should disconnect it from the instrument, remove it from memory, and remove it from the MATLAB® workspace.

    fclose(g)
    delete(g)
    clear g

Communicating with a GPIB-VXI Instrument

This example illustrates how to communicate with a VXI instrument via a GPIB controller using the VISA standard provided by Keysight.

The GPIB controller is a Keysight E1406A command module in VXI slot 0. The instrument is a Keysight E1441A Function/Arbitrary Waveform Generator in VXI slot 1, which is outputting a 2 volt peak-to-peak signal. The GPIB controller communicates with the instrument over the VXI backplane.

You should modify this example to suit your specific instrument control application needs. If you want detailed information about communicating with an instrument using VISA, refer to Get Started with VISA.

  1. Create an instrument object — Create the VISA-GPIB-VXI object v associated with the E1441A instrument located in chassis 0 with logical address 80.

    v = visa('keysight','GPIB-VXI0::80::INSTR');
  2. Connect to the instrument — Connect v to the instrument.

    fopen(v)
  3. Configure property values — Configure v to complete a read operation when the line feed character is read from the instrument.

    v.EOSMode = 'read'
    v.EOSCharCode = 'LF'
  4. Write and read data — Change the instrument's peak-to-peak voltage to three volts by writing the Volt 3 command, query the peak-to-peak voltage value, and then read the voltage value.

    fprintf(v,'Volt 3')
    fprintf(v,'Volt?')
    data = fscanf(v)
    data =
    +3.00000E+00
  5. Disconnect and clean up — When you no longer need v, you should disconnect it from the instrument, remove it from memory, and remove it from the MATLAB workspace.

    fclose(v)
    delete(v)
    clear v

Communicating with a Serial Port Instrument

This example illustrates how to communicate with an instrument via the serial port. The instrument is a Tektronix® TDS 210 two-channel digital oscilloscope connected to the serial port of a PC, and configured for a baud rate of 4800 and a carriage return (CR) terminator.

You should modify this example to suit your specific instrument control application needs. If you want detailed information about communicating with an instrument connected to the serial port, refer to Serial Port Overview.

Note

This example is Windows® specific.

  1. Create an instrument object — Create the serial port object s associated with the COM1 serial port.

    s = serial('COM1');
  2. Configure property values — Configure s to match the instrument's baud rate and terminator.

    s.BaudRate = 4800
    s.Terminator = 'CR'
  3. Connect to the instrument — Connect s to the instrument. This step occurs after property values are configured because serial port instruments can transfer data immediately after the connection is established.

    fopen(s)
  4. Write and read data — Write the *IDN? command to the instrument and then read back the result of the command. *IDN? queries the instrument for identification information.

    fprintf(s,'*IDN?')
    out = fscanf(s)
    out =
    TEKTRONIX,TDS 210,0,CF:91.1CT FV:v1.16 TDS2CM:CMV:v1.04
  5. Disconnect and clean up — When you no longer need s, you should disconnect it from the instrument, remove it from memory, and remove it from the MATLAB workspace.

    fclose(s)
    delete(s)
    clear s

Communicating with a GPIB Instrument Using a Device Object

This example illustrates how to communicate with a GPIB instrument through a device object. The GPIB controller is a Measurement Computing™ card, and the instrument is a Keysight 33120A Function Generator, which you set to produce a 1 volt peak-to-peak sine wave at 1000 Hz. Device objects use instrument drivers; this example uses the driver agilent_33120a.mdd.

You should modify this example to suit your specific instrument control application needs. If you want detailed information about communicating through device objects, see Device Objects.

  1. Create instrument objects — Create the GPIB object g associated with a Measurement Computing GPIB board with board index 0, and an instrument with primary address 4. Then create the device object d associated with the interface object g, and with the instrument driver agilent_33120a.mdd.

    g = gpib('mcc',0,4);
    d = icdevice('agilent_33120a.mdd',g);
  2. Connect to the instrument — Connect d to the instrument.

    connect(d)
  3. Call device object method — Use the devicereset method to set the generator to a known configuration. The behavior of the generator for this method is defined in the instrument driver.

    devicereset(d)
  4. Configure property values — Configure d to set the amplitude and frequency for the signal from the function generator.

    d.Amplitude = 1.00
    d.AmplitudeUnits = 'vpp'
    d.Frequency = 1000
  5. Disconnect and clean up — When you no longer need d and g, you should disconnect from the instrument, remove the objects from memory, and remove them from the MATLAB workspace.

    disconnect(d)
    delete([d g])
    clear d g