Get Started with CAN FD Communication in MATLAB
This example shows you how to use CAN FD channels to transmit and receive CAN FD messages. It uses MathWorks® virtual CAN FD channels connected in a loopback configuration.
View Available CAN FD Channels
Use canFDChannelList
to see all available device channels supporting CAN FD.
canFDChannelList
ans=2×6 table
Vendor Device Channel DeviceModel ProtocolMode SerialNumber
___________ ___________ _______ ___________ _____________ ____________
"MathWorks" "Virtual 1" 1 "Virtual" "CAN, CAN FD" "0"
"MathWorks" "Virtual 1" 2 "Virtual" "CAN, CAN FD" "0"
Create Transmitting and Receiving Channels
Use canFDChannel
with device details specified to create CAN FD channels for transmitting and receiving messages.
txCh = canFDChannel("MathWorks","Virtual 1",1)
txCh = Channel with properties: Device Information DeviceVendor: 'MathWorks' Device: 'Virtual 1' DeviceChannelIndex: 1 DeviceSerialNumber: 0 ProtocolMode: 'CAN FD' Status Information Running: 0 MessagesAvailable: 0 MessagesReceived: 0 MessagesTransmitted: 0 InitializationAccess: 1 InitialTimestamp: [0x0 datetime] FilterHistory: 'Standard ID Filter: Allow All | Extended ID Filter: Allow All' Bit Timing Information BusStatus: 'N/A' SilentMode: 0 TransceiverName: 'N/A' TransceiverState: 'N/A' ReceiveErrorCount: 0 TransmitErrorCount: 0 ArbitrationBusSpeed: 500000 DataBusSpeed: 2000000 Other Information Database: [] UserData: []
rxCh = canFDChannel("MathWorks","Virtual 1", 2);
Configure Bus Speed
CAN FD channels require setting of bus speed before going online. Both the arbitration and data phase speeds are configured using configBusSpeed
.
configBusSpeed(txCh, 500000, 1000000); configBusSpeed(rxCh, 500000, 1000000);
Open the DBC File
Use canDatabase
to open the database file that contains definitions of CAN FD messages and signals.
db = canDatabase("CANFDExample.dbc")
db = Database with properties: Name: 'CANFDExample' Path: '/tmp/Bdoc24b_2725827_3770987/tpf6b9d6b0/vnt-ex36915890/CANFDExample.dbc' UTF8_File: '/tmp/Bdoc24b_2725827_3770987/tpf6b9d6b0/vnt-ex36915890/CANFDExample.dbc' Nodes: {} NodeInfo: [0x0 struct] Messages: {'CANFDMessage'} MessageInfo: [1x1 struct] Attributes: {2x1 cell} AttributeInfo: [2x1 struct] UserData: []
Attach the database directly to the receiving channel. Definitions from the DBC file are automatically applied to decode incoming messages and signals.
rxCh.Database = db;
Start the Channels
Use the start
command to set the channels online.
start(txCh); start(rxCh);
Create CAN FD Messages
Create CAN FD messages using the canFDMessage
function.
msg1 = canFDMessage(500, false, 12)
msg1 = Message with properties: Message Identification ProtocolMode: 'CAN FD' ID: 500 Extended: 0 Name: '' Data Details Timestamp: 0 Data: [0 0 0 0 0 0 0 0 0 0 0 0] Signals: [] Length: 12 DLC: 9 Protocol Flags BRS: 0 ESI: 0 Error: 0 Other Information Database: [] UserData: []
msg2 = canFDMessage(1000, false, 24); msg3 = canFDMessage(1500, false, 64);
To engage the bit rate switch capability of CAN FD, set the BRS
property of the messages.
msg1.BRS = true; msg2.BRS = true; msg3.BRS = true;
CAN FD messages can also be created using a database. The database defines if a message is CAN or CAN FD as well as the BRS status.
msg4 = canFDMessage(db,"CANFDMessage")
msg4 = Message with properties: Message Identification ProtocolMode: 'CAN FD' ID: 1 Extended: 0 Name: 'CANFDMessage' Data Details Timestamp: 0 Data: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] Signals: [] Length: 48 DLC: 14 Protocol Flags BRS: 1 ESI: 0 Error: 0 Other Information Database: [1x1 can.Database] UserData: []
Transmit Messages
Use transmit
to send the created messages from the transmitting channel.
transmit(txCh, [msg1 msg2 msg3 msg4])
Receive Messages
Receive the messages from the receiving channel using the receive
function. The default return type for CAN FD channels is a timetable containing information specific to the received CAN FD messages.
rxMsg = receive(rxCh, Inf)
rxMsg=4×12 timetable
Time ID Extended Name ProtocolMode Data Length DLC Signals Error Remote BRS ESI
___________ ____ ________ ________________ ____________ ___________________________________________________________________________________________________________________________________ ______ ___ ____________ _____ ______ _____ _____
0.13471 sec 500 false {0x0 char } {'CAN FD'} {[ 0 0 0 0 0 0 0 0 0 0 0 0]} 12 9 {0x0 struct} false false true false
0.13471 sec 1000 false {0x0 char } {'CAN FD'} {[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]} 24 12 {0x0 struct} false false true false
0.13471 sec 1500 false {0x0 char } {'CAN FD'} {[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]} 64 15 {0x0 struct} false false true false
0.13471 sec 1 false {'CANFDMessage'} {'CAN FD'} {[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]} 48 14 {1x1 struct} false false true false
Stop the Channels
Use the stop
command to set the channels offline.
stop(txCh); stop(rxCh);
Close the DBC File
Close access to the DBC file by clearing its variable from the workspace.
clear db