Registry / serialization / cantools

cantools

JSON →
library43.0.2pypypi✓ verified 23d ago

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.

pip install cantools
INSTALL
IMPORT
SIG · CANTOOLS
C
cantools
serializationpythonv43.0.2
Install
2.8s avg
Import
514ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v43.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.546s · 23.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.8s · import 0.482s · 24MB
22MB installed
● package 22MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

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
For manually constructing a CAN database in Python.

This quickstart demonstrates how to load a CAN database (either from a string or file), encode Python dictionary data into a CAN message payload, and then decode a CAN message payload back into human-readable signal values. For actual CAN bus communication, the `python-can` library is commonly used in conjunction with `cantools`.

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.")
cantools --version
Debug
Known issues
breakingIn 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.
fix
Review code accessing `Signal.initial` or `*.decimal` attributes. Ensure you are working with scaled values for `initial`. For precise comparisons, use `Database.is_similar()` instead of direct equality checks after load-store-load cycles.
affects: >=39.0.0
gotchacantools 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.
fix
For comprehensive J1939 protocol handling, consider using the `python-can-j1939` library which provides more complete SAE J1939 support.
affects: All versions
gotchaDecoding 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).
fix
Ensure you are using a recent version of `cantools` (>=40.0.0) which may have improved CAN-FD handling. If issues persist, consider manually parsing extended data payloads or consulting specific `cantools` examples for CAN-FD if available.
affects: <40.0.0 (potentially some earlier minor versions)
deprecatedPython 2 support was deprecated in older versions. The library now explicitly requires Python 3.10 and above.
fix
Ensure your environment is running Python 3.10 or a newer compatible version. Upgrade your Python interpreter if necessary.
affects: <35.0.0 (Python 2 supported), >=3.10.0 (Python 3.10+ required)
gotchaAn 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.
fix
Ensure `diskcache` is updated to a compatible version (>=5.0.2 if manually managing) or simply use a clean Python 3.10+ environment with `pip install cantools`.
affects: Older versions of cantools, especially when used with Python 2.x environments. Less relevant for current Python 3.10+ requirement.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'cantools'
The 'cantools' package is not installed in the Python environment being used, or the Python interpreter cannot find it.
fix
Install the library using pip: `pip install cantools`.
cantools.database.errors.EncodeError: The signal "<signal_name>" is required for encoding.
When attempting to encode a CAN message, the provided dictionary of signal values is missing one or more signals that are defined as required in the DBC file for that message (e.g., a multiplexer switch signal or a non-multiplexed signal). This often occurs when the database is loaded with `strict=True`.
fix
Ensure that the dictionary passed to the `encode` or `encode_message` method contains values for all signals required by the message definition in the DBC file, including the multiplexer switch signal if the message is multiplexed.
cantools.database.errors.Error: The signals <signal_X> and <signal_Y> are overlapping in message <message_name>.
The loaded DBC file contains message definitions where two or more signals occupy overlapping bit positions, or the message data length is inconsistent with the DBC definition, and the database was loaded with the default `strict=True` parameter.
fix
Correct the DBC file to remove any overlapping signal definitions or inconsistencies. Alternatively, load the database with `strict=False` to ignore these errors during parsing, e.g., `db = cantools.database.load_file('your.dbc', strict=False)`.
KeyError: <frame_id_value>
When calling `db.decode_message(frame_id, data)`, the provided `frame_id` does not correspond to any message defined in the loaded CAN database. This can also happen if the database was programmatically modified (e.g., messages added/removed) without refreshing its internal lookup tables.
fix
Verify that the `frame_id` you are trying to decode exists in your DBC file. If you have modified the database object programmatically after loading, call `db.refresh()` to update its internal state before attempting to decode messages.
Upgrade
Version history
43.0.2latest on PyPI · released Aug 27, 2026
Audit
Dependencies
python-canoptionalRequired for interacting with live CAN buses (sending and receiving messages), used in many examples.
matplotliboptionalRequired for the 'plot' subcommand to visualize signals.
Agent activity
57 hits · last 30 days
node
52
OpenAI (training)
1
Resources
cantools — pip install cantools · libregistry