External Interfaces/API |
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 might 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.
You can use the type
command to display instrcallback
at the command line.
Example: Reading Binary Data | Event Types and Callback Properties |