gusucode.com > vnt工具箱matlab源码程序 > vnt/vntblks/vntmasks/private/privatevntslinitlib.m
function privatevntslinitlib %PRIVATEVNTSLINITLIB Initializes the Vehicle Network Toolbox block library. % % PRIVATEVNTSLINITLIB populates the VNT blocks with information on the % available devices. % SS 04-01-08 % Copyright 2008-2012 The MathWorks, Inc. libBlkInfo = {... 'canlib/CAN Configuration', 'CANCONFIG'; ... 'canlib/CAN Receive', 'CANREC'; ... 'canlib/CAN Transmit', 'CANTRAN'; ... 'canlib/CAN Replay', 'CANREP'; ... 'canlib/CAN Log', 'CANLOG'}; for b=1:size(libBlkInfo,1) localInitBlk( libBlkInfo{b,:} ); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function localInitBlk(blockName, sysID) sysBlockHandle = [sysID 'BLOCKHANDLE']; sysDeviceList = [sysID 'DEVICELIST']; sysDevice = [sysID 'DEVICE']; sysConstructor = [sysID 'CONSTRUCTOR']; % For multi port option of display, this is required. set_param(blockName, 'MaskSelfModifiable','on'); styleString = get_param(blockName, 'MaskStyleString'); if isempty( findstr(styleString, sysDeviceList) ) % Block already initialized. return; end % Query the system for the available hardware. [devices, constructors] = privatevntslparsehwinfo; switch sysID case {'CANREP', 'CANLOG'} % Select the default device. devices = [{'Select a device'} devices]; constructors = [{'Select a constructor'} constructors]; otherwise % Do nothing. end % Select the default device. firstDevice = devices{1}; objConstructor = constructors{1}; % Create pop up strings. devices{end+1} = '(none)'; allDevices = localCreatePopUpStrings(devices); styleString = get_param(blockName, 'MaskStyleString'); styleString = strrep(styleString, sysDeviceList, allDevices); set_param(blockName, 'MaskStyleString', styleString); valueString = get_param(blockName, 'MaskValueString'); valueString = strrep(valueString, sysDevice, firstDevice); valueString = strrep(valueString, sysConstructor, objConstructor); % Get the block handle. blockHandle = num2str(get_param(blockName, 'Handle')); valueString = strrep(valueString, sysBlockHandle, blockHandle); % Initialization specific to the blocks. switch sysID case 'CANCONFIG' % Find initial values. [busSpeed sjw tseg1 tseg2 nSamples] = localFindInitialValues(objConstructor); valueString = strrep(valueString, 'CANCONFIGBUSSPEED', busSpeed); valueString = strrep(valueString, 'CANCONFIGSJW', sjw); valueString = strrep(valueString, 'CANCONFIGTSEG1', tseg1); valueString = strrep(valueString, 'CANCONFIGTSEG2', tseg2); valueString = strrep(valueString, 'CANCONFIGNSAMPLES', nSamples); case {'CANREC' 'CANTRAN' 'CANREP' 'CANLOG'} % Do Nothing. otherwise assert('false', 'vnt:vntblks:InvalidSystem', 'Invalid System ID.'); end set_param(blockName, 'MaskValueString', valueString); set_param(blockName, 'LoadFcn', ... ['if ~strcmpi(get_param(bdroot(gcb), ''BlockDiagramType''), ''library'')' ... 'set_param(gcb, ''BlockHandle'', num2str(gcbh)); '... 'end']); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function retString = localCreatePopUpStrings(entries) % Create popup strings for the block combobox. if isempty(entries) retString = ''; else retString = sprintf('%s|', entries{:}); retString = retString(1:end-1); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [busSpeed sjw tseg1 tseg2 nSamples] = localFindInitialValues(objConstructor, varargin) try canch = eval(objConstructor); catch e %#ok<NASGU> % Set the initialization to (none). busSpeed = '(none)'; sjw = '(none)'; tseg1 = '(none)'; tseg2 = '(none)'; nSamples = '(none)'; return; end % Get bus speed and bit parameters. busSpeed = sprintf('%d', canch.BusSpeed); sjw = sprintf('%d', canch.SJW); tseg1 = sprintf('%d', canch.TSEG1); tseg2 = sprintf('%d', canch.TSEG2); nSamples = sprintf('%d', canch.NumOfSamples); % Deleting the object (not required) delete(canch); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%