MATLAB J1939 通信快速入门
此示例说明如何创建和使用 J1939 通道在 J1939 网络上传输和接收参数组。此示例使用数据库文件 J1939.dbc 和以环回配置连接的 MathWorks® 虚拟 CAN 通道。
打开 DBC 文件
使用 canDatabase 打开 DBC 文件以访问定义。
db = canDatabase("J1939.dbc")db =
Database with properties:
Name: 'J1939'
Path: 'C:\Users\michellw\OneDrive - MathWorks\Documents\MATLAB\Examples\vnt-ex33605241\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: []
创建 J1939 通道
使用函数 j1939Channel 创建可用于发送和接收信息的 J1939 通道。
txCh = j1939Channel(db, "MathWorks", "Virtual 1", 1)
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 = j1939Channel(db, "MathWorks", "Virtual 1", 2)
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 参数组
使用函数 j1939ParameterGroup 创建要在网络上发送的单帧参数组。
pgSingleFrame = j1939ParameterGroup(db, "VehicleDataSingle")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: []
设置传输详细信息和信号数据。
pgSingleFrame.SourceAddress = 30; pgSingleFrame.DestinationAddress = 50; pgSingleFrame.Signals.VehicleSignal1 = 25; pgSingleFrame.Signals.VehicleSignal2 = 1000; pgSingleFrame.Signals
ans = struct with fields:
VehicleSignal4: -1
VehicleSignal3: -1
VehicleSignal2: 1000
VehicleSignal1: 25
使用相同的方法,创建一个多帧参数组,然后设置传输详细信息和信号数据。
pgMultiFrame = j1939ParameterGroup(db, "VehicleDataMulti")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: []
pgMultiFrame.SourceAddress = 30; pgMultiFrame.DestinationAddress = 255; pgMultiFrame.Signals.VehicleSignal1 = 5; pgMultiFrame.Signals.VehicleSignal2 = 650; pgMultiFrame.Signals.VehicleSignal3 = 5000; pgMultiFrame.Signals
ans = struct with fields:
VehicleSignal6: -1
VehicleSignal5: -1
VehicleSignal4: -1
VehicleSignal3: 5000
VehicleSignal2: 650
VehicleSignal1: 5
启动 J1939 通道
使用函数 start 启动 J1939 通道进行传输和接收操作。
start(rxCh); start(txCh);
发送 J1939 参数组
transmit 函数将参数组发送到网络上。J1939 通道通过其传输协议自动发送要求使用多帧报文传输的参数组。
transmit(txCh, pgSingleFrame) transmit(txCh, pgSingleFrame) transmit(txCh, pgMultiFrame) transmit(txCh, pgSingleFrame) transmit(txCh, pgSingleFrame) pause(2);
接收参数组
receive 函数从网络上进行报文传输的通道中检索信息。
pgRx = receive(rxCh, Inf)
pgRx=5×8 timetable
Time Name PGN Priority PDUFormatType SourceAddress DestinationAddress Data Signals
___________ _________________ _____ ________ _____________________ _____________ __________________ ____________________________________________ ____________
0.13955 sec VehicleDataSingle 40192 6 Peer-to-Peer (Type 1) 30 50 {[ 25 0 232 3 255 255 255 255]} {1×1 struct}
0.14347 sec VehicleDataSingle 40192 6 Peer-to-Peer (Type 1) 30 50 {[ 25 0 232 3 255 255 255 255]} {1×1 struct}
0.59386 sec VehicleDataMulti 51200 6 Peer-to-Peer (Type 1) 30 255 {[5 0 138 2 136 19 255 255 255 255 255 255]} {1×1 struct}
0.76564 sec VehicleDataSingle 40192 6 Peer-to-Peer (Type 1) 30 50 {[ 25 0 232 3 255 255 255 255]} {1×1 struct}
0.7702 sec VehicleDataSingle 40192 6 Peer-to-Peer (Type 1) 30 50 {[ 25 0 232 3 255 255 255 255]} {1×1 struct}
检查收到的参数组信号
查看单帧和多帧参数组实例接收的信号的详细信息。
pgRx.Signals{1}ans = struct with fields:
VehicleSignal4: -1
VehicleSignal3: -1
VehicleSignal2: 1000
VehicleSignal1: 25
pgRx.Signals{3}ans = struct with fields:
VehicleSignal6: -1
VehicleSignal5: -1
VehicleSignal4: -1
VehicleSignal3: 5000
VehicleSignal2: 650
VehicleSignal1: 5
访问信号值
j1939SignalTimetable 函数可用于从参数组时间表中轻松提取和变换信号数据。
sigTT = j1939SignalTimetable(pgRx)
sigTT = struct with fields:
VehicleDataMulti: [1×6 timetable]
VehicleDataSingle: [4×4 timetable]
sigTT.VehicleDataSingle
ans=4×4 timetable
Time VehicleSignal4 VehicleSignal3 VehicleSignal2 VehicleSignal1
___________ ______________ ______________ ______________ ______________
0.13955 sec -1 -1 1000 25
0.14347 sec -1 -1 1000 25
0.76564 sec -1 -1 1000 25
0.7702 sec -1 -1 1000 25
sigTT.VehicleDataMulti
ans=1×6 timetable
Time VehicleSignal6 VehicleSignal5 VehicleSignal4 VehicleSignal3 VehicleSignal2 VehicleSignal1
___________ ______________ ______________ ______________ ______________ ______________ ______________
0.59386 sec -1 -1 -1 5000 650 5
停止 J1939 通道
要停止从网络接收数据,请使用 stop 函数停止 J1939 通道。
stop(rxCh); stop(txCh);