gusucode.com > vnt工具箱matlab源码程序 > vnt/vnt/+can/+peaksystem/+pcanbasic/Utility.m

    classdef (Hidden) Utility
% Utility Supporting functionality for toolbox operations.
%
%   This class implements internal functionality required for toolbox
%   operation. These methods are not intended for external use. 
%   Methods contained here are applicable to PEAK-System devices only.

% Authors: JDP
% Copyright 2013-2015 The MathWorks, Inc.

properties (Constant)
    % Channels.
    PCAN_NONEBUS             = hex2dec('00');  % Undefined/default value for a PCAN bus
    PCAN_ISABUS1             = hex2dec('21');  % PCAN-ISA interface, channel 1
    PCAN_ISABUS2             = hex2dec('22');  % PCAN-ISA interface, channel 2
    PCAN_ISABUS3             = hex2dec('23');  % PCAN-ISA interface, channel 3
    PCAN_ISABUS4             = hex2dec('24');  % PCAN-ISA interface, channel 4
    PCAN_ISABUS5             = hex2dec('25');  % PCAN-ISA interface, channel 5
    PCAN_ISABUS6             = hex2dec('26');  % PCAN-ISA interface, channel 6
    PCAN_ISABUS7             = hex2dec('27');  % PCAN-ISA interface, channel 7
    PCAN_ISABUS8             = hex2dec('28');  % PCAN-ISA interface, channel 8
    PCAN_DNGBUS1             = hex2dec('31');  % PCAN-Dongle/LPT interface, channel 1
    PCAN_PCIBUS1             = hex2dec('41');  % PCAN-PCI interface, channel 1
    PCAN_PCIBUS2             = hex2dec('42');  % PCAN-PCI interface, channel 2
    PCAN_PCIBUS3             = hex2dec('43');  % PCAN-PCI interface, channel 3
    PCAN_PCIBUS4             = hex2dec('44');  % PCAN-PCI interface, channel 4
    PCAN_PCIBUS5             = hex2dec('45');  % PCAN-PCI interface, channel 5
    PCAN_PCIBUS6             = hex2dec('46');  % PCAN-PCI interface, channel 6
    PCAN_PCIBUS7	         = hex2dec('47');  % PCAN-PCI interface, channel 7
    PCAN_PCIBUS8	         = hex2dec('48');  % PCAN-PCI interface, channel 8
    PCAN_USBBUS1             = hex2dec('51');  % PCAN-USB interface, channel 1
    PCAN_USBBUS2             = hex2dec('52');  % PCAN-USB interface, channel 2
    PCAN_USBBUS3             = hex2dec('53');  % PCAN-USB interface, channel 3
    PCAN_USBBUS4             = hex2dec('54');  % PCAN-USB interface, channel 4
    PCAN_USBBUS5             = hex2dec('55');  % PCAN-USB interface, channel 5
    PCAN_USBBUS6             = hex2dec('56');  % PCAN-USB interface, channel 6
    PCAN_USBBUS7             = hex2dec('57');  % PCAN-USB interface, channel 7
    PCAN_USBBUS8             = hex2dec('58');  % PCAN-USB interface, channel 8
    PCAN_PCCBUS1             = hex2dec('61');  % PCAN-PC Card interface, channel 1
    PCAN_PCCBUS2             = hex2dec('62');  % PCAN-PC Card interface, channel 2    
    
    ChannelNames = { ...
        'PCAN_PCIBUS1', 'PCAN_PCIBUS2', 'PCAN_PCIBUS3', 'PCAN_PCIBUS4', ...
        'PCAN_PCIBUS5', 'PCAN_PCIBUS6', 'PCAN_PCIBUS7', 'PCAN_PCIBUS8', ...
        'PCAN_USBBUS1', 'PCAN_USBBUS2', 'PCAN_USBBUS3', 'PCAN_USBBUS4', ...
        'PCAN_USBBUS5', 'PCAN_USBBUS6', 'PCAN_USBBUS7', 'PCAN_USBBUS8', ...
        'PCAN_PCCBUS1', 'PCAN_PCCBUS2' };
    
    ChannelNamesMap = containers.Map( ...
        { 'PCAN_PCIBUS1', 'PCAN_PCIBUS2', 'PCAN_PCIBUS3', 'PCAN_PCIBUS4', ...
          'PCAN_PCIBUS5', 'PCAN_PCIBUS6', 'PCAN_PCIBUS7', 'PCAN_PCIBUS8', ...
          'PCAN_USBBUS1', 'PCAN_USBBUS2', 'PCAN_USBBUS3', 'PCAN_USBBUS4', ...
          'PCAN_USBBUS5', 'PCAN_USBBUS6', 'PCAN_USBBUS7', 'PCAN_USBBUS8', ...
          'PCAN_PCCBUS1', 'PCAN_PCCBUS2', 'PCAN_NONEBUS' }, ...
        { hex2dec('41'), hex2dec('42'), hex2dec('43'), hex2dec('44'), ...
          hex2dec('45'), hex2dec('46'), hex2dec('47'), hex2dec('48'), ...
          hex2dec('51'), hex2dec('52'), hex2dec('53'), hex2dec('54'), ...
          hex2dec('55'), hex2dec('56'), hex2dec('57'), hex2dec('58'), ...
          hex2dec('61'), hex2dec('62'), hex2dec('00') } );
    
    % Baud rates.
    PCAN_BAUD_1M             = hex2dec('0014');  %   1 MBit/s
    PCAN_BAUD_800K           = hex2dec('0016');  % 800 kBit/s
    PCAN_BAUD_500K           = hex2dec('001C');  % 500 kBit/s
    PCAN_BAUD_250K           = hex2dec('011C');  % 250 kBit/s
    PCAN_BAUD_125K           = hex2dec('031C');  % 125 kBit/s
    PCAN_BAUD_100K           = hex2dec('432F');  % 100 kBit/s
    PCAN_BAUD_95K            = hex2dec('C34E');  %  95,238 kBit/s
    PCAN_BAUD_83K            = hex2dec('852B');  %  83,333 kBit/s
    PCAN_BAUD_50K            = hex2dec('472F');  %  50 kBit/s
    PCAN_BAUD_47K            = hex2dec('1414');  %  47,619 kBit/s
    PCAN_BAUD_33K            = hex2dec('8B2F');  %  33,333 kBit/s
    PCAN_BAUD_20K            = hex2dec('532F');  %  20 kBit/s
    PCAN_BAUD_10K            = hex2dec('672F');  %  10 kBit/s
    PCAN_BAUD_5K             = hex2dec('7F7F');  %   5 kBit/s

    % Baud rate to driver codes map.
    BaudRateMap = containers.Map( ...
        { 1000000, 800000, 500000, 250000, 125000, 100000, 95238, 83333, ...
          50000, 47619, 33333, 20000, 10000, 5000 }, ...
        { hex2dec('0014'), hex2dec('0016'), hex2dec('001C'), hex2dec('011C'), ...
          hex2dec('031C'), hex2dec('432F'), hex2dec('C34E'), hex2dec('852B'), ...
          hex2dec('472F'), hex2dec('1414'), hex2dec('8B2F'), hex2dec('532F'), ...
          hex2dec('672F'), hex2dec('7F7F') } );

    % Allowed bus speeds.
    AllowedBaudRates = [1000000, 800000, 500000, 250000, 125000, 100000, ...
        95238, 83333, 50000, 47619, 33333, 20000, 10000, 5000]
    
    % Error and status codes.
    PCAN_ERROR_OK = hex2dec('00000');
    
    % Parameters.
    PCAN_DEVICE_NUMBER       = hex2dec('01');  % PCAN-USB device number parameter
    PCAN_5VOLTS_POWER        = hex2dec('02');  % PCAN-PC Card 5-Volt power parameter
    PCAN_RECEIVE_EVENT       = hex2dec('03');  % PCAN receive event handler parameter
    PCAN_MESSAGE_FILTER      = hex2dec('04');  % PCAN message filter parameter
    PCAN_API_VERSION         = hex2dec('05');  % PCAN-Basic API version parameter
    PCAN_CHANNEL_VERSION     = hex2dec('06');  % PCAN device channel version parameter
    PCAN_BUSOFF_AUTORESET    = hex2dec('07');  % PCAN Reset-On-Busoff parameter
    PCAN_LISTEN_ONLY         = hex2dec('08');  % PCAN Listen-Only parameter
    PCAN_LOG_LOCATION        = hex2dec('09');  % Directory path for trace files
    PCAN_LOG_STATUS          = hex2dec('0A');  % Debug-Trace activation status
    PCAN_LOG_CONFIGURE       = hex2dec('0B');  % Configuration of the debugged information (LOG_FUNCTION_***)
    PCAN_LOG_TEXT            = hex2dec('0C');  % Custom insertion of text into the log file
    PCAN_CHANNEL_CONDITION   = hex2dec('0D');  % Availability status of a PCAN-Channel
    PCAN_HARDWARE_NAME       = hex2dec('0E');  % PCAN hardware name parameter
    PCAN_RECEIVE_STATUS      = hex2dec('0F');  % Message reception status of a PCAN-Channel
    PCAN_CONTROLLER_NUMBER   = hex2dec('10');  % CAN-Controller number of a PCAN-Channel     
    
    % Parameter values.
    PCAN_PARAMETER_OFF       = hex2dec('00');  % The PCAN parameter is not set (inactive)
    PCAN_PARAMETER_ON        = hex2dec('01');  % The PCAN parameter is set (active)
    PCAN_FILTER_CLOSE        = hex2dec('00');  % The PCAN filter is closed. No messages will be received
    PCAN_FILTER_OPEN         = hex2dec('01');  % The PCAN filter is fully opened. All messages will be received
    PCAN_FILTER_CUSTOM       = hex2dec('02');  % The PCAN filter is custom configured. Only registered messages will be received
    PCAN_CHANNEL_UNAVAILABLE = hex2dec('00');  % The PCAN-Channel handle is illegal, or its associated hadware is not available
    PCAN_CHANNEL_AVAILABLE   = hex2dec('01');  % The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means futhermore that the hardware is plugged-in)
    PCAN_CHANNEL_OCCUPIED    = hex2dec('02');  % The PCAN-Channel handle is valid, and is already being used
    
    % Log functions.
    LOG_FUNCTION_DEFAULT    = hex2dec('00');  % Logs system exceptions and errors. Custom log texts are also included.
    LOG_FUNCTION_ENTRY      = hex2dec('01');  % Logs the entries to the PCAN-Basic API functions.
    LOG_FUNCTION_PARAMETERS = hex2dec('02');  % Logs the parameters passed to the PCAN-Basic API functions.
    LOG_FUNCTION_LEAVE      = hex2dec('04');  % Logs the exits from the PCAN-Basic API functions.
    LOG_FUNCTION_WRITE      = hex2dec('08');  % Logs the CAN messages passed to the CAN_Write function.
    LOG_FUNCTION_READ       = hex2dec('10');  % Logs the CAN messages received within the CAN_Read function.
    LOG_FUNCTION_ALL        = hex2dec('FFFF');  % Logs all possible information within the PCAN-Basic API functions.
end

methods (Static)
    
    function found = isDriverAvailable()
        % isDriverAvailable Verifies the presence of a driver.
        %
        %   This method is used internally to check if a driver interface
        %   is installed on the system. It returns true if the driver was
        %   identified, otherwise returns false.
        
        try
             % Silence load library warnings to prevent potential BaT stalls.
            import com.mathworks.toolbox.testmeas.util.mwSystem.*;
            com.mathworks.toolbox.testmeas.util.mwSystem.silenceLoadLibrary();
            
            % There is no driver open function in this library, so to
            % verify if the library is available, we can simply call one of
            % the other functions. If the calls succeeds, we know the
            % library has to be present.
            can.peaksystem.pcanbasic.PCANBasic.CAN_GetValue( ...
                can.peaksystem.pcanbasic.Utility.PCAN_NONEBUS, ...
                can.peaksystem.pcanbasic.Utility.PCAN_API_VERSION);
            
            % Return true if no error is encountered.
            found = true;
            
        catch err %#ok<NASGU>
            % Otherwise return false.
            found = false;
        end
    end
    
    function out = getDriverVersion()
    % getDriverVersion Returns vendor driver version.
    %
    %   This method obtains driver version information and returns
    %   the version as a string built from the various version number
    %   components.
        
        % Get all the channel information.
        out = can.peaksystem.pcanbasic.PCANBasic.CAN_GetValue( ...
            can.peaksystem.pcanbasic.Utility.PCAN_NONEBUS, ...
            can.peaksystem.pcanbasic.Utility.PCAN_API_VERSION);
        out = regexprep(out, ', ', '.');
    end
    
    function out = getNumberOfChannels()
    % getNumberOfChannels Get the number of channels available.
    %
    %   This method returns the count of device channels available
    %   on the system.
        
        % Get all the channel information.
        chInfo = can.peaksystem.pcanbasic.Utility.getAllChannelInfo();
        % Return the count of channel information as the number of channels.
        out = numel(chInfo);
    end
    
    function out = getChannelInfoByInterface(interface)
    % getChannelInfoByInterface Get specific channel information.
    %
    %   This method returns a channel information structure for the device
    %   channel represented by the input. The expected input is a string
    %   that is the interface tag assigned to the channel.
        
        % Get information on all of the channels available.
        channelInfo = can.peaksystem.pcanbasic.Utility.getAllChannelInfo();
        
        % Find the specific channel information structure that corresponds
        % to the interface name provided and return it. Note that we loop
        % because the string comparison functions that make use of logical
        % indexing only can compare on a fixed string size and not an exact
        % match.
        for ii = 1:numel(channelInfo)
            % Return the channel information structure at the matching
            % index for the requested interface.
            if strcmpi(interface, channelInfo(ii).Handle)
                out = channelInfo(ii);
                return;
            end
        end
        
        % Return empty on no match.
        out = [];
    end
    
    function out = getAllChannelInfo()
    % getAllChannelInfo Get channel information for all channels.
    %
    %   This method returns an array of channel information structures with
    %   an entry for each device channel available on the system.
    
        % Initialize output.
        out = struct([]);

        % Loop through each possible channel.
        for ii = can.peaksystem.pcanbasic.Utility.ChannelNames
            % Get the channel condition.
            chCondition = can.peaksystem.pcanbasic.PCANBasic.CAN_GetValue( ...
                can.peaksystem.pcanbasic.Utility.(char(ii)), ...
                can.peaksystem.pcanbasic.Utility.PCAN_CHANNEL_CONDITION);
            
            % If the channel is unavailable, then continue.
            if chCondition == can.peaksystem.pcanbasic.Utility.PCAN_CHANNEL_UNAVAILABLE
                continue;
            end
            
            % If the channel is not initialized, then initialize it.
            if chCondition == can.peaksystem.pcanbasic.Utility.PCAN_CHANNEL_AVAILABLE
                can.peaksystem.pcanbasic.PCANBasic.CAN_Initialize( ...
                    can.peaksystem.pcanbasic.Utility.(char(ii)), ...
                    can.peaksystem.pcanbasic.Utility.PCAN_BAUD_500K, ...
                    0, 0, 0);
            end
            
            % Get the various parameters.
            out(end + 1).APIVersion = can.peaksystem.pcanbasic.PCANBasic.CAN_GetValue( ...
                can.peaksystem.pcanbasic.Utility.(char(ii)), ...
                can.peaksystem.pcanbasic.Utility.PCAN_API_VERSION); %#ok<AGROW>
            out(end).APIVersion = regexprep(out(end).APIVersion, ', ', '.');
            
            % We have to stub the device number to zero because it is a
            % required field for the info structure in Simulink, but
            % PEAK-System devices do not use it. There is a value for this
            % item in the PEAK-System API, but it does not work for all
            % hardware types. So, we set to zero just to have the field
            % present through the Simulink layers, but it is ignored in the
            % PEAK-System specific Simulink code.
            out(end).DeviceNumber = 0;
            
            out(end).ChannelVersion = can.peaksystem.pcanbasic.PCANBasic.CAN_GetValue( ...
                can.peaksystem.pcanbasic.Utility.(char(ii)), ...
                can.peaksystem.pcanbasic.Utility.PCAN_CHANNEL_VERSION);
            [~, out(end).ChannelVersion] = strtok(out(end).ChannelVersion, ' ');
            out(end).ChannelVersion = strtok(out(end).ChannelVersion, '(');
            out(end).ChannelVersion = out(end).ChannelVersion(2:end-1);
            
            out(end).HardwareName = can.peaksystem.pcanbasic.PCANBasic.CAN_GetValue( ...
                can.peaksystem.pcanbasic.Utility.(char(ii)), ...
                can.peaksystem.pcanbasic.Utility.PCAN_HARDWARE_NAME);
            
            out(end).ControllerNumber = can.peaksystem.pcanbasic.PCANBasic.CAN_GetValue( ...
                can.peaksystem.pcanbasic.Utility.(char(ii)), ...
                can.peaksystem.pcanbasic.Utility.PCAN_CONTROLLER_NUMBER) + 1;
            
            out(end).Handle = char(ii);
            
            out(end).SerialNumber = 0;
            
            % If the channel was formerly not initialized, then
            % uninitialize it now.
            if chCondition == can.peaksystem.pcanbasic.Utility.PCAN_CHANNEL_AVAILABLE
                can.peaksystem.pcanbasic.PCANBasic.CAN_Uninitialize( ...
                    can.peaksystem.pcanbasic.Utility.(char(ii)));
            end
        end
    end    
    
    function enableLogging()
    % enableLogging Enable the PCAN Basic logging feature.
    %
    %   This method is used to enable the logging capability of the driver.
    
        % Set to log all.
        can.peaksystem.pcanbasic.PCANBasic.CAN_SetValue( ...
            can.peaksystem.pcanbasic.Utility.PCAN_NONEBUS, ...
            can.peaksystem.pcanbasic.Utility.PCAN_LOG_CONFIGURE, ...
            can.peaksystem.pcanbasic.Utility.LOG_FUNCTION_ALL);
        % Turn on logging.
        can.peaksystem.pcanbasic.PCANBasic.CAN_SetValue( ...
            can.peaksystem.pcanbasic.Utility.PCAN_NONEBUS, ...
            can.peaksystem.pcanbasic.Utility.PCAN_LOG_STATUS, ...
            can.peaksystem.pcanbasic.Utility.PCAN_PARAMETER_ON);
        
    end
    
    function disableLogging()
    % disableLogging Disable the PCAN Basic logging feature.
    %
    %   This method is used to disable the logging capability of the driver.

        % Turn on logging.
        can.peaksystem.pcanbasic.PCANBasic.CAN_SetValue( ...
            can.peaksystem.pcanbasic.Utility.PCAN_NONEBUS, ...
            can.peaksystem.pcanbasic.Utility.PCAN_LOG_STATUS, ...
            can.peaksystem.pcanbasic.Utility.PCAN_PARAMETER_OFF);
    end
    
end

end