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

10 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)?

Answers (2)

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.

Exeedo on 15 Apr 2021
Hello Emmanuel,
I had the same problem as you do. By setting the ComPort paramter to 'COM5', it only appeared on Serial Configuration and other blocks without it being attached to these blocks. After some trials, I was finally able to know what the problem is.
These blocks have 2 hidden parameters that need to be edited. The first one is called ComPort, which will take a value of "COM5" for example. This value will appear on the block name and, by default, would come from the [visible] popup ComPortMenu. The second one is called ObjConstructor, which will take the value of "serial('COM5');" as our example here. By modifying both parameters to the desired value, you can modify the communication port programmatically in a correct way. So the code should look like this:
I was trying to promote the ComPortMenu parameter to a mask I created when I saw these hidden parameters and tried them out. Hope this will help you.
  1 Comment
Exeedo on 28 Apr 2021
I should note that I was working on R2020b for the solution that I mentioned here.
After trying R2021a, this problem was not an issue anymore. All serial blocks do not have the ObjConstructor parameter anymore. Setting the port value is enough to change the actual communication port used in that block, along with the display on the block itself.

Sign in to comment.


Find more on Event Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by