How to programatically disconnect from Target Hardware inside Simulink?

9 次查看(过去 30 天)
I know already how control various Simulink functionalities from Matlab Command windows. What I want is to execute those functionality inside Simulink. Lets I want to disconnect from target hardware, I need to press the Connect/Disconnect button in 'External Mode Control Panel' or execute a command
'set_param(gcs,'SimulationCommand','connect')'
from command window.
But I don't want either of the above. I want to execute this line of code inside Simulink somehow or the equivalent of this function.
i.e Press Connect Button (Push Button) which will connect hardware with the target.

回答(1 个)

Ayush Singh
Ayush Singh 2024-7-17,10:00
Hey cody,
To achieve the desired workflow given by you where you do not want to execute from command window or click Button in Simulink tooltip, you can use a combination of MATLAB Function blocks and callbacks. Specifically, you can create a custom block (such as a Push Button) that triggers a MATLAB function to execute the desired command.
Below are the steps to achieve this
  • Create a MATLAB Function Block
1. Open your Simulink model.
2. Drag a "MATLAB Function" block from the Simulink library into your model.
  • Define the MATLAB Function
1. Double-click the MATLAB Function block to open the editor.
2. Define a function that executes the desired command (`set_param(gcs,'SimulationCommand','connect')` in this case).
function connectToHardware()
coder.extrinsic('set_param');
set_param(gcs,'SimulationCommand','connect');
end
  • Create a Push Button Block
1. Drag a "Push Button" block from the Simulink library (found under Simulink > Dashboard) into your model.
2. Name the Push Button block (e.g., `Connect`).
  • Configure the Push Button Block
1. Right-click the Push Button block and select "Properties".
2. In the Properties dialog, set the `Tag` property to a unique identifier (e.g., `ConnectTag`).
  • Add a Callback to the Push Button
1. In the Simulink model, go to the "Model Properties" (right-click on the model background and select "Model Properties").
2. In the "Callbacks" tab, add a callback for the "StartFcn" or "InitFcn" (whichever suits your needs).
% Add the following code to the callback
add_exec_event_listener('ConnectTag', 'PostSet', @connectToHardwareCallback);
function connectToHardwareCallback(~, ~)
connectToHardware();
end
NOTE:
Make sure the `Tag` property of the Push Button block matches the tag used in the `add_exec_event_listener` function.

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by