Skip to content

CAN Message

All message classes have a ToRaw() and a FromRaw() function that serialize and deserialize the message. The messages are pushed or popped from a message queue. The message queues are list of smart pointers. To create a message, use the following code.

#include <bus/candataframe.h>

auto msg = std::make_shared<CanDataFrame>
// Set the properties and message data (payload).

publisher->Push(msg);
Each CAN message, have a Time Stamp and a Bus Channel property. The Timestamp is always referencing nanoseconds since 1970-01-01 UTC (64-bit unsigned integer). The bus channel is 1–255 and defines the source of the message.

Warning

Each CAN message has a unique ID which in someway is a little bit unique. The CAN Message ID is the CAN ID with the 31:th bit indicating that the ID is extended. It is a common mistake to blend the CAN and Message ID within the code.

CAN Data Frame

The standard Data Frame message is the normal message on a functional CAN or CAN-FD bus. The CAN message can hold up to eight bytes while the CAN-FD message can hold up to 64 bytes of payload data.

CAN Data Frame Message Layout
Byte (:Bits)DescriptionSize
0-17Message Header18 bytes
18-21Message ID (CAN Id + IDE)uint32_t
22DLCuint8_t
23Data Lengthuint8_t
24-27CRCuint32_t
28:0Direction (Rx=0, Tx=1)1-bit
28:1SRR1-bit
28:2EDL1-bit
28:3BRS1-bit
28:4ESI1-bit
28:5RTR1-bit
28:6R01-bit
28:7R11-bit
29:0Wake Up1-bit
29:1Single Wire1-bit
30-33Frame Duration (ns)/td>uint32_t
34-xxData BytesData Length bytes

Message ID

The Message ID is a 32-bit value that includes an 11/27-bits CAN ID and an Extended Address flag at the highest bit. The Extended flag defines if it is a 27-bits or an 11-bits address. Yes, this is confusing, but this is industry standard.

Data Length Code (DLC)

The data length code (DLC) is somewhat an artifact. The number of bytes and DLC are identical for CAN buses, but CAN-FD uses different code for more than 8-bytes payload bytes.

Hint

The class sets the DLC and Data Length properties automatically when the payload data is set.

Checksum (CRC)

The checksum is rarely used and is optional to use.

Direction

The Direction flag defines if the source (channel) was sending this message or receiving it. This is actually an enumerated value where 0 is Rx and 1 is Tx.

Substitute Remote Request (SRR)

You need to ask a CAN expert about this flag.

Extended Data Length (EDL)

The CAN-FD buses sets this flag to define that this is a CAN-FD frame.

Bit Rate Switch (BRS)

Used by the CAN-FD bus.

Error State Indicator (ESI)

Used by the CAN-FD bus.

Remote Frame (RTR)

Remote Frame flag defines that this is a remote frame. There is actually a special Remote Frame message defined. Remote Frame doesn't include any payload data.

Reserved Bit 0 and 1

Undefined was these bits are used for.

Wake Up

Flag that indicates that this was a wake-up message.

Single Wire Detected

Some hardware can detect if only one pin was used to detect the message.

Frame Duration

Frame Duration of the message in nanoseconds.

CAN Remote Frame

The Remote Frame is almost identical with the Data Frame but with the RTR flag set. There is no payload data sent with this message.

CAN Error Frame

The Error Frame is a normal Data Frame with error information.

CAN Overload Frame

Seldom used message and is used mainly as an indication.