| Contents | Index |
| On this page… |
|---|
Asynchronous Write and Read Operations |
Asynchronous write and read operations do not block access to the MATLAB Command Window. Additionally, while an asynchronous operation is in progress you can
Execute a read (write) operation while an asynchronous write (read) operation is in progress. This is because serial ports have separate pins for reading and writing.
Make use of all supported callback properties. Refer to Events and Callbacks for more information about the callback properties supported by serial port objects.
The process of writing data asynchronously is given in Synchronous Versus Asynchronous Write Operations.
For a general overview about writing and reading data, as well as a list of all associated functions and properties, refer to Communicating with Your Instrument.
For serial port objects, you specify whether read operations are synchronous or asynchronous with the ReadAsyncMode property. You can configure ReadAsyncMode to continuous or manual.
If ReadAsyncMode is continuous (the default value), the serial port object continuously queries the instrument to determine if data is available to be read. If data is available, it is asynchronously stored in the input buffer. To transfer the data from the input buffer to the MATLAB workspace, you use one of the synchronous (blocking) read functions such as fgetl, fgets, fscanf, or fread. If data is available in the input buffer, these functions will return quickly.
s = serial('COM1');
fopen(s)
s.ReadAsyncMode = 'continuous';
fprintf(s,'*IDN?')
s.BytesAvailable
ans =
56
out = fscanf(s);If ReadAsyncMode is manual, the serial port object does not continuously query the instrument to determine if data is available to be read. To read data asynchronously, you use the readasync function. You then use one of the synchronous read functions to transfer data from the input buffer to the MATLAB workspace.
s.ReadAsyncMode = 'manual';
fprintf(s,'*IDN?')
s.BytesAvailable
ans =
0
readasync(s)
s.BytesAvailable
ans =
56
out = fscanf(s);The rules for completing synchronous and asynchronous read and write operations are described below.
A write operation using fprintf or fwrite completes when one of these conditions is satisfied:
The specified data is written.
The time specified by the Timeout property passes.
In addition to these rules, you can stop an asynchronous write operation at any time with the stopasync function.
A text command is processed by the instrument only when it receives the required terminator. For serial port objects, each occurrence of \n in the ASCII command is replaced with the Terminator property value. Because the default format for fprintf is %s\n, all commands written to the instrument will end with the Terminator value. The default value of Terminator is the line feed character. The terminator required by your instrument will be described in its documentation.
A read operation with fgetl, fgets, fscanf, or readasync completes when one of these conditions is satisfied:
The terminator specified by the Terminator property is read.
The time specified by the Timeout property passes.
The input buffer is filled.
The specified number of values is read (fscanf and readasync only).
A read operation with fread completes when one of these conditions is satisfied:
The time specified by the Timeout property passes.
The specified number of values is read.
Note Set the terminator property to '' (null), if appropriate, to ensure efficient throughput of binary data. |
In addition to these rules, you can stop an asynchronous read operation at any time with the stopasync function.
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.
Create a serial port object — Create the serial port object s associated with serial port COM1.
s = serial('COM1');Connect to the instrument — Connect s to the oscilloscope. Because 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.
fopen(s)
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(s,'*IDN?')
s.BytesAvailable
ans =
56
idn = fscanf(s)
idn =
TEKTRONIX,TDS 210,0,CF:91.1CT FV:v1.16 TDS2CM:CMV:v1.04You need to determine the measurement source. Possible measurement sources include channel 1 and channel 2 of the oscilloscope.
fprintf(s,'MEASUREMENT:IMMED:SOURCE?') source = fscanf(s) source = CH1
The scope is configured to return a measurement from channel 1. Because the input signal is connected to channel 2, you must configure the instrument to return a measurement from this channel.
fprintf(s,'MEASUREMENT:IMMED:SOURCE CH2') fprintf(s,'MEASUREMENT:IMMED:SOURCE?') source = fscanf(s) source = CH2
You can now configure the scope to return the peak-to-peak voltage, and then request the value of this measurement.
fprintf(s,'MEASUREMENT:MEAS1:TYPE PK2PK') fprintf(s,'MEASUREMENT:MEAS1:VALUE?')
Transfer data from the input buffer to the MATLAB workspace using fscanf.
ptop = fscanf(s) ptop = 2.0199999809E0
Disconnect and clean up — When you no longer need s, you should disconnect it from the instrument, and remove it from memory and from the MATLAB workspace.
fclose(s) delete(s) clear s
![]() | Configuring Communication Settings | Events and Callbacks | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |