Main Content

configureCallback

Set callback function and trigger condition for communication with serial port device

Description

configureCallback(device,"terminator",callbackFcn) sets the callback function callbackFcn to trigger whenever a terminator is available to be read from the specified serial port. The syntax sets the BytesAvailableFcnMode property of device to "terminator" and the BytesAvailableFcn property to callbackFcn.

Set the terminator character using configureTerminator.

example

configureCallback(device,"byte",count,callbackFcn) sets the callback function callbackFcn to trigger whenever a new count number of bytes are available to be read. The syntax sets the BytesAvailableFcnMode property of device to "byte", the BytesAvailableFcnCount property to count, and the BytesAvailableFcn property to callbackFcn.

example

configureCallback(device,"off") turns off callbacks. The syntax sets the BytesAvailableFcnMode property of device to "off".

example

Examples

collapse all

Create a connection to a serial port device.

device = serialport("COM3",9600)
device = 

  Serialport with properties:

                 Port: "COM3"
             BaudRate: 9600
    NumBytesAvailable: 0

  Show all properties, functions

Set the callback to trigger when a terminator is available to be read.

configureCallback(device,"terminator",@callbackFcn)

View the properties to confirm the change.

device.BytesAvailableFcnMode
device.BytesAvailableFcn
ans = 

    "terminator"


ans =

  function_handle with value:

    @callbackFcn

Turn the callback off.

configureCallback(device,"off")

Verify that the callback is off.

device.BytesAvailableFcnMode
ans = 

    "off"

Create a connection to a serial port device.

device = serialport("COM3",9600)
device = 

  Serialport with properties:

                 Port: "COM3"
             BaudRate: 9600
    NumBytesAvailable: 0

  Show all properties, functions

Set the callback to trigger each time 50 new bytes of data are available to be read.

configureCallback(device,"byte",50,@callbackFcn)

View the properties to confirm the change.

device.BytesAvailableFcnMode
device.BytesAvailableFcnCount
device.BytesAvailableFcn
ans = 

    "byte"


ans =

    50


ans =

  function_handle with value:

    @callbackFcn

Turn the callback off.

configureCallback(device,"off")

Verify that the callback is off.

device.BytesAvailableFcnMode
ans = 

    "off"

Create a connection to a serial port device.

device = serialport("COM3",9600)
device = 

  Serialport with properties:

                 Port: "COM3"
             BaudRate: 9600
    NumBytesAvailable: 0

  Show all properties, functions

Create a callback function that reads ASCII terminated string data and saves it to the UserData property of device.

function readSerialData(src,evt)
    data = readline(src);
    src.UserData = data;
end

Set the callback to trigger when a terminator is available to be read.

configureCallback(device,"terminator",@readSerialData)

Input Arguments

collapse all

Serial port connection, specified as a serialport object.

Example: configureCallback(device,"byte",128,@callbackFcn) sets the callbackFcn callback to trigger each time 128 bytes of new data are available to be read from the serial port connection device.

Number of bytes of available data to trigger the callback, specified as a positive integer value. Set the BytesAvailableFcnCount property using this argument.

Example: configureCallback(device,"byte",128,@callbackFcn) sets the callbackFcn callback to trigger each time 128 bytes of new data are available to be read.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Callback function to run when trigger condition is met, specified as a function handle. The function handle can be a named function handle or an anonymous function with input arguments. Set the BytesAvailableFcn property using this argument.

Example: configureCallback(device,"terminator",@callbackFcn) sets the callbackFcn callback to trigger when a terminator is available to be read.

Data Types: function_handle

Version History

Introduced in R2019b

See Also

Functions