How do I add Speedgoat target event listeners and callbacks to an SLRT App?

2 visualizzazioni (ultimi 30 giorni)
I would like to trigger actions in my App Designer app based on Speedgoat target events, for example, when the target is connected, or the real-time application is loaded, started, stopped, etc.
How do I accomplish this for my Simulink Real-Time (SLRT) App?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 30 Gen 2024
The target object has many events the user can listen for, such as when the real-time target is connected, loaded, started, or stopped. 
To list the available target object events, use the events(tg) function:
>> tg = slrealtime;
>> events(tg)
See the following documentation for a basic code example that can be used in a MATLAB script:
The attached "TargetListenerApp.mlapp" (created with R2023a) shows an example of how to listen for 'started' and 'stopped' target events & to add callback code to an SLRT app:
  • Define listeners as private properties and the related callback functions as private methods:
properties (Access = private)
tg slrealtime.Target
listenerStarted event.listener
listenerStopped event.listener
end
methods
function StartedFcn(app, event)
  % add callback code for 'started' event
disp('Simulation started callback')
end
function StoppingFcn(app, event)
% add callback code for 'stopped' event
disp('Simulation stopped callback')
end
end
  • Add listeners to the target object in the startupFnc():
function startupFcn(app)
% <....>
  app.tg = slrealtime;
app.listenerStarted = addlistener(app.tg,'Started',@(~,~)app.StartedFcn);
app.listenerStopped = addlistener(app.tg,'Stopped',@(~,~)app.StoppingFcn);
end
  • Delete listeners in the UIFigureCloseRequest():
function UIFigureCloseRequest(app, event)
delete(app.listenerStarted)
delete(app.listenerStopped)
delete(app)
end
Note that these listeners are attached to whatever the default target is at the time the app is started. Changing the selected target in the Target Selector will have no effect on the listeners. To attach the listeners to a specific target, use app.tg = slrealtime('TargetPCName').

Più risposte (0)

Categorie

Scopri di più su Events in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by