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);
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.
Byte (:Bits) | Description | Size |
---|---|---|
0-17 | Message Header | 18 bytes |
18-21 | Message ID (CAN Id + IDE) | uint32_t |
22 | DLC | uint8_t |
23 | Data Length | uint8_t |
24-27 | CRC | uint32_t |
28:0 | Direction (Rx=0, Tx=1) | 1-bit |
28:1 | SRR | 1-bit |
28:2 | EDL | 1-bit |
28:3 | BRS | 1-bit |
28:4 | ESI | 1-bit |
28:5 | RTR | 1-bit |
28:6 | R0 | 1-bit |
28:7 | R1 | 1-bit |
29:0 | Wake Up | 1-bit |
29:1 | Single Wire | 1-bit |
30-33 | Frame Duration (ns)/td> | uint32_t |
34-xx | Data Bytes | Data 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.