CAN BUS tools
cantools is a Python 3 library for working with CAN (Controller Area Network) bus data. It provides extensive functionalities for parsing and interacting with various CAN database file formats, including DBC, KCD, SYM, ARXML (versions 3&4), and CDD. Key features include encoding and decoding CAN messages, handling simple and extended signal multiplexing, diagnostic DID encoding and decoding, and command-line tools for monitoring CAN bus traffic, generating C source code from databases, and visualizing signals. The library is actively maintained with regular updates.
Warnings
- breaking In version 39.0.0, the `initial` attribute of `Signal` objects was changed to always hold the scaled signal value. Previously, it used raw values for DBC files and scaled values for ARXML. Additionally, the machinery for storing decimal numbers without rounding errors (`*.decimal` attributes) was removed.
- gotcha cantools has weak or limited direct support for J1939 PGNs (Parameter Group Numbers). While basic decoding might work by splitting databases, for robust J1939 support, it's recommended to use the dedicated `python-can-j1939` package.
- gotcha Decoding CAN-FD messages or any CAN message with a payload length greater than 8 bytes using `database.decode_message()` might fail in some earlier versions or specific contexts, as the method was primarily designed for standard CAN messages (up to 8 bytes).
- deprecated Python 2 support was deprecated in older versions. The library now explicitly requires Python 3.10 and above.
- gotcha An older `diskcache` dependency (version < 5.0.2) used by `cantools` could cause installation issues on Python 2 environments. While `cantools` now requires Python 3.10+, if you encounter `diskcache`-related installation failures in a mixed or legacy setup, it might be due to this.
Install
-
pip install cantools
Imports
- cantools
import cantools
- load_file
import cantools.database db = cantools.database.load_file('path/to/your.dbc') - Database
from cantools.database import Database, Message, Signal
Quickstart
import cantools
import os
# A minimal DBC file content for demonstration
dbc_content = """
VERSION """
NS_ :
BS_:
BU_: Vector__XXX
BO_ 256 EXAMPLE_MESSAGE: 8 Vector__XXX
SG_ Signal1 : 0|8@1+ (1,0) [0|255] "Unit" Vector__XXX
SG_ Signal2 : 8|8@1+ (0.1,0) [0|25.5] "V" Vector__XXX
"""
# Load the database from a string (or use cantools.database.load_file('path/to/your.dbc'))
try:
db = cantools.database.load_string(dbc_content, database_format='dbc')
print("Database loaded successfully.")
# Get a message from the database
example_message = db.get_message_by_name('EXAMPLE_MESSAGE')
# Encode a message
data = example_message.encode({'Signal1': 100, 'Signal2': 12.5})
print(f"Encoded data for EXAMPLE_MESSAGE: {data.hex()}")
# Decode a message
decoded_data = db.decode_message(example_message.frame_id, data)
print(f"Decoded data: {decoded_data}")
except Exception as e:
print(f"An error occurred: {e}")
print("Note: For real-world usage, provide a valid .dbc file path.")