Main Content

本页的翻译已过时。点击此处可查看最新英文版本。

使用数据库定义创建和处理报文

以下示例说明如何使用存储在 CAN 数据库文件中的信息创建、接收和处理报文。此示例使用 CAN 数据库文件 demoVNT_CANdbFiles.dbc

打开数据库文件

打开数据库文件并检查 Messages 属性,以查看此数据库中定义的所有报文的名称。

db = canDatabase('demoVNT_CANdbFiles.dbc')
db.Messages
db = 

  Database with properties:

             Name: 'demoVNT_CANdbFiles'
             Path: '/tmp/Bdoc21a_1599804_127502/tpc4b0dfa7/ex80654288/demoVNT_CANdbFiles.dbc'
            Nodes: {}
         NodeInfo: [0x0 struct]
         Messages: {5x1 cell}
      MessageInfo: [5x1 struct]
       Attributes: {}
    AttributeInfo: [0x0 struct]
         UserData: []


ans =

  5x1 cell array

    {'DoorControlMsg'   }
    {'EngineMsg'        }
    {'SunroofControlMsg'}
    {'TransmissionMsg'  }
    {'WindowControlMsg' }

查看报文信息

使用 messageInfo 查看报文信息,包括标识符、数据长度和信号列表。

messageInfo(db, 'EngineMsg')
ans = 

  struct with fields:

             Name: 'EngineMsg'
     ProtocolMode: 'CAN'
          Comment: ''
               ID: 100
         Extended: 0
            J1939: []
           Length: 8
              DLC: 8
              BRS: 0
          Signals: {2x1 cell}
       SignalInfo: [2x1 struct]
          TxNodes: {0x1 cell}
       Attributes: {}
    AttributeInfo: [0x0 struct]

您还可以一次查询所有报文的信息。

messageInfo(db)
ans = 

  5x1 struct array with fields:

    Name
    ProtocolMode
    Comment
    ID
    Extended
    J1939
    Length
    DLC
    BRS
    Signals
    SignalInfo
    TxNodes
    Attributes
    AttributeInfo

查看信号信息

使用 signalInfo 查看信号定义信息,包括用于将原始信号转换为物理值的类型、字节顺序、大小和缩放值。

signalInfo(db, 'EngineMsg', 'EngineRPM')
ans = 

  struct with fields:

             Name: 'EngineRPM'
          Comment: ''
         StartBit: 0
       SignalSize: 32
        ByteOrder: 'LittleEndian'
           Signed: 0
        ValueType: 'Integer'
            Class: 'uint32'
           Factor: 0.1000
           Offset: 250
          Minimum: 250
          Maximum: 9500
            Units: 'rpm'
       ValueTable: [0x1 struct]
      Multiplexor: 0
      Multiplexed: 0
    MultiplexMode: 0
          RxNodes: {0x1 cell}
       Attributes: {}
    AttributeInfo: [0x0 struct]

您还可以一次查询报文中所有信号的信息。

signalInfo(db, 'EngineMsg')
ans = 

  2x1 struct array with fields:

    Name
    Comment
    StartBit
    SignalSize
    ByteOrder
    Signed
    ValueType
    Class
    Factor
    Offset
    Minimum
    Maximum
    Units
    ValueTable
    Multiplexor
    Multiplexed
    MultiplexMode
    RxNodes
    Attributes
    AttributeInfo

使用数据库定义创建报文

在创建要应用数据库定义的新报文时,需要指定报文的名称。此报文中的 CAN 信号除以原始数据字节表示外,还以工程单位来表示。

msgEngineInfo = canMessage(db, 'EngineMsg')
msgEngineInfo = 

  Message with properties:

   Message Identification
    ProtocolMode: 'CAN'
              ID: 100
        Extended: 0
            Name: 'EngineMsg'

   Data Details
       Timestamp: 0
            Data: [0 0 0 0 0 0 0 0]
         Signals: [1x1 struct]
          Length: 8

   Protocol Flags
           Error: 0
          Remote: 0

   Other Information
        Database: [1x1 can.Database]
        UserData: []

查看信号信息

使用 Signals 属性查看此报文的信号值。您可以直接对这些信号进行写入和读取,以打包或解包报文中的数据。

msgEngineInfo.Signals
ans = 

  struct with fields:

    VehicleSpeed: 0
       EngineRPM: 250

更改信号信息

直接写入信号以更改值并读回其当前值。

msgEngineInfo.Signals.EngineRPM = 5500.25
msgEngineInfo.Signals
msgEngineInfo = 

  Message with properties:

   Message Identification
    ProtocolMode: 'CAN'
              ID: 100
        Extended: 0
            Name: 'EngineMsg'

   Data Details
       Timestamp: 0
            Data: [23 205 0 0 0 0 0 0]
         Signals: [1x1 struct]
          Length: 8

   Protocol Flags
           Error: 0
          Remote: 0

   Other Information
        Database: [1x1 can.Database]
        UserData: []


ans = 

  struct with fields:

    VehicleSpeed: 0
       EngineRPM: 5.5003e+03

直接写入信号时,使用数据库定义对值进行转换、缩放并打包到报文数据中。

msgEngineInfo.Signals.VehicleSpeed = 70.81
msgEngineInfo.Signals
msgEngineInfo = 

  Message with properties:

   Message Identification
    ProtocolMode: 'CAN'
              ID: 100
        Extended: 0
            Name: 'EngineMsg'

   Data Details
       Timestamp: 0
            Data: [23 205 0 0 71 0 0 0]
         Signals: [1x1 struct]
          Length: 8

   Protocol Flags
           Error: 0
          Remote: 0

   Other Information
        Database: [1x1 can.Database]
        UserData: []


ans = 

  struct with fields:

    VehicleSpeed: 71
       EngineRPM: 5.5003e+03

接收具有数据库信息的报文

将数据库连接到 CAN 通道,该通道接收报文以自动将数据库定义应用于传入报文。数据库仅解码已定义的报文。所有其他报文都以其原始形式接收。

rxCh = canChannel('MathWorks', 'Virtual 1', 2);
rxCh.Database = db
rxCh = 

  Channel with properties:

   Device Information
            DeviceVendor: 'MathWorks'
                  Device: 'Virtual 1'
      DeviceChannelIndex: 2
      DeviceSerialNumber: 0
            ProtocolMode: 'CAN'

   Status Information
                 Running: 0
       MessagesAvailable: 0
        MessagesReceived: 0
     MessagesTransmitted: 0
    InitializationAccess: 1
        InitialTimestamp: [0x0 datetime]
           FilterHistory: 'Standard ID Filter: Allow All | Extended ID Filter: Allow All'

   Channel Information
               BusStatus: 'N/A'
              SilentMode: 0
         TransceiverName: 'N/A'
        TransceiverState: 'N/A'
       ReceiveErrorCount: 0
      TransmitErrorCount: 0
                BusSpeed: 500000
                     SJW: []
                   TSEG1: []
                   TSEG2: []
            NumOfSamples: []

   Other Information
                Database: [1x1 can.Database]
                UserData: []

接收报文

启动通道,生成一些报文流,然后通过物理报文解码来接收报文。

start(rxCh);
generateMsgsDb();
rxMsg = receive(rxCh, Inf, 'OutputFormat', 'timetable');
rxMsg(1:15, :)
ans =

  15x8 timetable

        Time        ID     Extended            Name                       Data              Length      Signals       Error    Remote
    ____________    ___    ________    _____________________    ________________________    ______    ____________    _____    ______

    0.037132 sec    100     false      {'EngineMsg'        }    {[     0 0 0 0 0 0 0 0]}      8       {1x1 struct}    false    false 
    0.037139 sec    200     false      {'TransmissionMsg'  }    {[     0 0 0 0 0 0 0 0]}      8       {1x1 struct}    false    false 
    0.037149 sec    400     false      {'DoorControlMsg'   }    {[     0 0 0 0 0 0 0 0]}      8       {1x1 struct}    false    false 
    0.03716 sec     600     false      {'WindowControlMsg' }    {[             0 0 0 0]}      4       {1x1 struct}    false    false 
    0.037162 sec    800     false      {'SunroofControlMsg'}    {[                 0 0]}      2       {1x1 struct}    false    false 
    0.062134 sec    100     false      {'EngineMsg'        }    {[172 129 0 0 50 0 0 0]}      8       {1x1 struct}    false    false 
    0.087148 sec    100     false      {'EngineMsg'        }    {[172 129 0 0 50 0 0 0]}      8       {1x1 struct}    false    false 
    0.087152 sec    200     false      {'TransmissionMsg'  }    {[     4 0 0 0 0 0 0 0]}      8       {1x1 struct}    false    false 
    0.11215 sec     100     false      {'EngineMsg'        }    {[172 129 0 0 50 0 0 0]}      8       {1x1 struct}    false    false 
    0.13715 sec     100     false      {'EngineMsg'        }    {[172 129 0 0 50 0 0 0]}      8       {1x1 struct}    false    false 
    0.13715 sec     200     false      {'TransmissionMsg'  }    {[     4 0 0 0 0 0 0 0]}      8       {1x1 struct}    false    false 
    0.16215 sec     100     false      {'EngineMsg'        }    {[177 131 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.16215 sec     400     false      {'DoorControlMsg'   }    {[     1 0 0 0 0 0 0 0]}      8       {1x1 struct}    false    false 
    0.18715 sec     100     false      {'EngineMsg'        }    {[177 131 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.18715 sec     200     false      {'TransmissionMsg'  }    {[     4 0 0 0 0 0 0 0]}      8       {1x1 struct}    false    false 

停止通道并从工作区中清除它。

stop(rxCh);
clear rxCh

检查收到的报文

检查收到的报文以查看应用的数据库解码。

rxMsg(10, :)
rxMsg.Signals{10}
ans =

  1x8 timetable

       Time        ID     Extended        Name                   Data              Length      Signals       Error    Remote
    ___________    ___    ________    _____________    ________________________    ______    ____________    _____    ______

    0.13715 sec    100     false      {'EngineMsg'}    {[172 129 0 0 50 0 0 0]}      8       {1x1 struct}    false    false 


ans = 

  struct with fields:

    VehicleSpeed: 50
       EngineRPM: 3.5696e+03

提取指定报文的所有实例

使用 MATLAB 表示法按名称提取指定报文的所有实例。

allMsgEngine = rxMsg(strcmpi('EngineMsg', rxMsg.Name), :);
allMsgEngine(1:15, :)
ans =

  15x8 timetable

        Time        ID     Extended        Name                   Data              Length      Signals       Error    Remote
    ____________    ___    ________    _____________    ________________________    ______    ____________    _____    ______

    0.037132 sec    100     false      {'EngineMsg'}    {[     0 0 0 0 0 0 0 0]}      8       {1x1 struct}    false    false 
    0.062134 sec    100     false      {'EngineMsg'}    {[172 129 0 0 50 0 0 0]}      8       {1x1 struct}    false    false 
    0.087148 sec    100     false      {'EngineMsg'}    {[172 129 0 0 50 0 0 0]}      8       {1x1 struct}    false    false 
    0.11215 sec     100     false      {'EngineMsg'}    {[172 129 0 0 50 0 0 0]}      8       {1x1 struct}    false    false 
    0.13715 sec     100     false      {'EngineMsg'}    {[172 129 0 0 50 0 0 0]}      8       {1x1 struct}    false    false 
    0.16215 sec     100     false      {'EngineMsg'}    {[177 131 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.18715 sec     100     false      {'EngineMsg'}    {[177 131 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.21214 sec     100     false      {'EngineMsg'}    {[177 131 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.23713 sec     100     false      {'EngineMsg'}    {[177 131 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.26214 sec     100     false      {'EngineMsg'}    {[177 131 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.28716 sec     100     false      {'EngineMsg'}    {[ 91 133 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.3122 sec      100     false      {'EngineMsg'}    {[ 91 133 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.33714 sec     100     false      {'EngineMsg'}    {[ 91 133 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.36221 sec     100     false      {'EngineMsg'}    {[ 91 133 0 0 55 0 0 0]}      8       {1x1 struct}    false    false 
    0.38721 sec     100     false      {'EngineMsg'}    {[201 130 0 0 54 0 0 0]}      8       {1x1 struct}    false    false 

绘制物理信号值

绘制随时间变化的数据库解码信号的值。引用报文时间戳和变量中的信号值。

signalTimetable = canSignalTimetable(rxMsg, 'EngineMsg');
signalTimetable(1:15, :)
plot(signalTimetable.Time, signalTimetable.VehicleSpeed)
title('Vehicle Speed from EngineMsg', 'FontWeight', 'bold')
xlabel('Timestamp')
ylabel('Vehicle Speed')
ans =

  15x2 timetable

        Time        VehicleSpeed    EngineRPM
    ____________    ____________    _________

    0.037132 sec          0             250  
    0.062134 sec         50          3569.6  
    0.087148 sec         50          3569.6  
    0.11215 sec          50          3569.6  
    0.13715 sec          50          3569.6  
    0.16215 sec          55          3621.3  
    0.18715 sec          55          3621.3  
    0.21214 sec          55          3621.3  
    0.23713 sec          55          3621.3  
    0.26214 sec          55          3621.3  
    0.28716 sec          55          3663.9  
    0.3122 sec           55          3663.9  
    0.33714 sec          55          3663.9  
    0.36221 sec          55          3663.9  
    0.38721 sec          54          3598.1