# Pure Python 3 CAN decoder for a 8-byte payload.
FRAME_LEN = 8
def dbc_1_min_cell_voltage_physical(data: bytes) -> float:
if len(data) < 8:
raise ValueError("Expected at least 8 payload bytes")
raw = (int.from_bytes(data[:8], "little") >> 0) & 0xFFFF
return raw * (1.0) + (0.0)
def dbc_2_max_cell_voltage_physical(data: bytes) -> float:
if len(data) < 8:
raise ValueError("Expected at least 8 payload bytes")
raw = (int.from_bytes(data[:8], "little") >> 16) & 0xFFFF
return raw * (1.0) + (0.0)
def dbc_3_pack_temp_physical(data: bytes) -> float:
if len(data) < 8:
raise ValueError("Expected at least 8 payload bytes")
raw = (int.from_bytes(data[:8], "little") >> 32) & 0xFF
return raw * (1.0) + (-40.0)
def dbc_4_state_of_charge_physical(data: bytes) -> float:
if len(data) < 8:
raise ValueError("Expected at least 8 payload bytes")
raw = (int.from_bytes(data[:8], "little") >> 40) & 0xFF
return raw * (1.0) + (0.0)
def dbc_5_status_flags_physical(data: bytes) -> float:
if len(data) < 8:
raise ValueError("Expected at least 8 payload bytes")
raw = (int.from_bytes(data[:8], "little") >> 48) & 0xFFFF
return raw * (1.0) + (0.0)
def decode_frame(data: bytes) -> dict[str, float]:
return {
"dbc_1_min_cell_voltage": dbc_1_min_cell_voltage_physical(data),
"dbc_2_max_cell_voltage": dbc_2_max_cell_voltage_physical(data),
"dbc_3_pack_temp": dbc_3_pack_temp_physical(data),
"dbc_4_state_of_charge": dbc_4_state_of_charge_physical(data),
"dbc_5_status_flags": dbc_5_status_flags_physical(data),
}
Decode min/max cell millivolts and BMS fault registers before closing main contactors
Industrial EV, marine and 29-bit J1939/CANopen BMS telemetry frames pack the weakest and strongest cell voltages side by side in bytes 0–3 alongside an offset-40 °C temperature byte, SOC byte, and 16-bit protection bitmask.
Validating exact byte ordering on captured hex frames before coding inverter/VCU derating logic prevents misreading 0x0C94 (3,220 mV) as a catastrophic cell overvoltage fault.
Formulas
Min cell = 0x0C94 (3,220 mV); Max cell = 0x0CE4 (3,300 mV) → Δ = 80 mV
Fault preset = 3,460 mV min vs. 3,640 mV max (Δ180 mV), 32 °C, flags 0x15
Worked example
Hex frame 94 0C E4 0C 41 58 03 00 unpacks to 3,220 mV min cell, 3,300 mV max cell, 25 °C pack temperature, 88% SOC and 0x0003 status bits alongside portable C99 inline helpers.
Frequently asked questions
Does the 8-byte data payload layout change between 11-bit standard and 29-bit extended CAN IDs?
No—11-bit vs. 29-bit only extends the arbitration header identifier; the 8-byte (or 16-byte CAN FD) signal payload still decodes identically using Intel or Motorola bit numbering.
Why do automotive and BMS CAN specs commonly add an offset of −40 to temperature bytes?
Subtracting 40 lets a single unsigned 8-bit byte (0–255) cleanly express temperatures from −40 °C up to +215 °C without signed two’s-complement ambiguity.
How do I separate individual alarm flags inside the 16-bit Status flags integer?
Mask the decoded DBC_5_STATUS_FLAGS_RAW(data) integer with (1u << bit_index) in C99 firmware to inspect overvoltage, undervoltage, overcurrent or FET state bits individually.