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

创建和使用 J1939 参数组

以下示例说明如何使用 Vehicle Network Toolbox™,基于 J1939 通过使用存储在 CAN 数据库文件中的信息来创建和管理 J1939 参数组。此示例使用 CAN 数据库文件 J1939.dbc

打开数据库文件

打开数据库文件以访问定义。

db = canDatabase('J1939.dbc');

创建参数组

使用 j1939ParameterGroup 函数以及数据库中包含的信息创建一个参数组。

pg = j1939ParameterGroup(db, 'VehicleDataSingle')
pg = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 6
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 254
    DestinationAddress: 254

   Data Details:
   -------------
             Timestamp: 0
                  Data: [255 255 255 255 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

设置源地址和目标地址

要完全定义参数组并确定其在网络上传输的路径,请设置源地址和目标地址。

pg.SourceAddress = 30
pg.DestinationAddress = 50
pg = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 6
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 254

   Data Details:
   -------------
             Timestamp: 0
                  Data: [255 255 255 255 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []


pg = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 6
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [255 255 255 255 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

设置优先级

设置 Priority 属性以进一步自定义传输。

pg.Priority = 5;

查看信号信息

使用 Signals 属性查看此参数组的信号值。您可以直接写入和读取这些信号,以将数据打包到参数组中或从中解包数据。

pg.Signals
ans = 

  struct with fields:

    VehicleSignal4: -1
    VehicleSignal3: -1
    VehicleSignal2: -1
    VehicleSignal1: -1

更改信号信息

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

pg.Signals.VehicleSignal1 = 10
pg.Signals.VehicleSignal2 = 100
pg.Signals.VehicleSignal3 = 1000
pg.Signals.VehicleSignal4 = 10000
pg.Signals
pg = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [10 0 255 255 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []


pg = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [10 0 100 0 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []


pg = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [10 0 100 0 232 3 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []


pg = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [10 0 100 0 232 3 16 39]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []


ans = 

  struct with fields:

    VehicleSignal4: 10000
    VehicleSignal3: 1000
    VehicleSignal2: 100
    VehicleSignal1: 10

直接写入新数据

您还可以将值直接写入 Data 属性。

pg.Data(1:2) = [50 0]
pg.Signals
pg = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [50 0 100 0 232 3 16 39]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []


ans = 

  struct with fields:

    VehicleSignal4: 10000
    VehicleSignal3: 1000
    VehicleSignal2: 100
    VehicleSignal1: 50