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

    classdef Utility
% Utility General definitions and functions for J1939 protocol.
%
%   This class provides generic property definitions and methods used for
%   implementing support for the J1939 protocol.
%
%   See also VNT.

% Authors: Jaremy Pyle
% Copyright 2015 The MathWorks, Inc.
  
properties (Constant)
    % Properties used to mask individual PG parameters from a CAN identifier.
    PriorityBitMask      = uint32(bin2dec('00011100000000000000000000000000'));
    ReservedBitMask      = uint32(bin2dec('00000010000000000000000000000000'));
    DataPageBitMask      = uint32(bin2dec('00000001000000000000000000000000'));
    PDUFormatBitMask     = uint32(bin2dec('00000000111111110000000000000000'));
    PDUSpecificBitMask   = uint32(bin2dec('00000000000000001111111100000000'));
    SourceAddressBitMask = uint32(bin2dec('00000000000000000000000011111111'));
    PGNBitMask           = uint32(bin2dec('00000011111111111111111100000000'));
    PGNBitMaskPDU1       = uint32(bin2dec('00000011111111110000000000000000'));
    PGNBitMaskPDU2       = uint32(bin2dec('00000011111111111111111100000000'));

    % Properties used to clear individual PG parameters from a CAN identifier.
    PriorityBitMaskClear      = uint32(bin2dec('00000011111111111111111111111111'));
    ReservedBitMaskClear      = uint32(bin2dec('00011101111111111111111111111111'));
    DataPageBitMaskClear      = uint32(bin2dec('00011110111111111111111111111111'));
    PDUFormatBitMaskClear     = uint32(bin2dec('00011111000000001111111111111111'));
    PDUSpecificBitMaskClear   = uint32(bin2dec('00011111111111110000000011111111'));
    SourceAddressBitMaskClear = uint32(bin2dec('00011111111111111111111100000000'));
    PGNBitMaskClear           = uint32(bin2dec('00011100000000000000000011111111'));
    PGNBitMaskClearPDU1       = uint32(bin2dec('00011100000000001111111111111111'));
    PGNBitMaskClearPDU2       = uint32(bin2dec('00011100000000000000000011111111'));

    % Properties for shifting individual PG parameters into a CAN identifier.
    PriorityBitShift      = 26;
    ReservedBitShift      = 25;
    DataPageBitShift      = 24;
    PDUFormatBitShift     = 16;
    PDUSpecificBitShift   = 8;
    SourceAddressBitShift = 0;
    PGNBitShift           = 8;
    
    % Properties for shifting individual PG parameters out of a CAN identifier.
    PriorityBitShiftRight      = -26;
    ReservedBitShiftRight      = -25;
    DataPageBitShiftRight      = -24;
    PDUFormatBitShiftRight     = -16;
    PDUSpecificBitShiftRight   = -8;
    SourceAddressBitShiftRight = 0;
    PGNBitShiftRight           = -8;

    % PDUFormatThreshold - The value at which a PGN changes from PDU type
    % 1 to PDU type 2.
    PDUFormatThreshold = 240;
    % PDUFormatType1 - String to use for property values representing a
    % PDU type 1 PGN.
    PDUFormatType1 = 'Peer-to-Peer (Type 1)';
    % PDUFormatType2 - String to use for property values representing a
    % PDU type 2 PGN.
    PDUFormatType2 = 'Broadcast (Type 2)';
    
    % DefaultPriority - The default priority of a PG.
    DefaultPriority = uint32(7);
    % DestinationAll - The destination address to use for broadcast PGs.
    DestinationAll = uint32(255);
    % FillByte - Byte value to use as filler for PG data.
    FillByte = uint8(255);
    
    % TPConnectionManagementPGN - The PGN of a TP CM message.
    TPConnectionManagementPGN = uint32(60416);
    % TPDataTransferPGN - The PGN of a TP DT message.
    TPDataTransferPGN = uint32(60160);
    
    % Control byte values for TP CM messages.
    TPRequestToSendControlByte     = uint8(16);
    TPClearToSendControlByte       = uint8(17);
    TPAcknowledgmentControlByte    = uint8(19);
    TPBroadcastAnnounceControlByte = uint8(32);
    TPConnectionAbortControlByte   = uint8(255);
    
    % Time delays and timeouts used for J1939 transport protocol activities.
    TPDTTransmitDelay = 0.050;
    TPTimeoutTr = 0.200;
    TPTimeoutTh = 0.500;
    TPTimeoutT1 = 0.750;
    TPTimeoutT2 = 1.250;
    TPTimeoutT3 = 1.250;
    TPTimeoutT4 = 1.050;
    TPWaitTime  = 0.001;
end

methods (Static)
    
    function result = setPDUField(name, value, identifier)
    % setPDUField Sets a value into a J1939 parameter identifier.
    %
    %   This method does bit masking and shifting to set a specific J1939
    %   parameter group into a message identifier. The parameter to set is
    %   specified by the NAME input.
        
        % Get the mask and shift counts for the specified parameter.
        switch name
            case 'Priority'
                mask = j1939.Utility.PriorityBitMaskClear;
                shift = j1939.Utility.PriorityBitShift;
            case 'Reserved'
                mask = j1939.Utility.ReservedBitMaskClear;
                shift = j1939.Utility.ReservedBitShift;
            case 'DataPage'
                mask = j1939.Utility.DataPageBitMaskClear;
                shift = j1939.Utility.DataPageBitShift;
            case 'PDUFormat'
                mask = j1939.Utility.PDUFormatBitMaskClear;
                shift = j1939.Utility.PDUFormatBitShift;
            case 'PDUSpecific'
                mask = j1939.Utility.PDUSpecificBitMaskClear;
                shift = j1939.Utility.PDUSpecificBitShift;
            case 'SourceAddress'
                mask = j1939.Utility.SourceAddressBitMaskClear;
                shift = j1939.Utility.SourceAddressBitShift;
            case 'PGN'
                mask = j1939.Utility.PGNBitMaskClear;
                shift = j1939.Utility.PGNBitShift;
            otherwise
                result = [];
                return;
        end
        
        % Clear the bits in the identifier in order to place the new value.
        result = bitand(uint32(identifier), mask);
        % Shift the new value bits to the proper bit positioning.
        value = bitshift(uint32(value), shift);
        % Put the new value and cleared identifier together into one value.
        result = bitor(result, value);
    end
    
    function out = tpCreateReceiverKey(sourceAddress, destinationAddress)
    % tpCreateReceiverKey Build a key for the transport protocol receiver map.
    %
    %   This method is used to create a string key for the containers.Map
    %   object used to function as the transport protocol receive buffer.
        
        % Combine the numeric address values into a single string value.
        out = sprintf('%d%d', sourceAddress, destinationAddress);
    end
    
end

end