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

创建和使用 J1939 通道

以下示例说明如何使用 Vehicle Network Toolbox™,基于 J1939 创建和使用 J1939 通道以在网络上传输与接收参数组。此示例使用 CAN 数据库文件 J1939.dbc。它也使用以环回配置形式连接的 MathWorks Virtual CAN 通道。

打开数据库文件

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

db = canDatabase('J1939.dbc');

创建 J1939 通道

创建可用于发送和接收信息的 J1939 通道。

txCh = j1939Channel(db, 'MathWorks', 'Virtual 1', 1)
rxCh = j1939Channel(db, 'MathWorks', 'Virtual 1', 2)
txCh = 

  Channel with properties:

   Device Information:
   -------------------
                  DeviceVendor: 'MathWorks'
                        Device: 'Virtual 1'
            DeviceChannelIndex: 1
            DeviceSerialNumber: 0

   Data Details:
   -------------
      ParameterGroupsAvailable: 0
       ParameterGroupsReceived: 0
    ParameterGroupsTransmitted: 0
                FilterPassList: []
               FilterBlockList: []

   Channel Information:
   --------------------
                       Running: 0
                     BusStatus: 'N/A'
          InitializationAccess: 1
              InitialTimestamp: [0×0 datetime]
                    SilentMode: 0
               TransceiverName: 'N/A'
              TransceiverState: 'N/A'
                      BusSpeed: 500000
                           SJW: []
                         TSEG1: []
                         TSEG2: []
                  NumOfSamples: []

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


rxCh = 

  Channel with properties:

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

   Data Details:
   -------------
      ParameterGroupsAvailable: 0
       ParameterGroupsReceived: 0
    ParameterGroupsTransmitted: 0
                FilterPassList: []
               FilterBlockList: []

   Channel Information:
   --------------------
                       Running: 0
                     BusStatus: 'N/A'
          InitializationAccess: 1
              InitialTimestamp: [0×0 datetime]
                    SilentMode: 0
               TransceiverName: 'N/A'
              TransceiverState: 'N/A'
                      BusSpeed: 500000
                           SJW: []
                         TSEG1: []
                         TSEG2: []
                  NumOfSamples: []

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

创建 J1939 参数组

您可以创建要在网络上发送的 J1939 参数组。

pgSingleFrame = j1939ParameterGroup(db, 'VehicleDataSingle')
pgSingleFrame.SourceAddress = 30;
pgSingleFrame.DestinationAddress = 50;
pgMultiFrame = j1939ParameterGroup(db, 'VehicleDataMulti')
pgMultiFrame.SourceAddress = 30;
pgMultiFrame.DestinationAddress = 255;
pgSingleFrame = 

  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: []


pgMultiFrame = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataMulti'
                   PGN: 51200
              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 255 255 255 255]
               Signals: [1×1 struct]

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

启动 J1939 通道

要开始使用通道进行传输和接收操作,请将它们置于在线状态。

start(rxCh);
start(txCh);

发送 J1939 参数组

transmit 函数将参数组发送到网络上。J1939 通道通过其传输协议自动发送要求使用多帧报文传输的参数组。

transmit(txCh, pgSingleFrame)
transmit(txCh, pgSingleFrame)
transmit(txCh, pgMultiFrame)
transmit(txCh, pgSingleFrame)
transmit(txCh, pgSingleFrame)
pause(1);

接收参数组

receive 函数从网络上进行报文传输的通道中检索信息。

pgRx = receive(rxCh, Inf)
pgRx = 

  1×5 ParameterGroup array with properties:

   Protocol Data Unit Details:
   ---------------------------
    Name
    PGN
    Priority
    PDUFormatType
    SourceAddress
    DestinationAddress

   Data Details:
   -------------
    Timestamp
    Data
    Signals

   Other Information:
   ------------------
    UserData

检查收到的参数组

查看收到的信息的细节。

pgRx(1)
ans = 

  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.0262
                  Data: [255 255 255 255 255 255 255 255]
               Signals: [1×1 struct]

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

查看多帧接收

使用传输协议发送的参数组由通道完整地重新构造。

pgRx(3)
ans = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataMulti'
                   PGN: 51200
              Priority: 6
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 255

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

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

停止 J1939 通道

要停止从网络接收数据,请停止 J1939 通道。

stop(rxCh);
stop(txCh);