1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
| function sfun_callInterface(block) setup(block); end
function setup(block) block.NumInputPorts = 0; block.NumOutputPorts = 1; block.OutputPort(1).Dimensions = 1; block.OutputPort(1).DatatypeID = 0;
block.OutputPort(1).SamplingMode = 'Sample';
block.SampleTimes = [0 0]; block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('PostPropagationSetup', @PostProp); block.RegBlockMethod('InitializeConditions', @InitCond); block.RegBlockMethod('Outputs', @Outputs); block.RegBlockMethod('Terminate', @Terminate);
set_param(block.BlockHandle, 'OpenFcn', 'start_import_gui'); end
function PostProp(block) block.NumDworks = 3; block.Dwork(1).Name = 'guiLaunched'; block.Dwork(1).Dimensions = 1; block.Dwork(1).DatatypeID = 0; block.Dwork(1).Complexity = 'Real'; block.Dwork(1).UsedAsDiscState = true; block.Dwork(2).Name = 'confirmationFlag'; block.Dwork(2).Dimensions = 1; block.Dwork(2).DatatypeID = 0; block.Dwork(2).Complexity = 'Real'; block.Dwork(2).UsedAsDiscState = true;
block.Dwork(3).Name = 'simStarted'; block.Dwork(3).Dimensions = 1; block.Dwork(3).DatatypeID = 0; block.Dwork(3).Complexity = 'Real'; block.Dwork(3).UsedAsDiscState = true; end
function InitCond(block) block.Dwork(1).Data = 0; block.Dwork(2).Data = 0; block.Dwork(3).Data = 0; end
function Outputs(block) guiLaunched = block.Dwork(1).Data; confirmationFlag = block.Dwork(2).Data; simStarted = block.Dwork(3).Data; if guiLaunched == 0 rootSystem = bdroot(block.BlockHandle); modelName = '';
if isnumeric(rootSystem) && ishandle(rootSystem) modelName = get_param(rootSystem, 'Name'); elseif ischar(rootSystem) modelName = rootSystem; end
if isempty(modelName) errordlg('无法获取 Simulink 模型名称,GUI 无法启动。', '严重错误'); set_param(bdroot(block.BlockHandle), 'SimulationCommand', 'stop'); return; end start_import_gui(modelName); block.Dwork(1).Data = 1; end try confirmationFlag = evalin('base', 'confirmationFlag'); block.Dwork(2).Data = confirmationFlag; catch end block.OutputPort(1).Data = confirmationFlag; if confirmationFlag == 1 && simStarted == 0 set_param(bdroot(block.BlockHandle), 'SimulationCommand', 'start'); block.Dwork(3).Data = 1; end
if confirmationFlag == -1 try simStatus = get_param(bdroot(block.BlockHandle), 'SimulationStatus'); if strcmp(simStatus, 'running') set_param(bdroot(block.BlockHandle), 'SimulationCommand', 'stop'); disp('仿真已取消'); end catch disp('未能检查到正在运行的仿真,或仿真未启动'); end end end
function Terminate(~) evalin('base', 'clear confirmationFlag'); end
function start_import_gui(modelName) screenSize = get(0, 'ScreenSize'); screenWidth = screenSize(3); screenHeight = screenSize(4); figWidth = 800; figHeight = 400;
figPosX = (screenWidth - figWidth) / 2; figPosY = (screenHeight - figHeight) / 2;
fig = uifigure('Name', '数据导入与显示', 'Position', [figPosX, figPosY, figWidth, figHeight], ... 'CloseRequestFcn', @(src, event) close_gui(fig));
fig.UserData.modelName = modelName;
dll = fullfile(pwd, 'bin', 'BWIBridgeDLL.dll'); hdr = fullfile(pwd, 'bin', 'BWIBridgeDLL.h'); if ~libisloaded('BWIBridgeDLL') loadlibrary(dll, hdr); end fig.UserData.dllLoaded = true;
btnImport = uibutton(fig, 'push', 'Text', '导入数据', 'Position', [680, 360, 100, 30], ... 'ButtonPushedFcn', @(src, event) import_data(fig));
uit = uitable(fig, 'Position', [20, 60, 760, 280], ... 'ColumnName', {'变量名', '数值', '最小值', '最大值', '说明'}, ... 'Data', {}, 'ColumnEditable', [false true false false false]); uit.Tag = 'dataTable';
fig.UserData.data = []; fig.UserData.originalData = [];
filePathLabel = uilabel(fig, 'Text', '选择的文件夹路径:', 'Position', [20, 360, 100, 30]); filePathTextArea = uitextarea(fig, 'Position', [130, 360, 530, 30], 'Tag', 'filePathTextArea'); filePathTextArea.Editable = 'off';
uibutton(fig, 'push', 'Text', '确认', 'Position', [520, 20, 100, 30], ... 'ButtonPushedFcn', @(src, event) confirm_action(fig));
uibutton(fig, 'push', 'Text', '取消', 'Position', [640, 20, 100, 30], ... 'ButtonPushedFcn', @(src, event) cancel_action(fig)); end
function import_data(fig) folderPath = uigetdir('', '选择数据文件夹'); if folderPath == 0 return; end
iniFilePath = fullfile(folderPath, 'bwiparamtemp.ini'); if ~isfile(iniFilePath) uialert(fig, 'bwiparamtemp.ini 文件不存在', '导入错误'); return; end try proc = calllib('BWIBridgeDLL', 'CreateProcessor'); calllib('BWIBridgeDLL', 'LoadParametersFromIni', proc, iniFilePath); paramCount = calllib('BWIBridgeDLL', 'GetGlobalParameterCount', proc); data = cell(paramCount, 5); for i = 0:paramCount-1 name = calllib('BWIBridgeDLL', 'GetParameterName', proc, i); value = calllib('BWIBridgeDLL', 'GetParameterValue', proc, i); minVal = calllib('BWIBridgeDLL', 'GetParameterMin', proc, i); maxVal = calllib('BWIBridgeDLL', 'GetParameterMax', proc, i); desc = calllib('BWIBridgeDLL', 'GetParameterDescription', proc, i); data{i+1, 1} = char(name); data{i+1, 2} = value; data{i+1, 3} = minVal; data{i+1, 4} = maxVal; data{i+1, 5} = char(desc); end uit = findobj(fig, 'Tag', 'dataTable'); uit.Data = data; fig.UserData.data = data; fig.UserData.originalData = data;
filePathTextArea = findobj(fig, 'Tag', 'filePathTextArea'); if ~isempty(filePathTextArea) filePathTextArea.Value = {folderPath}; else uialert(fig, '未找到文件夹路径文本框,无法更新路径', '更新错误'); end calllib('BWIBridgeDLL', 'DeleteProcessor', proc); disp('数据导入成功'); catch ME uialert(fig, ['文件导入失败: ', ME.message], '导入错误'); end end
function confirm_action(fig) uit = findobj(fig, 'Tag', 'dataTable'); data = uit.Data;
filePathTextArea = findobj(fig, 'Tag', 'filePathTextArea'); folderPath = filePathTextArea.Value{1}; if isempty(folderPath) uialert(fig, '文件夹路径无效', '导入错误'); return; end
folderPath = char(folderPath);
proc = calllib('BWIBridgeDLL', 'CreateProcessor');
originalData = fig.UserData.originalData; if isempty(originalData) originalData = data; fig.UserData.originalData = data; end updated = false; for i = 1:size(data, 1) paramName = data{i, 1}; newValue = data{i, 2}; paramName = char(paramName); if ischar(newValue) || iscell(newValue) newValue = str2double(newValue); end if isnan(newValue) uialert(fig, ['参数 ', paramName, ' 的值无效(非数值)'], '导入错误'); calllib('BWIBridgeDLL', 'DeleteProcessor', proc); return; end originalValue = originalData{i, 2}; if isequal(newValue, originalValue) disp(['参数 ', paramName, ' 未发生变化,跳过更新']); continue; end result = calllib('BWIBridgeDLL', 'RewriteBWIIni_Param', proc, paramName, newValue, folderPath); if result disp(['参数 ', paramName, ' 修改成功']); originalData{i, 2} = newValue; updated = true; else uialert(fig, ['参数 ', paramName, ' 修改失败'], '导入错误'); calllib('BWIBridgeDLL', 'DeleteProcessor', proc); return; end end fig.UserData.originalData = originalData;
try modelName = fig.UserData.modelName; if isempty(modelName) || ~ischar(modelName) error('未能获取有效的模型名称。'); end set_param([modelName, '/XXX_Launcher'], 'paramPath', folderPath); set_param([modelName, '/XXX_Processor'], 'dataDir', folderPath); disp('已成功将路径参数传递到后续模块。'); catch ME uialert(fig, ['无法设置后续模块的路径参数: ', ME.message], '参数传递错误'); return; end calllib('BWIBridgeDLL', 'DeleteProcessor', proc); assignin('base', 'confirmationFlag', 1); delete(fig); end
function cancel_action(fig) assignin('base', 'confirmationFlag', -1); delete(fig); end
function close_gui(fig) delete(fig); end
|