gusucode.com > vnt工具箱matlab源码程序 > vnt/vntblks/vntmasks/vntcansfcngetparams.m

    function [errMsg paramsStruct] = vntcansfcngetparams(blockHandleStr, varargin)
%VNTCANSFCNGETPARAMS Start function called from VNT CAN blocks Start Method.
%
%    [ERRMSG PARAMSSTRUCT] = VNTCANSFCNGETPARAMS(BLOCKHANDLESTR, OBJCONSTRUCTOR)
%    Gets the required object parameters, PARAMSSTRUCT, and opens the selected 
%    CAN port for the block specified in BLOCKHANDLESTR.
%
%    PARAMSSTRUCT consists of the following fields:
%    BLOCKHANDLE, BUSSPEED, SJW, TSEG1, TSEG2, NSAMPLES, ACKMODE, 
%    DEVICE, VENDOR, DEVICENAME, DEVICEINDEX, DEVICECHANNELINDEX, DEVICECODE
%    ACCESSMASK, RECEIVEQUEUESIZE, PORTHANDLE, PERMISSIONMASK,
%    REQUIREDSILENTMODE, CONVERTERPATH, ACTUALSILENTMODE, PLUGINPATH
%    STDIDS, STDIDSSIZE, EXTIDS, EXTIDSSIZE.
%
%    Fields of VARARGIN:
%    String ON/OFF - representing whether Standard IDs filter is enabled
%    Array of Standard IDs
%    String ON/OFF - representing whether Extended IDs filter is enabled
%    Array of Extended IDs
%
%    This is called by CAN Receive, Transmit, Config, Replay and Logging 
%    blocks during object creation in mdlStart method.

%    SS 06-21-10
%    Copyright 2010-2011 The MathWorks, Inc.

% Initialize errMsg.
errMsg = [];  %#ok<NASGU>

% Initialize output params.
paramsStruct = [];

try
    % Check for nargin for Replay and Logging blocks.
    if (nargin==1)
        paramsStruct.SLLibraryPath = fullfile(toolboxdir('vnt'), 'vntblks', 'vntmex', computer('arch'));
        return;
    end
    
    % Get the object constructor.
    objConstructor = varargin{1};

    % Check if model has required/right number of Configuration blocks.
    [errMsg paramsStruct] = privatevntslconfigcheck(blockHandleStr, objConstructor);
    if ~isempty(errMsg)
        return;
    end
    [errMsg paramsStruct] = privatevntsldevicecheck(paramsStruct);
    if ~isempty(errMsg)
        return;
    end

    % Query the current simulation mode.
    simMode = get_param(bdroot, 'RapidAcceleratorSimStatus');
    if strcmpi(simMode, 'building')
        paramsStruct.IsRapidAccel = 1;
    else
        paramsStruct.IsRapidAccel = 0;
    end

    if (nargin>2)
        % Append code and mask information for filters.
        % idInfo{2} specifies if Standard IDs are enabled.
        % idInfo{3} consists of Standard IDs.
        % idInfo{4} specifies if Extended IDs are enabled.
        % idInfo{5} consists of Extended IDs.
        idInfo = varargin;

        % Extract ID information.
        paramsStruct.StdIDs = unique(idInfo{3});
        paramsStruct.ExtIDs = unique(idInfo{5});

        % Save the Standard and Extended ID list.
        stdIDs = localGetIds(paramsStruct.StdIDs);
        paramsStruct.StartIDs = zeros(1,length(stdIDs));
        paramsStruct.EndIDs = zeros(1,length(stdIDs));
        paramsStruct.NumRangeStdIDs = length(stdIDs);
        for idx = 1:length(stdIDs)
            paramsStruct.StartIDs(idx) = stdIDs{idx}.start;
            paramsStruct.EndIDs(idx) = stdIDs{idx}.end;
        end
        
        % Get the code and mask.
        [paramsStruct.StdIDsCode paramsStruct.StdIDsMask] = can.Utility.filterCalc(paramsStruct.StdIDs, 'Standard');
        [paramsStruct.ExtIDsCode paramsStruct.ExtIDsMask] = can.Utility.filterCalc(paramsStruct.ExtIDs, 'Extended');

        % Filtering status - Standard
        [paramsStruct.StdIDs paramsStruct.StdIDsSize paramsStruct.EnabledStdIDFiltering] = localAssignIDs(idInfo{2}, paramsStruct.StdIDs);
        [paramsStruct.ExtIDs paramsStruct.ExtIDsSize paramsStruct.EnabledExtIDFiltering] = localAssignIDs(idInfo{4}, paramsStruct.ExtIDs);
    end
catch e
    errMsg = e.message;
    return;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [outIDs IDsSize enabledIDFiltering] = localAssignIDs(filterSetting, inIDs)

% Assign filtering based on user setting.
switch filterSetting
    case {'Allow all', 'off'}
        outIDs = uint32(hex2dec('FFFFFFFF'));
        IDsSize = 1;
        enabledIDFiltering = 0;
    case {'Allow only', 'on'}
        outIDs = uint32(inIDs);
        IDsSize = length(inIDs);
        enabledIDFiltering = 1;
    case 'Block all'        
        outIDs = uint32(hex2dec('FFFFFFFF'));
        IDsSize = 1;
        enabledIDFiltering = 2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ids = localGetIds(idsIn)

% Get ID range.
idRange = unique(idsIn);

% Find the difference between consecutive elements.
diffValue = diff(idRange);

% Find locations where diff resulted non-contiguous
allIndices = find(diffValue~=1);

ids = cell(0,1);

if isempty(allIndices) % If empty, either single value or single range.
    ids{1}.start = idRange(1);
    ids{1}.end = idRange(end);
else
    % Append indices list with start and end index.
    allIndices = [0 allIndices length(idRange)];

    for index = 1:length(allIndices)-1
        ids{index}.start = idRange(allIndices(index)+1);
        ids{index}.end = idRange(allIndices(index+1));
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%