gusucode.com > vnt工具箱matlab源码程序 > vnt/vnt/+can/+kvaser/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 Kvaser devices only.

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

properties (Constant, GetAccess = 'private')
    % SupportedDeviceCodes - Maps device string names to numeric device
    %   codes for all devices supported by the toolbox.
    SupportedDeviceCodes = containers.Map( ...
        { ...
        'virtual', ...
        'lapcan', ...
        'lapcan ii', ...
        'pccan', ...
        'pcican', ...
        'pcicanx', ...
        'pciecan', ...
        'usbcan', ...
        'usbcan ii', ...
        'usbcan rugged', ...
        'memorator', ...
        'leaf', ...
        'leaf light', ...
        'leaf light rugged', ...
        'leaf semipro', ...
        'leaf professional', ...
        'leaf light china', ...
        'leaf light v2', ...
        'pc104', ...
        'pc104+', ...
        'pcican ii', ...
        'pcicanx ii', ...
        'memorator ii', ...
        'memorator professional', ...
        'memorator r semipro', ...
        'usbcan professional', ...
        'usbcan r', ...
        'blackbird', ...
        'blackbird semipro', ...
        'eagle', ...
        'blackbird v2', ...
        'mini pci express', ...
        'ethercan light', ...
        'usbcan light', ...
        'usbcan pro', ...
        'pciecan v2', ...
        'memorator pro', ...
        'leaf pro', ...
        'memorator light' ...
        }, ...
        { ...
        1, ...  % 'Virtual'
        2, ...  % 'LAPcan'
        2, ...  % 'LAPcan II'
        8, ...  % 'PCcan'
        9, ...  % 'PCIcan'
        9, ...  % 'PCIcanx'
        9, ...  % 'PCIEcan'
        11, ... % 'USBcan'
        42, ... % 'USBcan II'
        42, ... % 'USBcan Rugged'
        42, ... % 'Memorator'
        48, ... % 'Leaf'
        48, ... % 'Leaf Light'
        48, ... % 'Leaf Light Rugged'
        48, ... % 'Leaf SemiPro'
        48, ... % 'Leaf Professional'
        48, ... % 'Leaf Light China'
        48, ... % 'Leaf Light v2'
        50, ... % 'PC104'
        50, ... % 'PC104+'
        52, ... % 'PCIcan II'
        52, ... % 'PCIcanx II'
        54, ... % 'Memorator II'
        54, ... % 'Memorator Professional'
        54, ... % 'Memorator R SemiPro'
        56, ... % 'USBcan Professional'
        56, ... % 'USBcan R'
        58, ... % 'BlackBird'
        58, ... $ 'BlackBird SemiPro'
        62, ... % 'Eagle'
        64, ... % 'BlackBird v2'
        66, ... % 'Mini PCI Express'
        70, ... % 'Ethercan Light'
        72, ... % 'USBcan Light'
        74, ... % 'USBcan Pro'
        76, ... % 'PCIEcan v2'
        78, ... % 'Memorator Pro'
        80, ... % 'Leaf Pro'
        82 ... % 'Memorator Light'
        } );
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();
            
            % Open the driver interface.
            can.kvaser.CANLib.canInitializeLibrary();
            
            % Return true if no error is encountered.
            found = true;
            
        catch err %#ok<NASGU>
            % Otherwise return false.
            found = false;
        end
    end
    
    function supported = isDeviceSupported(device)
    % isDeviceSupported Verify if a device is supported by the toolbox.
    %
    %   This method is used internally to check if a given device is
    %   supported by the toolbox. It returns true if the DEVICE is
    %   supported, otherwise false. DEVICE can be a device name as a
    %   string or a numeric device code.
        
        % Look up the device depending on the class of the input.
        if ischar(device)
            % All keys in the supported device container are lower
            % case, so the comparison must be made in lower case.
            supported = ...
                isKey(can.kvaser.Utility.SupportedDeviceCodes, lower(device));
        else
            supported = ...
                any(cell2mat(values(can.kvaser.Utility.SupportedDeviceCodes)) == device);
        end
    end
    
    function deviceCode = translateDeviceCode(deviceName)
    % translateDeviceCode Gets the device code from the device name.
    %
    %   This method takes the name of a device as a string and returns
    %   the vendor assigned device code for that device.
        
        % Look up the device code in the map.
        deviceCode = can.kvaser.Utility.SupportedDeviceCodes(lower(deviceName));
    end
    
    function deviceName = translateDeviceName(driverDeviceName)
    % translateDeviceName Gets the VNT device name from the Kvaser device name.
    %
    %   This method takes the name reported by the Kvaser CANLib and
    %   translates it into the device name expected to be used by VNT.
        
        % Set the name as lower case for safer matching.
        driverDeviceName = lower(driverDeviceName);
        
        % Check for certain key words in the device name and return the
        % formatted name as appropriate.
        
        % Virtual device.
        if strfind(driverDeviceName, 'virtual')
            deviceName = 'Virtual';
        % LAPcan device family.
        elseif strfind(driverDeviceName, 'lapcan')
            if strfind(driverDeviceName, 'ii')
                deviceName = 'LAPcan II';
            else
                deviceName = 'LAPcan';
            end
        % PCcan device.
        elseif strfind(driverDeviceName, 'pccan')
            deviceName = 'PCcan';
        % PCIcanx device family.
        elseif strfind(driverDeviceName, 'pcicanx')
            if strfind(driverDeviceName, 'ii')
                deviceName = 'PCIcanx II';
            else
                deviceName = 'PCIcanx';
            end
        % PCIcan device family.
        elseif strfind(driverDeviceName, 'pcican')
            if strfind(driverDeviceName, 'ii')
                deviceName = 'PCIcan II';
            else
                deviceName = 'PCIcan';
            end
        % PCIEcan device.
        elseif strfind(driverDeviceName, 'pciecan')
            if strfind(driverDeviceName, 'v2')
                deviceName = 'PCIEcan v2';
            else
                deviceName = 'PCIEcan';
            end
        % USBcan device family.
        elseif strfind(driverDeviceName, 'usbcan')
            if strfind(driverDeviceName, 'professional')
                deviceName = 'USBcan Professional';
            elseif strfind(driverDeviceName, 'rugged')
                deviceName = 'USBcan Rugged';
            elseif strfind(driverDeviceName, 'ii')
                deviceName = 'USBcan II';
            elseif strfind(driverDeviceName, ' r')
                deviceName = 'USBcan R';
            elseif strfind(driverDeviceName, 'light')
                deviceName = 'USBcan Light';
            elseif strfind(driverDeviceName, 'pro')
                deviceName = 'USBcan Pro';
            else
                deviceName = 'USBcan';
            end
        % Memorator device family.
        elseif strfind(driverDeviceName, 'memorator')
            if strfind(driverDeviceName, 'professional')
                deviceName = 'Memorator Professional';
            elseif strfind(driverDeviceName, 'ii')
                deviceName = 'Memorator II';
            elseif strfind(driverDeviceName, 'r semipro')
                deviceName = 'Memorator R SemiPro';
            elseif strfind(driverDeviceName, 'light')
                deviceName = 'Memorator Light';
            elseif strfind(driverDeviceName, 'pro')
                deviceName = 'Memorator Pro';
            else
                deviceName = 'Memorator';
            end
        % Leaf device family.
        elseif strfind(driverDeviceName, 'leaf')
            if strfind(driverDeviceName, 'light rugged')
                deviceName = 'Leaf Light Rugged';
            elseif strfind(driverDeviceName, 'light china')
                deviceName = 'Leaf Light China';
            elseif strfind(driverDeviceName, 'light v2')
                deviceName = 'Leaf Light v2';
            elseif strfind(driverDeviceName, 'light')
                deviceName = 'Leaf Light';
            elseif strfind(driverDeviceName, 'semi')
                deviceName = 'Leaf SemiPro';
            elseif strfind(driverDeviceName, 'professional')
                deviceName = 'Leaf Professional';
            elseif strfind(driverDeviceName, 'pro')
                deviceName = 'Leaf Pro';
            else
                deviceName = 'Leaf';
            end
        % This is a special case for the Leaf Light China device. That
        % device is sold only in China and reports with a name using
        % Chinese characters. When queried in Kvaser CANLib for an
        % ASCII name, the driver automatically sets the Chinese
        % characters to question marks, so we key off the question
        % marked name to identify the device.
        elseif strfind(driverDeviceName, '??l')
            deviceName = 'Leaf Light China';
        % PC104 device family.
        elseif strfind(driverDeviceName, 'pc104')
            if strfind(driverDeviceName, '+')
                deviceName = 'PC104+';
            else
                deviceName = 'PC104';
            end
        % BlackBird device.
        elseif strfind(driverDeviceName, 'blackbird')
            if strfind(driverDeviceName, 'v2')
                deviceName = 'BlackBird v2';
            elseif strfind(driverDeviceName, 'semi')
                deviceName = 'BlackBird SemiPro';
            else
                deviceName = 'BlackBird';
            end
        % Eagle device.
        elseif strfind(driverDeviceName, 'eagle')
            deviceName = 'Eagle';
        % Mini PCI Express device.
        elseif strfind(driverDeviceName, 'mini pci express')
            deviceName = 'Mini PCI Express';
        % Ethercan device.
        elseif strfind(driverDeviceName, 'ethercan')
            deviceName = 'Ethercan Light';
        else
            % Return blank on no match.
            deviceName = '';
        end
    end
    
    function data = getDriverData(deviceCode, deviceIndex, deviceChannelIndex)
    % getDriverData Retrieve channel information for a device.
    %
    %   This method is used internally to look up detailed device and
    %   channel information in the driver interface. This
    %   information is returned as a structure.
        
        % Get the number of channels present.
        channelCount = can.kvaser.CANLib.canGetNumberOfChannels();
        
        % Loop through the channel data until a match is found to the input.
        for ii = 0:(channelCount - 1)
            % Call for the channel data for this index.
            data = can.kvaser.CANLib.canGetChannelData(ii);
            
            % Check for a match between the current channel data and
            % the channel specified by the method input.
            if (deviceCode == data.CardType) && ...
               ((deviceIndex - 1) == data.CardNumber) && ...
               ((deviceChannelIndex - 1) == data.ChannelNumberOnCard)
                
                % Return if matched.
                return;
            end
        end
        
        % If no driver data was found for the specified device, then
        % return as empty.
        data = [];
    end
    
end

end