update CAN Pack / CAN Unpack blocks in the Vehicle Network Toolbox programatically Failed

조회 수: 4 (최근 30일)
when i update a CAN pack block with new dbc, i find the block port will not change, you should open the block and manually set following, then it can update ,how to realize these steps programatically

답변 (1개)

Purvaja
Purvaja 2025년 9월 3일
I understand that when you update a "CAN Pack" block with a new DBC file, the block ports do not refresh automatically. Currently, you have to open the block, select the new message, and enable Output as bus manually to see the updated ports.
You can automate this process programmatically using set_param. The key parameters for the CAN Pack block required here are:
  • CANdbFile : to specify the DBC file path
  • MsgName : choose the message from the DBC (optional)
  • BusOutput : enable the bus output (equivalent to checking “Output as bus”)
I tried to recreate this on my side using a dummy Simulink model with 2 similar databases ('dummy_can.dbc' and 'dummy_can_copy.dbc'). And to update the parameters programmatically I scripted a method which would change the database and keep the 'Output port as bus'.
You can refer to this example method definition of "updateCanBlock":
function updateCanBlock(modelName, dbcFile)
load_system(modelName);
% Find all CAN Pack blocks
blk = find_system(modelName, 'MaskType', 'CAN Pack');
if isempty(blk)
error('No CAN Pack block found in model %s', modelName);
end
for i = 1:numel(blk)
set_param(blk{i}, 'CANdbFile', dbcFile);
% set_param(blk{i}, 'MsgName', msgName);
set_param(blk{i}, 'BusOutput', 'on');
fprintf('Updated CAN Pack block: %s\n', blk{i});
end
% Refresh Simulink model
set_param(modelName, 'SimulationCommand', 'update');
end
In command line, call method with by supplying new database like below:
updateCanBlock('dummyCANModel', fullfile(pwd, 'dummy_can_copy.dbc'));
You can compare the below outputs:
1.Before changing the database :
2.After calling the method :
If you want to know try for more parameters that the block seupport, try this command :
>> blk = find_system('dummyCANModel', 'Name', 'CAN Pack');
>> p = get_param(blk, 'DialogParameters');
>> disp(fieldnames(p))
{'DataFormat' }
{'CANdbFile' }
{'MsgList' }
{'MsgName' }
{'MsgIDType' }
{'MsgIdentifier'}
{'CANIDInput' }
{'MsgLength' }
{'Remote' }
{'ApplyMinMax' }
{'BusOutput' }
{'SignalInfo' }
For more clarification, refer to following links:
  1. 'set_param' : https://www.mathworks.com/help/simulink/slref/set_param.html
  2. 'CAN Pack' : https://www.mathworks.com/help/ecoder/ref/canpack.html
Hope this helps you!

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by