User`s manual

8 Serial Port I/O
8-42
s.BytesAvailable
ans =
69
You can return the data to MATLAB using any of the synchronous read
functions. However, if you use
fgetl, fgets, or fscanf, then you must issue the
function twice since there are two terminators stored in the input buffer. If you
use
fread, then you can return all the data to MATLAB in one function call.
out = fread(s,69);
By default, fread returns numerical values in double precision arrays.
However, you can specify many other precisions as described in the
fread
reference pages. You can convert the numerical data to text using MATLAB’s
char function.
val = char(out)'
val =
HBARS;CH1;SECONDS;-1.0E-3;1.0E-3;VOLTS;-6.56E-1;6.24E-1
YT;DOTS;0;45
For more information about synchronous and asynchronous read operations,
refer to “Reading Text Data” on page 8-39. For a description of the rules used
by
fread to complete a read operation, refer to its reference pages.
Example: Writing and Reading Text Data
This example illustrates how to communicate with a serial port instrument by
writing and reading text data.
The instrument is a Tektronix TDS 210 two-channel oscilloscope connected to
the serial port COM1. Therefore, many of the commands given below 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 a serial port object – Create the serial port object s associated with
serial port COM1.
s = serial('COM1');
2. Connect to the device – Connect s to the oscilloscope. Since the default
value for the
ReadAsyncMode property is continuous, data is asynchronously
returned to the input buffer as soon as it is available from the instrument.