(To be removed) Read data asynchronously from device
readasync(obj) initiates an asynchronous
read operation on the serial port object,
readasync(obj,size) asynchronously reads,
at most, the number of bytes given by
size is greater than the difference between the
InputBufferSize property value and the
BytesAvailable property value, an error is returned.
This example creates the serial port object
s on a Windows® platform. It connects
s to a Tektronix® TDS 210 oscilloscope, configures
s to read data
asynchronously only if
readasync is issued, and configures the
instrument to return the peak-to-peak value of the signal on channel 1.
s = serial('COM1'); fopen(s) s.ReadAsyncMode = 'manual'; fprintf(s,'Measurement:Meas1:Source CH1') fprintf(s,'Measurement:Meas1:Type Pk2Pk') fprintf(s,'Measurement:Meas1:Value?')
Begin reading data asynchronously from the instrument using
readasync. When the read operation is complete, return the
data to the MATLAB® workspace using
ans = 15
out = fscanf(s)
out = 2.0399999619E0
Before you can read data, you must connect
obj to the device with
fopen function. A connected serial port object has a
Status property value of
open. An error
is returned if you attempt to perform a read operation while
is not connected to the device.
readasync to configure the
ReadAsyncMode property to
readasync is ignored if used when
TransferStatus property indicates if an asynchronous read
or write operation is in progress. You can write data while an asynchronous read is
in progress because serial ports have separate read and write pins. You can stop
asynchronous read and write operations with the
You can monitor the amount of data stored in the input buffer with the
BytesAvailable property. Additionally, you can use the
BytesAvailableFcn property to execute a callback function
when the terminator or the specified amount of data is read.
Rules for Completing an Asynchronous Read Operation
An asynchronous read operation with
when one of these conditions is met:
The terminator specified by the
Terminatorproperty is read.
The time specified by the
The specified number of bytes is read.
The input buffer is filled (if
sizeis not specified).
readasync checks for the terminator, this function
can be slow. To increase speed, you might want to configure
continuously return data to the input buffer as soon as it is available from the
Version HistoryIntroduced before R2006a
serial object interface will be removed
Use of this function with a
serial object will be removed. To access a
serial port device, use a
serialport object with its functions and properties
The recommended functionality has additional capabilities and improved performance. See Transition Your Code to serialport Interface for more information about using the recommended functionality.