matlab can 通信快速入门 -pg电子麻将胡了
以下示例说明如何使用 can 通道传输和接收 can 报文。它使用以环回配置形式连接的 mathworks® 虚拟 can 通道。
创建接收通道
通过指定供应商名称、设备名称和设备通道索引,使用 创建一个 can 通道以接收报文。
rxch = canchannel("mathworks", "virtual 1", 2);
检查通道
使用 get 命令获取有关所有通道属性及其当前值的更多详细信息。
get(rxch)
arbitrationbusspeed: []
databusspeed: []
receiveerrorcount: 0
transmiterrorcount: 0
initializationaccess: 1
initialtimestamp: [0×0 datetime]
silentmode: 0
transceiverstate: 'n/a'
busspeed: 500000
numofsamples: []
sjw: []
tseg1: []
tseg2: []
busstatus: 'n/a'
transceivername: 'n/a'
database: []
messagereceivedfcn: []
messagereceivedfcncount: 1
userdata: []
filterhistory: 'standard id filter: allow all | extended id filter: allow all'
messagesreceived: 0
messagestransmitted: 0
running: 0
device: 'virtual 1'
devicechannelindex: 2
deviceserialnumber: 0
devicevendor: 'mathworks'
protocolmode: 'can'
messagesavailable: 0
启动通道
使用 命令将通道设置为在线状态。
start(rxch);
传输报文
示例函数 generatemsgs 使用 创建 can 报文,并使用 以不同周期性速率传输它们。它会在 can 总线上生成流量,用于演示目的。
type generatemsgsfunction generatemsgs()
% generatemsgs creates and transmits can messages for demo purposes.
%
% generatemsgs periodically transmits multiple can messages at various
% periodic rates with changing message data.
%
% pg电子麻将胡了 copyright 2008-2016 the mathworks, inc.
% create the messages to send using the canmessage function. the
% identifier, an indication of standard or extended type, and the data
% length is given for each message.
msgtx100 = canmessage(100, false, 0);
msgtx200 = canmessage(200, false, 2);
msgtx400 = canmessage(400, false, 4);
msgtx600 = canmessage(600, false, 6);
msgtx800 = canmessage(800, false, 8);
% create a can channel on which to transmit.
txch = canchannel('mathworks', 'virtual 1', 1);
% register each message on the channel at a specified periodic rate.
transmitperiodic(txch, msgtx100, 'on', 0.500);
transmitperiodic(txch, msgtx200, 'on', 0.250);
transmitperiodic(txch, msgtx400, 'on', 0.125);
transmitperiodic(txch, msgtx600, 'on', 0.050);
transmitperiodic(txch, msgtx800, 'on', 0.025);
% start the can channel.
start(txch);
% run for several seconds incrementing the message data regularly.
for ii = 1:50
% increment the message data bytes.
msgtx200.data = msgtx200.data 1;
msgtx400.data = msgtx400.data 1;
msgtx600.data = msgtx600.data 1;
msgtx800.data = msgtx800.data 1;
% wait for a time period.
pause(0.100);
end
% stop the can channel.
stop(txch);
end
对于该示例,运行 generatemsgs 函数以传输报文。
generatemsgs();
接收报文
当 generatemsgs 完成时,使用 函数接收来自通道的所有可用报文。
rxmsg = receive(rxch, inf, "outputformat", "timetable");
使用 head 提取接收的报文的前几行进行预览。
head(rxmsg)
time id extended name data length signals error remote
___________ ___ ________ __________ ___________________ ______ ____________ _____ ______
0.31722 sec 100 false {0×0 char} {1×0 uint8 } 0 {0×0 struct} false false
0.31723 sec 200 false {0×0 char} {[ 0 0]} 2 {0×0 struct} false false
0.31723 sec 400 false {0×0 char} {[ 0 0 0 0]} 4 {0×0 struct} false false
0.31723 sec 600 false {0×0 char} {[ 0 0 0 0 0 0]} 6 {0×0 struct} false false
0.31723 sec 800 false {0×0 char} {[0 0 0 0 0 0 0 0]} 8 {0×0 struct} false false
0.34689 sec 800 false {0×0 char} {[1 1 1 1 1 1 1 1]} 8 {0×0 struct} false false
0.37728 sec 600 false {0×0 char} {[ 1 1 1 1 1 1]} 6 {0×0 struct} false false
0.37729 sec 800 false {0×0 char} {[1 1 1 1 1 1 1 1]} 8 {0×0 struct} false false
停止通道
使用 命令将通道设置为离线状态。
stop(rxch);
分析收到的报文
matlab® 为执行 can 报文分析提供功能强大的环境。plot 命令可以创建具有报文时间戳和标识符的散点图,以便概览某些报文何时出现在网络上。
plot(rxmsg.time, rxmsg.id, "x") ylim([0 2047]) xlabel("timestamp") ylabel("can identifier")
