Instrument Control Toolbox | ![]() ![]() |
Example: Writing and Reading Text Data
This example illustrates how to communicate with a GPIB instrument by writing and reading text data.
The instrument is a Tektronix TDS 210 two-channel oscilloscope. Therefore, many of the commands used are specific to this instrument. A sine wave is input into channel 2 of the oscilloscope, and your job is to measure the peak-to-peak voltage of the input signal.
1. Create an instrument object - Create the GPIB object g
associated with a National Instruments GPIB controller 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 oscilloscope, and return the default values for the EOSMode
and EOIMode
properties.
fopen(g) get(g,{'EOSMode','EOIMode'}) ans = 'none' 'on'
Using these property values, write operations complete when the last byte is written to the instrument, and read operations complete when the EOI line is asserted by the instrument.
3. Write and read data - Write the *IDN?
command to the instrument using fprintf
, and then read back the result of the command using fscanf
.
fprintf(g,'*IDN?') idn = fscanf(g) idn = TEKTRONIX,TDS 210,0,CF:91.1CT FV:v1.16 TDS2CM:CMV:v1.04
Determine the measurement source. Possible measurement sources include channel 1 and channel 2 of the oscilloscope.
fprintf(g,'MEASUREMENT:IMMED:SOURCE?') source = fscanf(g) source = CH1
The scope is configured to return a measurement from channel 1. Since the input signal is connected to channel 2, you must configure the instrument to return a measurement from this channel.
fprintf(g,'MEASUREMENT:IMMED:SOURCE CH2') fprintf(g,'MEASUREMENT:IMMED:SOURCE?') source = fscanf(g) source = CH2
You can now configure the scope to return the peak-to-peak voltage, request the value of this measurement, and then return the voltage value to MATLAB using fscanf
.
fprintf(g,'MEASUREMENT:MEAS1:TYPE PK2PK') fprintf(g,'MEASUREMENT:MEAS1:VALUE?') ptop = fscanf(g) ptop = 2.0199999809E0
4. Disconnect and clean up - When you no longer need g
, you should disconnect it from the instrument, and remove it from memory and from the MATLAB workspace.
fclose(g) delete(g) clear g
![]() | Rules for Completing Write and Read Operations | Example: Reading Binary Data | ![]() |