How to programatically modify simulink mask values (Serial Configuration Block)
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
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
else
disp('External mode COM port is not yet known...')
end
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)?
0 Kommentare
Antworten (2)
Sayyed Ahmad
am 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 Kommentar
Exeedo
am 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:
set_param(gcs,'ComPort','COM5');
set_param(gcs,'ObjConstructor',"serial('COM5');");
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.
Regards,
Osama
1 Kommentar
Exeedo
am 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.
Siehe auch
Kategorien
Mehr zu Author Block Masks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!