Instrument Control Toolbox | ![]() ![]() |
Asynchronous Write and Read Operations
Asynchronous write and read operations do not block access to the MATLAB command line. Additionally, while an asynchronous operation is in progress you can
The process of writing data asynchronously is given in Synchronous Versus Asynchronous Write Operations. The process of reading data asynchronously is described in the next section.
Asynchronous Read Operations
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 MATLAB, 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 MATLAB.
s.ReadAsyncMode = 'manual'; fprintf(s,'*IDN?') s.BytesAvailable ans = 0 readasync(s) s.BytesAvailable ans = 56 out = fscanf(s);
![]() | Writing and Reading Data | Rules for Completing Write and Read Operations | ![]() |