CAN Bus Technology Communication Network and Distributed Systems
Getting Started with CAN Bus: A Simple Guide to Automotive Communication
Hello everyone, I am Lao Dao. Today, let’s talk about the “internet celebrity” communication protocol in the industrial and automotive fields—CAN Bus. Although it sounds sophisticated, it is essentially a set of rules that allows different devices to “talk” to each other. Whether it’s the engine controller, transmission, or various sensors in industrial settings, they all rely on it to exchange messages.
What is CAN?
In simple terms, CAN is like a “telephone line” combined with a set of “intercom rules”. It was originally developed by Bosch in Germany for automobiles to reduce the clutter of wiring harnesses. A single CAN bus can connect up to 112 devices, allowing them to communicate over just one wire, much like a group chat in WeChat.
Regarding speed, there are two standards for CAN Bus:
-
Standard CAN: Up to 1Mbps (typically within 100 meters)
-
Low-speed CAN: Up to 125Kbps (can reach about 500 meters)
How Simple is the Hardware Connection?
The CAN bus only requires two wires to operate, called CANH and CANL:
Device A Device B Device C
| | |
|---------|---------| CANH
| | |
|---------|---------| CANL
| | |
End Resistor 120Ω End Resistor 120Ω
Important Reminder: 120Ω resistors must be added at both ends! This is like adding flow valves at the ends of a water pipe to prevent signal reflections. Without these resistors, communication errors can easily occur.
Data Frame Structure: The “Speaking Method” of CAN
Every message on the CAN bus is sent in a fixed format, just like how you must clearly write the recipient’s address when sending a package:
-
Frame Start Bit: Equivalent to saying “Hello”, indicating that I want to speak
-
Identifier (ID): 11-bit or 29-bit, determines the message’s priority
-
Data Length: Up to 8 bytes
-
Data Field: The actual content to be transmitted
-
CRC Check: Ensures data transmission is correct
-
Frame End Bit: Indicates that this message is complete
Practical Case: Temperature Monitoring System
Suppose we want to create a temperature monitoring system for a workshop, requiring the collection of temperature data from three points:
// Example code for the sender (based on STM32)
void CAN_Send_Temp(float temp, uint8_t sensor_id)
{
CAN_TxHeaderTypeDef TxHeader;
uint8_t TxData[8];
// Configure the sending frame
TxHeader.StdId = 0x200 + sensor_id; // Base ID for temperature sensor
TxHeader.IDE = CAN_ID_STD; // Standard frame
TxHeader.RTR = CAN_RTR_DATA; // Data frame
TxHeader.DLC = 4; // Send 4 bytes
// Convert temperature value to bytes
uint32_t temp_int = *(uint32_t*)&temp
TxData[0] = (temp_int >> 24) & 0xFF;
TxData[1] = (temp_int >> 16) & 0xFF;
TxData[2] = (temp_int >> 8) & 0xFF;
TxData[3] = temp_int & 0xFF;
// Send data
HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox);
}
Notes:
-
Do not send too frequently; generally, once every 100ms is sufficient
-
Data conversion should consider byte order issues
-
It is advisable to include a timeout handling mechanism
Common Problems and Solutions
-
Unstable Communication
-
Check if the terminal resistors are installed
-
Ensure the baud rate settings are consistent
-
Measure the voltage difference between CANH and CANL to see if it is normal (should be around 2.5V)
-
Occasional Packet Loss
-
It may be an EMC issue; check the grounding of the shielding layer
-
Check for any damage to the cables
-
Check if the bus load is too high
-
Error on Startup
-
Check the power-up timing sequence
-
Ensure that the initialization configuration is correct
-
Check for device address conflicts
Recommended Debugging Tools
-
USB-CAN Converter: Recommended to use PEAK-System’s PCAN-USB, which supports hot swapping
-
Oscilloscope: Preferably with more than 4 channels, to observe CANH and CANL simultaneously
-
CANoe Software: Powerful, but expensive
-
Cost-effective choice: Zhou Ligong’s USBCAN, paired with their upper computer software can meet basic debugging needs
Practical Suggestions
The first step can be to buy a USB-CAN module and familiarize yourself with the basic operations of CAN using upper computer software. Once you master the process of sending and receiving data, try implementing specific functions using a microcontroller. Remember to keep a record of each debugging session, including various error messages, as these are valuable experiences for solving future problems.
Lastly, a few words: The essence of CAN bus technology lies in its arbitration mechanism and error handling capabilities. It acts like a well-trained dispatcher, adept at handling emergencies and knowing how to prioritize important messages. In practical applications, correct hardware connections and reasonable protocol design are more important than writing code.
Safety Reminder: When debugging CAN bus in automotive electronic systems, always do so with the vehicle stationary and the power disconnected to avoid accidents. In industrial settings, take care to prevent static electricity from damaging equipment.