User`s manual

8 Serial Port I/O
8-48
Events and Callbacks
You can enhance the power and flexibility of your serial port application by
using events. An event occurs after a condition is met and may result in one or
more callbacks.
While the serial port object is connected to the device, you can use events to
display a message, display data, analyze data, and so on. Callbacks are
controlled through callback properties and callback functions. All event types
have an associated callback property. Callback functions are M-file functions
that you construct to suit your specific application needs.
You execute a callback when a particular event occurs by specifying the name
of the M-file callback function as the value for the associated callback property.
Example: Introduction to Events and Callbacks
This example uses the M-file callback function instrcallback to display a
message to the command line when a bytes-available event occurs. The event
is generated when the terminator is read.
s = serial('COM1');
fopen(s)
s.BytesAvailableFcnMode = 'terminator';
s.BytesAvailableFcn = @instrcallback;
fprintf(s,'*IDN?')
out = fscanf(s);
The resulting display from instrcallback is shown below.
BytesAvailable event occurred at 17:01:29 for the object:
Serial-COM1.
End the serial port session.
fclose(s)
delete(s)
clear s
You can use the type command to display instrcallback at the command line.