MATLAB J1939 参数组快速入门
此示例说明如何使用存储在 DBC 文件中的信息来创建和管理 J1939 参数组。此示例使用文件 J1939.dbc
。当需要向 J1939 网络传输数据时,建议以这种方式创建和使用参数组。
打开 DBC 文件
使用 canDatabase
打开 DBC 文件以访问定义。
db = canDatabase("J1939.dbc")
db = Database with properties: Name: 'J1939' Path: 'C:\Users\michellw\OneDrive - MathWorks\Documents\MATLAB\Examples\vnt-ex46196345\J1939.dbc' Nodes: {2×1 cell} NodeInfo: [2×1 struct] Messages: {2×1 cell} MessageInfo: [2×1 struct] Attributes: {3×1 cell} AttributeInfo: [3×1 struct] UserData: []
创建参数组
使用 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 = 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.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: 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 = 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.Signals.VehicleSignal2 = 100
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.Signals.VehicleSignal3 = 1000
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.Signals.VehicleSignal4 = 10000
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: []
pg.Signals
ans = struct with fields:
VehicleSignal4: 10000
VehicleSignal3: 1000
VehicleSignal2: 100
VehicleSignal1: 10
直接写入新数据
您还可以将值直接写入 Data
属性,但通常建议并首选通过 Signals
设置值。
pg.Data(1:2) = [50 0]
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: []
pg.Signals
ans = struct with fields:
VehicleSignal4: 10000
VehicleSignal3: 1000
VehicleSignal2: 100
VehicleSignal1: 50