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 cantoolsVerified import paths — ran on the pinned version, not inferred.
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`.
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.
For comprehensive J1939 protocol handling, consider using the `python-can-j1939` library which provides more complete SAE J1939 support.
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.
Ensure your environment is running Python 3.10 or a newer compatible version. Upgrade your Python interpreter if necessary.
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`.
Install the library using pip: `pip install cantools`.
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.
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)`.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.