matlab j1939 通信快速入门 -pg电子麻将胡了
此示例说明如何创建和使用 j1939 通道在 j1939 网络上传输和接收参数组。此示例使用数据库文件 j1939.dbc 和以环回配置连接的 mathworks® 虚拟 can 通道。
打开 dbc 文件
使用 打开 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 通道
使用函数 创建可用于发送和接收信息的 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 参数组
使用函数 创建要在网络上发送的单帧参数组。
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 通道
使用函数 启动 j1939 通道进行传输和接收操作。
start(rxch); start(txch);
发送 j1939 参数组
函数将参数组发送到网络上。j1939 通道通过其传输协议自动发送要求使用多帧报文传输的参数组。
transmit(txch, pgsingleframe) transmit(txch, pgsingleframe) transmit(txch, pgmultiframe) transmit(txch, pgsingleframe) transmit(txch, pgsingleframe) pause(2);
接收参数组
函数从网络上进行报文传输的通道中检索信息。
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
访问信号值
函数可用于从参数组时间表中轻松提取和变换信号数据。
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 通道
要停止从网络接收数据,请使用 函数停止 j1939 通道。
stop(rxch); stop(txch);