MATLAB Examples

Using CAN FD Channels and Messages

This example shows you how to use CAN FD channels to transmit and receive CAN FD messages. It uses MathWorks Virtual CAN channels connected in a loopback configuration.

Contents

Create Transmitting and Receiving Channels

Create CAN FD channels to send and receive messages by specifying the device details as well as the ProtocolMode.

txCh = canChannel('MathWorks', 'Virtual 1', 1, 'ProtocolMode', 'CAN FD')
rxCh = canChannel('MathWorks', 'Virtual 1', 2, 'ProtocolMode', 'CAN FD');
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: [0×0 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: []
            DataBusSpeed: []

   Other Information
                Database: []
                UserData: []

Configure Bus Speed

CAN FD channels require setting of bus speed before going online. Both the arbitration and data phase speeds are configured.

configBusSpeed(txCh, 500000, 1000000);
configBusSpeed(rxCh, 500000, 1000000);

Open a CAN Database File

CAN database files specify CAN FD message.

db = canDatabase('CANFDExample.dbc')
rxCh.Database = db;
db = 

  Database with properties:

             Name: 'CANFDExample'
             Path: '\\central-mi\home\jpyle\documents\MATLAB\examples\vnt-ex36915890\CANFDExample.dbc'
            Nodes: {}
         NodeInfo: [0×0 struct]
         Messages: {'CANFDMessage'}
      MessageInfo: [1×1 struct]
       Attributes: {2×1 cell}
    AttributeInfo: [2×1 struct]
         UserData: []

Start the Channels

Use the start command to set the channels online.

start(txCh);
start(rxCh);

Create CAN FD Messages

CAN FD messages are created also using the ProtocolMode.

msg1 = canMessage(500, false, 12, 'ProtocolMode', 'CAN FD')
msg2 = canMessage(1000, false, 24, 'ProtocolMode', 'CAN FD');
msg3 = canMessage(1500, false, 64, 'ProtocolMode', 'CAN FD');

% To engage the bit rate switch capabilty 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 file. The database defines if a message is CAN or CAN FD. BRS status is also taken from the database.
msg4 = canMessage(db, 'CANFDMessage')
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: []


msg4 = 

  Message with properties:

   Message Identification
    ProtocolMode: 'CAN FD'
              ID: 1
        Extended: 0
            Name: 'CANFDMessage'

   Data Details
       Timestamp: 0
            Data: [1×48 uint8]
         Signals: []
          Length: 48
             DLC: 14

   Protocol Flags
             BRS: 1
             ESI: 0
           Error: 0

   Other Information
        Database: [1×1 can.Database]
        UserData: []

Transmit Messages

Use the transmitting channel to send the messages.

transmit(txCh, [msg1 msg2 msg3 msg4])

Receive Messages

Receive the messages from the channel. Note that the return type for CAN FD channels is a timetable, containing additional information specific to CAN FD.

rxMsg = receive(rxCh, Inf)
rxMsg =

  4×12 timetable

       Time        ID     Extended         Name         ProtocolMode        Data        Length    DLC      Signals       Error    Remote     BRS      ESI 
    __________    ____    ________    ______________    ____________    ____________    ______    ___    ____________    _____    ______    _____    _____

    5.1283 sec     500    false       ''                'CAN FD'        [1×12 uint8]    12         9     [0×0 struct]    false    false     true     false
    5.1283 sec    1000    false       ''                'CAN FD'        [1×24 uint8]    24        12     [0×0 struct]    false    false     true     false
    5.1283 sec    1500    false       ''                'CAN FD'        [1×64 uint8]    64        15     [0×0 struct]    false    false     true     false
    5.1283 sec       1    false       'CANFDMessage'    'CAN FD'        [1×48 uint8]    48        14     [1×1 struct]    false    false     true     false

Stop the Channels

Use the stop command to set the channels offline.

stop(txCh);
stop(rxCh);