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: [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: []
            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: 'C:\TEMP\Bdoc18a_805738_11608\ib4C766E\15\tp75927dcd\ex36915890\CANFDExample.dbc'
            Nodes: {}
         NodeInfo: [0x0 struct]
         Messages: {'CANFDMessage'}
      MessageInfo: [1x1 struct]
       Attributes: {2x1 cell}
    AttributeInfo: [2x1 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 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 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: [1x48 uint8]
         Signals: []
          Length: 48
             DLC: 14

   Protocol Flags
             BRS: 1
             ESI: 0
           Error: 0

   Other Information
        Database: [1x1 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 =

  4x12 timetable

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

    0.071194 sec     500     false      ''                  'CAN FD'      [1x12 uint8]      12       9     [0x0 struct]    false    false     true     false
    0.071197 sec    1000     false      ''                  'CAN FD'      [1x24 uint8]      24      12     [0x0 struct]    false    false     true     false
    0.071199 sec    1500     false      ''                  'CAN FD'      [1x64 uint8]      64      15     [0x0 struct]    false    false     true     false
    0.071201 sec       1     false      'CANFDMessage'      'CAN FD'      [1x48 uint8]      48      14     [1x1 struct]    false    false     true     false

Stop the Channels

Use the stop command to set the channels offline.

stop(txCh);
stop(rxCh);