CAN
Controller Area Network is the backbone of in-vehicle communication. Learn the differential bus and termination, frame format (identifier, DLC, data, CRC, ACK), arbitration by priority, bit timing, error handling and bus-off, bus-load calculation, DBC signal databases and CAN FD.
Since the late 1980s, CAN has let ECUs share data over two wires instead of point-to-point wiring. It's robust against noise, prioritises urgent messages automatically, and detects errors reliably — the reason it's still in every car.
Engineers design message schedules so buses stay below safe load, and use tools like CANalyzer and DBC files to decode signals.
An analogy
CAN is like a meeting where anyone may speak, but if two start at once, the one with the higher-priority title automatically continues and the other politely waits — nobody's words get garbled.
Physical layer
Two twisted wires, CAN_H and CAN_L, carry a differential signal — noise affects both equally and cancels. Recessive (logical 1): both ≈ 2.5 V; dominant (logical 0): CAN_H ≈ 3.5 V, CAN_L ≈ 1.5 V. 120 Ω terminators at both ends prevent reflections (measure 60 Ω across the bus when powered off).
Frames and arbitration
A data frame: SOF, identifier, control (DLC), up to 8 data bytes, 15-bit CRC, ACK, EOF. All nodes transmit identifier bits simultaneously; a dominant 0 overwrites a recessive 1, so the lowest identifier (highest priority) wins without collision — non-destructive arbitration.
Error handling
Bit, stuff, CRC, form and ACK errors are detected; the detecting node sends an error frame and the message is retransmitted. Error counters move nodes from error-active to error-passive to bus-off, isolating faulty nodes automatically.
Scheduling, CAN FD and CAN XL
Bus load is typically kept below 50–70 % for latency margin; worst-case response-time analysis verifies deadlines. CAN FD raises data-phase bit rate to 2–8 Mbit/s and payload to 64 bytes; CAN XL reaches 10–20 Mbit/s and 2048 bytes. Security via SecOC message authentication.
Formulas
- Frame length (11-bit ID, 8 bytes):
≈ 111 bits + stuff bits (worst ≈ 135) → ≈ 125 bits typical - Frame time:
t = bits / bit rate - Bus load:
U = Σ (frames/s × bits/frame) / bit rate - Termination:
2 × 120 Ω in parallel = 60 Ω between CAN_H and CAN_L
Worked example: Bus load
Given: 500 kbit/s bus; 20 messages every 10 ms + 30 messages every 100 ms; ≈ 125 bits per frame
Find: Bus load.
- Frames/s = 20 × 100 + 30 × 10 = 2300
- Bits/s = 2300 × 125 = 287 500
- U = 287.5/500 = 57.5 %
Answer: ≈ 58 % — acceptable but little room to add messages
Worked example: Arbitration
Given: Node A sends ID 0x120, node B sends ID 0x0C8 simultaneously
Find: Who wins?
- Lower ID = higher priority
- 0x0C8 < 0x120 → node B wins; A retries after the frame
Answer: Node B (0x0C8)
Sending a frame with Linux SocketCAN (C)
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
int main(void) {
int s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
struct ifreq ifr; strcpy(ifr.ifr_name, "can0");
ioctl(s, SIOCGIFINDEX, &ifr);
struct sockaddr_can addr = { .can_family = AF_CAN, .can_ifindex = ifr.ifr_ifindex };
bind(s, (struct sockaddr *)&addr, sizeof addr);
struct can_frame f = { .can_id = 0x0C8, .can_dlc = 8 };
uint16_t rpm_raw = 3000 * 4; /* signal scaling 0.25 rpm/bit */
f.data[0] = rpm_raw & 0xFF; /* little-endian (Intel) signal */
f.data[1] = rpm_raw >> 8;
write(s, &f, sizeof f);
close(s);
return 0;
}The DBC file defines each signal's start bit, length, byte order, scale and offset — both sender and receiver must agree.
DBC signal definition
BO_ 200 EngineData: 8 ECM
SG_ EngineSpeed : 0|16@1+ (0.25,0) [0|16383.75] "rpm" TCM,BCM,IC
SG_ CoolantTemp : 16|8@1+ (1,-40) [-40|215] "degC" IC
SG_ ThrottlePos : 24|8@1+ (0.4,0) [0|102] "%" TCMMessage 200 (0x0C8), 8 bytes from the ECM: EngineSpeed starts at bit 0, 16 bits, Intel byte order, unsigned, 0.25 rpm/bit.
Common mistakes
- Wrong: Missing or extra termination resistors. — Right: Exactly two 120 Ω terminators — check for 60 Ω.
- Wrong: Loading the bus to 90 %. — Right: Keep headroom (< 50–70 %) for latency, retransmissions and future messages.
Applications
- Powertrain & chassis buses: Engine, transmission, ABS/ESC data at 500 kbit/s.
- Diagnostics: OBD-II and UDS run over CAN (ISO 15765).
- Trucks & off-highway: J1939 (29-bit IDs) standardises heavy-vehicle messages.
Key takeaways
- Differential two-wire bus, 120 Ω at each end.
- Lowest ID wins arbitration — priority by ID.
- Robust error detection and fault confinement.
- Keep bus load < ~60 %; use CAN FD for more data.
Going further
- Worst-case response time analysis: Computes latency including blocking and interference for each message ID.
- SecOC: AUTOSAR secure onboard communication adds MACs and freshness values.
- CAN XL: Bridges CAN and Ethernet with up to 2048-byte payloads.