User`s manual
8 Serial Port I/O
8-54
Example: Using Events and Callbacks
This example uses the M-file callback function instrcallback to display
event-related information to the command line when a bytes-available event or
an output-empty event occurs.
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 Tektronix TDS 210 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.
fopen(s)
3. Configure properties – Configure s to execute the callback function
instrcallback when a bytes-available event or an output-empty event occurs.
Since
instrcallback requires the serial port object and event information to
be passed as input arguments, the callback function is specified as a function
handle.
s.BytesAvailableFcnMode = 'terminator';
s.BytesAvailableFcn = @instrcallback;
s.OutputEmptyFcn = @instrcallback;
4. Write and read data – Write the RS232? command asynchronously to the
oscilloscope. This command queries the RS-232 settings and returns the baud
rate, the software flow control setting, the hardware flow control setting, the
parity type, and the terminator.
fprintf(s,'RS232?','async')
instrcallback
is called after the RS232? command is sent, and when the
terminator is read. The resulting displays are shown below.
OutputEmpty event occurred at 17:37:21 for the object:
Serial-COM1.
BytesAvailable event occurred at 17:37:21 for the object:
Serial-COM1.