Main Content

Create Listeners for Target Computer Events

This example shows how to define MATLAB® listeners for events that occur on a Simulink® Real-Time™ target computer. The listeners detect events through a Target object that represents the target computer. The Target object events monitored by listeners in this example include:

  • Target computer connected

  • Target computer disconnected

  • Target computer loaded with real-time application

  • Target computer started running real-time application

  • Target computer stopped running real-time application

Open Model and Build Real-Time Application

To show how to work with events that relate to a target object and a real-time application, this example uses the model slrt_ex_osc.

model = 'slrt_ex_osc';

Create Target Object and Event Listeners

This code creates a Target object to represent the target computer and defines MATLAB listeners for some of the target computer events.

tg = slrealtime;
listenerConnected = listener(tg,'Connected',@(~,~)disp('Connected to target computer'));
listenerDisconnected = listener(tg,'Disconnected',@(~,~)disp('Disconnected from target computer'));
listenerLoaded = listener(tg,'Loaded',@(~,~)disp('Loaded application on target computer'));
listenerStarted = listener(tg,'Started',@(~,~)disp('Started application on target computer'));
listenerStopped = listener(tg,'Stopped',@(~,~)disp('Stopped application on target computer'));

Observe Listener Messages from Target Object Events

This code executes a series of target computer operations with pauses between the operations to provide time to observe the event status messages.

Connected to target computer
Loaded application on target computer
Started application on target computer
Stopped application on target computer
Disconnected from target computer

Run Script to Observe Listener Operation

To observe the operation of the listeners for the Target object events, run the script.

run CreateListenersForTargetComputerEventsExample

More About Target Object Events

To list the available Target object events, use the events(tg) function. For more information about target computer events, see the Target object events list. For more information about MATLAB listeners, see Overview Events and Listeners.