📄 mbc_freeporttxd.m
字号:
function [buf_sizestr, portNamestr, baudratestr, channelstr, data_type_str, sampletime_str] = mbc_freeporttxd(sampletime, port, baudrate, channel, buf_size, data_type, format)
MAX_UCOM_CHANNELS = 10;
% inactivate link to library...
if(~strcmp(get_param(gcb, 'LinkStatus'), 'inactive'))
set_param(gcb,'LinkStatus','inactive')
end
% Check the sample time:
if ((sampletime < 0) & (sampletime ~= -1))
fprintf ('FreePortComms_TX: Inadmissible sample time !\n');
sampletime_str = 'invalid';
else
if(sampletime == -1)
sampletime_str = 'inherited';
else
if(sampletime == 0)
sampletime_str = 'continuous';
else
sampletime_str = [num2str(sampletime) ' s'];
end
end
end
% Check channel number
if ((channel < 0) | (channel > MAX_UCOM_CHANNELS))
channelstr = '???';
fprintf ('FreePortComms_TX: Inadmissible channel number !\n');
else
channelstr = num2str (channel);
end
% Check the number of inputs and adapt the underlying Simulink block to it:
if (buf_size < 1) | (buf_size > 100)
fprintf ('FreePortComms_TX: Inadmissible buffer size!\n');
buf_sizestr = '???'
else
buf_sizestr = num2str (buf_size);
end
% set data conversion type according to the block parameter 'data_type'
myBlocks = find_system(gcb, 'LookUnderMasks', 'all');
DTC = [];for(i = 1:length(myBlocks)), DTC = [DTC ~isempty(findstr(myBlocks{i},'Data Type Conversion'))]; end, DTC = find(DTC);
% define data_types as text
data_sz_str={'single', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'boolean'};
% get data_type_string
data_type_str = data_sz_str{data_type};
% telegram format (raw / formatted)
formatstr = num2str(format);
if(format == 1)
set_param(gcb, 'MaskVisibilities', {'on', 'on', 'on', 'off', 'on', 'off', 'on' });
% set_param(gcb, 'MaskEnables', {'on', 'on', 'on', 'off', 'on', 'on', 'on' });
channelstr = '-';
data_type_str = 'uint8';
else
set_param(gcb, 'MaskVisibilities', {'on', 'on', 'on', 'on', 'on', 'on', 'on' });
% set_param(gcb, 'MaskEnables', {'on', 'on', 'on', 'on', 'on', 'on', 'on' });
channelstr = num2str (channel);
data_type_str = data_sz_str{data_type};
end
% define 'portstr'
if(port <= 2)
% ensure consistency with the corresponding RTW option
kk_top = gcs;
ll = find(kk_top == '/');
if(~isempty(ll))
kk_top = kk_top(1:min(ll-1)); % this is the top-level system name (fw-08-06)
end
kk = get_param(kk_top, 'RTWOptions');
ll = findstr(kk, '-aExtMode=1');
if(~isempty(ll))
% Running in External Mode -> avoid port clashes...
ll = findstr(kk, '-aMATLAB_comms_port=');
RTWmatlabPort = str2num(kk(ll+24)) + 1;
if(RTWmatlabPort == port)
fprintf('FreePortComms_TX: The selected free port is used by MATLAB (see RTW options, External Mode)\n');
portNamestr = 'invalid';
% ... just in case -> should not be used (fw-09-06)
if(RTWmatlabPort == 1)
port = 2;
else
port = 1;
end
portstr = num2str(port - 1);
% fprintf ('FreePortComms_TX: Changing selected port to the free port on the target (see RTW options)\n');
% if(RTWmatlabPort == 1)
% port = 2;
% else
% port = 1;
% end
%
% % correct dialog parameters
% kk = find_system(gcb, 'FollowLinks', 'on');
% ll = get_param(kk{:}, 'DialogParameters');
% modified_port_text = ll.port.Enum;
% set_param(kk{:}, 'port', modified_port_text{port});
% make changes permanent
% save_system(kk_top);
else
portstr = num2str(port - 1);
portNamestr = ['SCI' portstr];
end
else
portstr = num2str(port - 1);
portNamestr = ['SCI' portstr];
end
else
portstr = num2str(port - 2);
portNamestr = ['COM' portstr];
end
% define 'baudratestr'
baudrate_strings ={'300', '600', '1200', '2400', '4800', '9600', '19200', '38400', '57600', '115200'};
baudratestr = baudrate_strings{baudrate};
% keep data_type as is (no index adjustment, e.g. 'data_type = data_type-1')
% -> this makes the first possible parameter ('1') map to the built-in data
% type 'single' ('0' = 'double'... which we omit here) -- fw-03-05
%debug
%disp(['mbc_TxD: data type: ' num2str(data_type) ' (' data_type_str ')'])
% set block parameter of the data conversion block
set_param(myBlocks{DTC}, 'DataType', data_type_str);
% check if each communication channel has only been allocated once
mySendBlocks = get_param(find_system(gcs, 'LookUnderMasks', 'all', 'Name', 'SEND_VIA_FREE_PORT'), 'Parent'); % get all TxD blocks
kk = get_param(mySendBlocks, 'channel'); % get channel number
kk = double(char(kk) - '0'); % turn this into numbers...
if(length(unique(kk)) < length(kk))
% the same channel number has been used more than once...
disp(['FreePortComms_TX: Warning... at least 2 TxD blocks have been allocated the same channel number!'])
end
% Create resource keywords to be reserved in resource database
modelRTWFields = struct( 'portStr', portstr, ...
'portNameStr', portNamestr, ...
'baudrateStr', baudratestr, ...
'formatStr', formatstr );
%debug
%modelRTWFields
% Insert modelRTWFields in the I/O block S-Function containing the Tag 'MC9S12DriverDataBlock'
% to set the block 'Tag' use:
% >> kk = find_system(gcb, 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Name', 'SEND_VIA_FREE_PORT')
% >> set_param(kk{:}, 'Tag', 'MC9S12target')
% ... then save the block / model (fw-02-05)
MC9S12DriverDataBlock = find_system(gcb, 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Tag', 'MC9S12target');
set_param(MC9S12DriverDataBlock{1}, 'RTWdata', modelRTWFields);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -