MATLAB Answers

How to programatically modify simulink mask values (Serial Configuration Block)

3 views (last 30 days)
I want to automate the process of setting the serial port configurations of a simulink file. In order to do this i have written a file that checks for the correct com port and do the setup accordingly. In simulink i execute the following set of commands in the 'postLoadFunction' callback of the model:
if exist('com_port_string', 'var')
disp('Automatically setting COM port');
model_name = bdroot;
sc_block = find_system(model_name,'Name','Serial Configuration');
sr_block = find_system(model_name,'Name','Serial Receive');
sc_mask = Simulink.Mask.get(sc_block);
sr_mask = Simulink.Mask.get(sr_block);
% Change ComPort mask parameter
sc_mask.Parameters(2).Value = com_port_string;
sr_mask.Parameters(2).Value = com_port_string;
sc_blockObject = get_param(sc_block,'object');
sr_blockObject = get_param(sr_block,'object');
instrumentslcallback(sc_blockObject, com_port_string, 'ComPortMenu')
instrumentslcallback(sr_blockObject, com_port_string, 'ComPortMenu')
clear parent sc_block sc_mask sr_block sr_mask sc_blockObject sr_blockObject
disp('External mode COM port is not yet known...')
I've also tried different approaches using a masked empty subsystem and putting this code in the 'initFunction' callback. The problem is that the mask parameters of the serial block are changed correctly but not applied (even if i modify the parameter using the set_param(-) function, when running the simulation it throws an error because simulink tries to open the wrong com port not the one displayed in the mask ). Is there any way to apply the parameters of the mask programmatically (i assume the problem is that my code doesn't call the callback function of the apply button, but don't know how to call it)?


Sign in to comment.

Answers (1)

Sayyed Ahmad
Sayyed Ahmad on 10 Jul 2019
I used the following codes in a matlab level-2 s-function and It's worked.
set_param(gcs,'BackgroundColor', 'red');

  1 Comment

Emmanuel Santiago Iglesias
Hello Sayyed,
thanks for the quick answer! Yes, whenever i use the function:
the mask parameter changes and sets COM5 for the serial port, but when i run the simulation and it tries to connect with the device, the error comes that it could not connect to COM1 (the com port that was configured when opening the simulink model). Then i have to manually double click on the Serial Receive Block and click on "Apply" in order for the block to really set the serial port to COM5.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by