Main Content

Communicating with Instruments

Configuring Instrument Settings

Once a device object has been created and connected, it can be used as the interface to an instrument. This chapter shows you how to access and configure your instrument's settings, as well as how to read and write data to the instrument.

Every device object contains properties specific to the instrument it represents. These properties are defined by the instrument driver used during device object creation. For example, there may be properties for an oscilloscope that allow you to adjust trigger parameters, or the contrast of the screen display.

Properties are assigned default values at device object creation. On execution of connect the object is updated to reflect the state of the instrument or vice versa, depending on the second argument given to connect.

You can obtain a full listing of configurable properties by calling the set command and passing the device object.

Configuring Settings on an Oscilloscope

This example illustrates how to configure an instrument using a device object.

The instrument used is a Tektronix® TDS 210 two-channel oscilloscope. A square wave is input into channel 1 of the oscilloscope. The task is to adjust the scope's settings so that triggering occurs on the falling edge of the signal:

  1. Create the device object — Create a GPIB interface object, and then a device object for a TDS 210 oscilloscope.

    g = gpib('ni',0,1);
    d = icdevice('tektronix_tds210', g);
  2. Connect the device object — Use the connect function to connect the device object to the instrument.

    connect(d);
  3. Check the current Slope settings for the Trigger property— Create a variable to represent the Trigger property and then use the get function to obtain the current value for the oscilloscope Slope setting.

    dtrigger = get(d, 'Trigger');
    dtrigger.Slope
    ans =
    
        rising
    

    The Slope is currently set to rising.

  4. Change the Slope setting — If you want triggering to occur on the falling edge, you need to modify that setting in the device object. This can be accomplished with the set command.

    dtrigger.Slope = 'falling');

    This changes Slope to falling.

  5. Disconnect and clean up — When you no longer need the device object, disconnect it from the instrument and remove it from memory. Remove the device object and interface object from the MATLAB® workspace.

    disconnect(d);
    delete(d);
    clear d g dtrigger;

Calling Device Object Methods

Device objects contain methods specific to the instruments they represent. Implementation details are hidden behind a single function. Instrument-specific functions are defined in the MATLAB instrument driver.

The methods function displays all available driver-defined functions for the device object. The display is divided into two sections:

  • Generic object functions

  • Driver-specific object functions

To view the available methods, type

methods(obj)

Use the instrhelp function to get help on the device object functions.

instrhelp(obj, methodname);

To call instrument-specific methods you use the invoke function. invoke requires the device object and the name of the function. You must also provide input arguments, when appropriate. The following example demonstrates how to use invoke to obtain measurement data from an oscilloscope.

Using Device Object Functions

This example illustrates how to call an instrument-specific device object function. Your task is to obtain the frequency measurement of a waveform. The instrument is a Tektronix TDS 210 two-channel oscilloscope.

The scope has been preconfigured with a square wave input into channel 1 of the oscilloscope. The hardware supports four different measurements: frequency, mean, period, and peak-to-peak. The requested measurement is signified with the use of an index variable from 1 to 4.

For demonstration purposes, the oscilloscope in this example has been preconfigured with the correct measurement settings:

  1. Create the device object — Create a GPIB interface object and a device object for the oscilloscope.

    g = gpib('ni',0,1);
    d = icdevice('tektronix_tds210', g);
  2. Connect the device object — Use the connect command to open the GPIB object and update the settings in the device object.

    connect(d);
  3. Obtain the frequency measurement — Use the invoke command and call measure. The measure function requires that an index parameter be specified. The value of the index specifies which measurement the oscilloscope should return. For the current setup of the Tektronix TDS 210 oscilloscope, an index of 1 indicates that frequency is to be measured.

    invoke(d, 'measure', 1)
    
    ans =
    
        999.9609

    The frequency returned is 999.96 Hz, or nearly 1 kHz.

  4. Disconnect and clean up — You no longer need the device object so you can disconnect it from the instrument. You should also delete it from memory and remove it from the MATLAB workspace.

    disconnect(d);
    delete(d);
    clear d g;

Control Commands

Control commands are special functions and properties that exist for all device objects. You use control commands to identify an instrument, reset hardware settings, perform diagnostic routines, and retrieve instrument errors. The set of control commands consists of

All control commands are defined within the MATLAB instrument driver for your device.

InstrumentModel

InstrumentModel is a device object property. When queried, the instrument identification command is sent to the instrument.

For example, for a Tektronix TDS 210 oscilloscope,

d.InstrumentModel

ans =

TEKTRONIX,TDS 210,0,CF:91.1CT FV:v2.03 TDS2MM:MMV:v1.04

devicereset

To restore the factory settings on your instrument, use the devicereset function. When devicereset is called, the appropriate reset instruction is sent to your instrument.

The command accepts a connected device object and has no output arguments.

devicereset(obj);

selftest

This command requests that your instrument perform a self-diagnostic. The actual operations performed and output arguments are specific to the instrument your device object is connected to. selftest accepts a connected device object as an input argument.

result = selftest(obj);

geterror

You can retrieve error messages generated by your instrument with the geterror function. The returned messages are instrument specific. geterror accepts a connected device object as an input argument.

msg = geterror(obj);