Main Content

本页面提供的是上一版软件的文档。当前版本中已删除对应的英文页面。

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

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

打开数据库文件

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

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

  Database with properties:

             Name: 'demoVNT_CANdbFiles'
             Path: '/tmp/BR2020ad_1302590_239645/publish_examples1/tp2f426c1c/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.0024975 sec    100     false      {'EngineMsg'        }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.0025021 sec    200     false      {'TransmissionMsg'  }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.0025033 sec    400     false      {'DoorControlMsg'   }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.0025053 sec    600     false      {'WindowControlMsg' }    {1x4 uint8}      4       {1x1 struct}    false    false 
    0.002506 sec     800     false      {'SunroofControlMsg'}    {1x2 uint8}      2       {1x1 struct}    false    false 
    0.027504 sec     100     false      {'EngineMsg'        }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.052501 sec     100     false      {'EngineMsg'        }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.052502 sec     200     false      {'TransmissionMsg'  }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.077503 sec     100     false      {'EngineMsg'        }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1025 sec       100     false      {'EngineMsg'        }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1025 sec       200     false      {'TransmissionMsg'  }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1275 sec       100     false      {'EngineMsg'        }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1275 sec       400     false      {'DoorControlMsg'   }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1525 sec       100     false      {'EngineMsg'        }    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1525 sec       200     false      {'TransmissionMsg'  }    {1x8 uint8}      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.1025 sec    100     false      {'EngineMsg'}    {1x8 uint8}      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.0024975 sec    100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.027504 sec     100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.052501 sec     100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.077503 sec     100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1025 sec       100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1275 sec       100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1525 sec       100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.1775 sec       100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.2025 sec       100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.22751 sec      100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.2525 sec       100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.2775 sec       100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.3025 sec       100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.32751 sec      100     false      {'EngineMsg'}    {1x8 uint8}      8       {1x1 struct}    false    false 
    0.35251 sec      100     false      {'EngineMsg'}    {1x8 uint8}      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.0024975 sec          0             250  
    0.027504 sec          50          3569.6  
    0.052501 sec          50          3569.6  
    0.077503 sec          50          3569.6  
    0.1025 sec            50          3569.6  
    0.1275 sec            50          3569.6  
    0.1525 sec            55          3621.3  
    0.1775 sec            55          3621.3  
    0.2025 sec            55          3621.3  
    0.22751 sec           55          3621.3  
    0.2525 sec            55          3663.9  
    0.2775 sec            55          3663.9  
    0.3025 sec            55          3663.9  
    0.32751 sec           55          3663.9  
    0.35251 sec           54          3598.1