CAN Bus IDs
There are two types of CAN Bus IDs. In the first version of the CAN Bus spec (2.0A) the IDs were made up of 11 bits. These are also known as base frame format messages. This allows for message IDs between 0x000 and 0x7FF. Which works out to be 2048 possible CAN IDs.
Binary, Decimal and Hex representation of the max 11 bit ID |
Binary, Decimal and Hex representation of the max 29 bit ID |
11 bit code example
Let's take a look at a very simple example of using an MCP2515 CAN controller with an Arduino and 11 bit CAN IDs. I am using a Seeed Studio CAN-BUS Shield but the MCP2515 is just an SPI chip that can be wired up with minimal external components.
In this 11 bit example I am using a CAN ID of 0x07B in hex (123 in decimal).
Here you can see my receiver Arduino is receiving CAN messages with an ID of 123.
29 bit code example
Next let's look at an example of sending 29 bit CAN IDs. In this example I am using a CAN ID of 0x17F8140E in hex (or 402134030 in decimal).
Here you can see my receiver Arduino is receiving CAN messages with an ID of 402134030.
sendMsgBuf format
The key piece of this example code is the sendMsgBuf function. This function is part of theMCP_CAN_lib. This is where messages are flagged as 11 bit or 29 bit.
11 bit: CAN.sendMsgBuf(0x07B, 0, 8, canMsg);
29 bit: CAN.sendMsgBuf(0x17F8140E, 1, 8, canMsg);
The format for this function is sendMsgBuf(can_id, id_type, dlc, data_buf)
can_id - id number for your message in hex or decimal.
id_type - flag for 11 or 29 bit message id. 0=11bit, 1=29bit
dlc - Number of bytes in the message data
data_buf - The data transmitted in the message.
If you are going to try out these examples you should download Cory Fowler's fork of the MCP_CAN_lib.
Resources
If you are going to try out these examples you should download Cory Fowler's fork of the MCP_CAN_lib.
Resources