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
Create Target Object and Connect
This code creates a Target
object to represent the target computer and connects to the target computer. In the Command Window, type:
tg = slrealtime; connect(tg);
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'; open_system(model); modelSTF = getSTFName(tg); set_param(model,"SystemTargetFile",modelSTF);
Build Real-Time Application
To build the model, in the Command Window, type:
evalc('slbuild(model)');
Create Target Object and Event Listeners
This code defines MATLAB listeners for some of the target computer events.
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.
load(tg,model); start(tg); stop(tg); disconnect(tg);
Stopped application on target computer Loaded application on target computer Started application on target computer Importing Log file 1 of 1 ... 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.
bdclose(model);