Azzera filtri
Azzera filtri

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

15 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 10 Lug 2024 alle 0:00
Modificato: MathWorks Support Team il 10 Lug 2024 alle 20:55
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 for your release, use the events(tg) function:
>> tg = slrealtime;
>> events(tg)
Refer to our documentation for a summary of target events and a basic code example that can be used in a MATLAB script.
 

New R2024a Features for Adding Target Event Listeners to an App:

In R2024a, two new features were added to ease adding target event listeners to SLRT apps:
  • The Target Events button in the SLRT App Generator lets you add callback code for target computer events by using the Callback Editor and outputs your event callback code in the generated app. For more information, see Add Callbacks for Target Computer Events.
  • The slrealtime.ui.tool.EventManager manages event listeners in your app and can be linked with a Target Selector to switch the listeners to a new target when the user makes a selection in the drop-down.
.

Code Example for Adding Target Event Listeners to an App:

To add event listener code to your app in previous versions, or add it manually, 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 App Building 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