主要内容

Event-Based CAN Communication in MATLAB

This example shows you how to configure CAN channels and messages for transmit messages on event. It uses MathWorks virtual CAN channels connected in a loopback configuration.

As this example is based on sending and receiving CAN messages on a virtual network, running CAN Explorer in conjunction may provide a more complete understanding of what the code is doing. To run CAN Explorer, open and configure it to use the same interface as the receiving channel of the example. Make sure to start CAN Explorer before beginning to run the example in order to see all of the messages as they occur.

This example describes the workflow for a CAN network, but the concept demonstrated also applies to a CAN FD network.

Create the CAN Channels

创建可以渠道消息反式mission and reception.

txCh = canChannel("MathWorks",“虚拟1”,1);rxCH =横annel("MathWorks",“虚拟1”, 2);

打开包含消息和信号定义的DBC文件,并将其连接到两个罐通道。

db = canDatabase(“ candatabaseevent.dbc”); txCh.Database = db; rxCh.Database = db;

Create the CAN Message

创建可以消息EngineMsgusing the database information.

msgenginemsg = canmessage(db,"EngineMsg")
msgenginemsg =带有属性的消息:消息标识协议模式:'can'id:100扩展:0名称:'enginemsg'数据详细信息时间戳时间戳:0数据:[0 0 0 0 0 0 0 0 0 0 0 0 0 0]信号:[1x1 struct]长度:8协议标志错误:0远程:0其他信息数据库:[1x1 can.database] userData:[]

Configure the Message for Event-Based Transmission

要启用有关基于事件的传输的消息,请使用transmitEventcommand specifying the transmitting channel, the message to register on the channel, and a state value.

TransmiteVent(TXCH,MSGENGINEMSG,"On");

开始基于事件的传输

Start the receiving and transmitting channels.

start(rxCh); start(txCh);

Write new values to the数据property and directly to theVehicleSpeedsignal to trigger automatic event-based transmission of the message onto the CAN bus.

msgEngineMsg.Data = [250 100 0 0 20 0 0 0]; pause(1); msgEngineMsg.Signals.VehicleSpeed = 60; pause(1);

Stop the transmitting and receiving channels.

停止(TXCH);停止(rxch);

Analyze the Behavior of Event-Based Transmission

The receiving channel now has two messages available, corresponding to the two updates that resulted in two transmissions.

rxCh.MessagesAvailable
ans = 2

Receive the available messages. Inspect the messages and note that each has the data values set previously to the数据property.

msgrx =接收(rxch,inf,"OutputFormat","timetable")
msgrx =2×8 timetableTime ID Extended Name Data Length Signals Error Remote ____________ ___ ________ _____________ ________________________ ______ ____________ _____ ______ 0.021303 sec 100 false {'EngineMsg'} {[250 100 0 0 20 0 0 0]} 8 {1x1 struct} false false 1.0278 sec 100 false {'EngineMsg'} {[250 100 0 0 60 0 0 0]} 8 {1x1 struct} false false

检查信号,并注意第二个实例VehicleSpeed以前将数据值设置为VehicleSpeedsignal.

signals = canSignalTimetable(msgRx)
signals=2×2 timetableTime VehicleSpeed EngineRPM ____________ ____________ _________ 0.021303 sec 20 2835 1.0278 sec 60 2835

查看为基于事件的传输配置的消息

To see messages configured on the transmitting channel for automatic transmission, use thetransmitConfigurationcommand.

发射配置(TXCH)
Periodic Messages None Event Messages ID Extended Name Data --- -------- --------- --------------------- 100 false EngineMsg 250 100 0 0 60 0 0 0

关闭频道和DBC文件

通过从工作区清除其变量,可以密切访问通道和DBC文件。

清除rxChtxCh清除db