Software
cantools
By cantools, from github.com/cantools/cantools (opens in a new tab)
- Cars
- Library
- MIT
Python 3 tools for CAN bus work: parsing CAN database files, encoding and decoding CAN messages, and generating C code. It also includes a CAN bus monitor and signal plotting.
Facts
- GitHub stars
- 2,332read on 27 Nov 2026
- Last update
- 20 Nov 2026Newest commit on master
- Maintenance
- Activea commit in the last 90 days
- Difficulty
- Intermediate
- Hardware it needs, as the project states it
- Requires a computer with Python 3.
Checked on GitHub on 27 Nov 2026.
Licenses
- Code
- MIT (opens in a new tab)
Read by Robotheca from LICENSE (opens in a new tab) (MIT) at commit a2a35a6 (opens in a new tab), on 20 Nov 2026.
- Commercial use
- Yes
At a glance
- What it is
- Parses DBC, KCD, SYM, ARXML 3 and 4, and CDD database files.
- Encodes and decodes CAN messages, including simple and extended signal multiplexing.
- Decodes diagnostic DID data and frames captured with candump.
- Generates C source code (structs, pack/unpack and encode/decode functions) from a database.
- Provides a text-based CAN bus monitor and a command line tool for dumping, listing and plotting databases and traffic.
- Can be used from Python scripts, with python-can for sending and receiving messages.
Robotheca's summary of the project's documents, not the creator's words.
- Robots on Robotheca
- No robot on Robotheca uses it yet.
- Licence
- MIT. Commercial use: Yes. Where it was read
- Install
- In the creator's words: The project's README (opens in a new tab)
- Maintenance
- Active, last commit on 20 Nov 2026.
Use it
How to install it, in the creator's words: The project's README (opens in a new tab)
Robotheca doesn't review code. Read it before running it.
Files and versions
- Repository on GitHubgithub.com/cantools/cantools (opens in a new tab)
- The version Robotheca reada2a35a6 (opens in a new tab)
- Latest release45.2.0 (17 Nov 2026)
Robotheca read the repository at two versions: the summary at 8d88413 and the facts and the README at a2a35a6 (read on 27 Nov 2026).
Robotheca never hosts the code: each link goes to the creator's own page.
The README
The creator's own README, copied as written: README.rst by cantools, at commit a2a35a6, under MIT (opens in a new tab). The original on GitHub (opens in a new tab).
Read the README here
|github-actions| |coverage|
.. |github-actions| image:: https://github.com/cantools/cantools/actions/workflows/pythonpackage.yml/badge.svg?branch=master (opens in a new tab) :target: https://github.com/cantools/cantools/actions/workflows/pythonpackage.yml (opens in a new tab) :alt: Github Actions workflow status
.. |coverage| image:: https://coveralls.io/repos/github/cantools/cantools/badge.svg?branch=master (opens in a new tab) :target: https://coveralls.io/github/cantoolscantools?branch=master (opens in a new tab) :alt: Test coverage reports on Coveralls.io
.. end-badges
About
CAN BUS tools in Python 3.
-
DBC,KCD, SYM, ARXML 3&4 and CDD file parsing. -
CAN message encoding and decoding.
-
Simple and extended signal multiplexing.
-
Diagnostic DID encoding and decoding.
-
candumpoutput decoder. -
Node
tester_. -
Csource code generator. -
CAN bus monitor.
-
Graphical plots of signals.
Project homepage: https://github.com/cantools/cantools (opens in a new tab)
Documentation: https://cantools.readthedocs.io (opens in a new tab)
Installation
.. code-block:: bash
python3 -m pip install cantools
Example usage
Scripting
The example starts by parsing a small DBC-file_ and printing its
messages and signals.
.. code-block:: python
import cantools from pprint import pprint db = cantools.database.load_file('tests/files/dbc/motohawk.dbc') db.messages [message('ExampleMessage', 0x1f0, False, 8, {None: 'Example message used as template in MotoHawk models.'})] example_message = db.get_message_by_name('ExampleMessage') pprint(example_message.signals) [signal('Enable', 7, 1, 'big_endian', False, None, 1, 0, None, None, '-', False, None, {0: 'Disabled', 1: 'Enabled'}, None, None), signal('AverageRadius', 6, 6, 'big_endian', False, None, 0.1, 0, 0, 5, 'm', False, None, None, None, None), signal('Temperature', 0, 12, 'big_endian', True, None, 0.01, 250, 229.52, 270.47, 'degK', False, None, None, None, None)]
The example continues encoding_ a message and sending it on a CAN
bus using the python-can_ package.
.. code-block:: python
import can can_bus = can.interface.Bus('vcan0', interface='socketcan') data = example_message.encode({'Temperature': 250.1, 'AverageRadius': 3.2, 'Enable': 1}) message = can.Message(arbitration_id=example_message.frame_id, is_extended_id=example_message.is_extended_frame, data=data) can_bus.send(message)
Alternatively, a message can be encoded using the encode_message()_
method on the database object.
The last part of the example receives and decodes_ a CAN message.
.. code-block:: python
message = can_bus.recv() db.decode_message(message.arbitration_id, message.data) {'Enable': 'Enabled', 'AverageRadius': 3.2, 'Temperature': 250.1}
See examples_ for additional examples.
Command line tool
The decode subcommand ^^^^^^^^^^^^^^^^^^^^^
Decode CAN frames captured with the Linux program candump.
.. code-block:: text
$ candump vcan0 | python3 -m cantools decode tests/files/dbc/motohawk.dbc vcan0 1F0 [8] 80 4A 0F 00 00 00 00 00 :: ExampleMessage( Enable: 'Enabled' -, AverageRadius: 0.0 m, Temperature: 255.92 degK ) vcan0 1F0 [8] 80 4A 0F 00 00 00 00 00 :: ExampleMessage( Enable: 'Enabled' -, AverageRadius: 0.0 m, Temperature: 255.92 degK ) vcan0 1F0 [8] 80 4A 0F 00 00 00 00 00 :: ExampleMessage( Enable: 'Enabled' -, AverageRadius: 0.0 m, Temperature: 255.92 degK )
Alternatively, the decoded message can be printed on a single line:
.. code-block:: text
$ candump vcan0 | python3 -m cantools decode --single-line tests/files/dbc/motohawk.dbc vcan0 1F0 [8] 80 4A 0F 00 00 00 00 00 :: ExampleMessage(Enable: 'Enabled' -, AverageRadius: 0.0 m, Temperature: 255.92 degK) vcan0 1F0 [8] 80 4A 0F 00 00 00 00 00 :: ExampleMessage(Enable: 'Enabled' -, AverageRadius: 0.0 m, Temperature: 255.92 degK) vcan0 1F0 [8] 80 4A 0F 00 00 00 00 00 :: ExampleMessage(Enable: 'Enabled' -, AverageRadius: 0.0 m, Temperature: 255.92 degK)
When piping decoded output into another program, Python may buffer its standard
output. Set PYTHONUNBUFFERED=1 on the decoder command to disable this buffering.
The receiving program may also need line buffering, as shown with grep here:
.. code-block:: bash
$ candump vcan0 | PYTHONUNBUFFERED=1 cantools decode --single-line
tests/files/dbc/motohawk.dbc | grep --line-buffered ExampleMessage
The plot subcommand ^^^^^^^^^^^^^^^^^^^
The plot subcommand is similar to the decode subcommand but messages are visualized using matplotlib_ instead of being printed to stdout.
.. code-block:: bash
$ candump -l vcan0
$ cat candump-2021-01-04_180521.log
(1609779922.655421) vcan0 00000343#B204B9049C049C04
(1609779922.655735) vcan0 0000024A#120527052E051905
(1609779923.657524) vcan0 00000343#C404C404CB04C404
(1609779923.658086) vcan0 0000024A#8B058B058B059205
(1609779924.659912) vcan0 00000343#5C04790479045504
(1609779924.660471) vcan0 0000024A#44064B0659064406
(1609779925.662277) vcan0 00000343#15040704F203F203
(1609779925.662837) vcan0 0000024A#8B069906A706A706
(1609779926.664191) vcan0 00000343#BC03B503A703BC03
(1609779926.664751) vcan0 0000024A#A006A706C406C406
$ cat candump-2021-01-04_180521.log | python3 -m cantools plot tests/files/dbc/abs.dbc
.. image:: https://github.com/cantools/cantools/raw/master/docs/plot-1.png (opens in a new tab)
If you don't want to show all signals you can select the desired signals with command line arguments.
A * can stand for any number of any character, a ? for exactly one arbitrary character.
Signals separated by a - are displayed in separate subplots.
Optionally a format can be specified after a signal, separated by a colon.
.. code-block:: bash
$ cat candump-2021-01-04_180521.log | python3 -m cantools plot tests/files/dbc/abs.dbc '*33.*fl:-<' '*33.*fr:->' - '*33.*rl:-<' '*33.*rr:->'
.. image:: https://github.com/cantools/cantools/raw/master/docs/plot-2-subplots.png (opens in a new tab)
Signals with a different range of values can be displayed in the same subplot on different vertical axes by separating them with a comma.
.. code-block:: bash
$ cat candump-2021-01-04_180521.log | cantools plot --auto-color tests/files/dbc/abs.dbc --
--ylabel 'Bremse 33' '_33.fl:-<' '_33.fr:>' '_33.rl:3' '_33.rr:4' ,
--ylabel 'Bremse 2' '_2.fl:-<' '_2.fr:>' '_2.rl:3' '_2.rr:4'
.. image:: https://github.com/cantools/cantools/raw/master/docs/plot-2-axes.png (opens in a new tab)
Matplotlib comes with different preinstalled styles that you can use:
.. code-block:: bash
$ cat candump-2021-01-04_180521.log | cantools plot tests/files/dbc/abs.dbc --style seaborn
.. image:: https://github.com/cantools/cantools/raw/master/docs/plot-seaborn.png (opens in a new tab)
You can try all available styles with
.. code-block:: bash
$ cantools plot --list-styles . | sed -n '/^- /s/^- //p' | while IFS= read -r style; do cat candump-2021-01-04_180521.log | cantools plot tests/files/dbc/abs.dbc --style "$style" --title "--style '$style'" done
For more information see
.. code-block:: bash
$ python3 -m cantools plot --help
Note that by default matplotlib is not installed with cantools. But it can be by specifying an extra at installation:
.. code-block:: bash
$ python3 -m pip install cantools[plot]
The dump subcommand ^^^^^^^^^^^^^^^^^^^
Dump given database in a human readable format:
.. code-block:: text
$ python3 -m cantools dump tests/files/dbc/motohawk.dbc ================================= Messages =================================
------------------------------------------------------------------------
Name: ExampleMessage
Id: 0x1f0
Length: 8 bytes
Cycle time: - ms
Senders: PCM1
Layout:
Bit
7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
0 |<-x|<---------------------x|<--|
+---+---+---+---+---+---+---+---+
| +-- AverageRadius
+-- Enable
+---+---+---+---+---+---+---+---+
1 |-------------------------------|
+---+---+---+---+---+---+---+---+
2 |----------x| | | | | |
B +---+---+---+---+---+---+---+---+
y +-- Temperature
t +---+---+---+---+---+---+---+---+
e 3 | | | | | | | | |
+---+---+---+---+---+---+---+---+
4 | | | | | | | | |
+---+---+---+---+---+---+---+---+
5 | | | | | | | | |
+---+---+---+---+---+---+---+---+
6 | | | | | | | | |
+---+---+---+---+---+---+---+---+
7 | | | | | | | | |
+---+---+---+---+---+---+---+---+
Signal tree:
-- {root}
+-- Enable
+-- AverageRadius
+-- Temperature
Signal choices:
Enable
0 Disabled
1 Enabled
------------------------------------------------------------------------
The list subcommand ^^^^^^^^^^^^^^^^^^^
Print all information of a given database in a human readable format. This is very similar to the "dump" subcommand, but the output is less pretty, slightly more comprehensive and easier to parse by shell scripts:
.. code-block:: bash
$ python3 -m cantools list -a tests/files/dbc/motohawk.dbc
ExampleMessage:
Comment[None]: Example message used as template in MotoHawk models.
Frame ID: 0x1f0 (496)
Size: 8 bytes
Is extended frame: False
Signals:
Enable:
Type: Integer
Start bit: 7
Length: 1 bits
Unit: -
Is signed: False
Named values:
0: Disabled
The generate C source subcommand ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Generate C source code from given database.
The generated code contains:
-
Message
structs_. -
Message
pack_ andunpack_ functions. -
Signal
encode_ anddecode_ functions. -
Frame id, length, type, cycle time and signal choices
defines_.
Known limitations:
- The maximum signal size is 64 bits, which in practice is never exceeded.
Below is an example of how to generate C source code from a
database. The database is tests/files/dbc/motohawk.dbc.
.. code-block:: text
$ python3 -m cantools generate_c_source tests/files/dbc/motohawk.dbc Successfully generated motohawk.h and motohawk.c.
See motohawk.h_ and motohawk.c_ for the contents of the generated
files.
In this example we use --use-float so floating point numbers in the generated
code are single precision (float) instead of double precision (double).
.. code-block:: text
$ python3 -m cantools generate_c_source --use-float tests/files/dbc/motohawk.dbc Successfully generated motohawk.h and motohawk.c.
In the next example we use --database-name to set a custom
namespace for all generated types, defines and functions. The output
file names are also changed by this option.
.. code-block:: text
$ python3 -m cantools generate_c_source --database-name my_database_name tests/files/dbc/motohawk.dbc Successfully generated my_database_name.h and my_database_name.c.
See my_database_name.h_ and my_database_name.c_ for the contents
of the generated files.
In the next example we use --no-floating-point-numbers to generate
code without floating point types, i.e. float and double.
.. code-block:: text
$ python3 -m cantools generate_c_source --no-floating-point-numbers tests/files/dbc/motohawk.dbc Successfully generated motohawk.h and motohawk.c.
See motohawk_no_floating_point_numbers.h_ and
motohawk_no_floating_point_numbers.c_ for the contents of the
generated files.
In the last example --node is used to generate
message pack functions only for messages sent by the specified node and unpack
functions only for messages with its signal receivers belonging to that node.
.. code-block:: text
$ cantools generate_c_source tests/files/dbc/motohawk.dbc --node PCM1 Successfully generated motohawk.h and motohawk.c.
See motohawk_sender_node.h_ and
motohawk_sender_node.c_ for the contents of the
generated files.
Other C code generators:
-
https://github.com/lonkamikaze/hsk-libs/blob/master/scripts/dbc2c.awk (opens in a new tab)
-
https://sourceforge.net/projects/comframe/ (opens in a new tab)
The monitor subcommand ^^^^^^^^^^^^^^^^^^^^^^
Monitor CAN bus traffic in a text based user interface.
.. code-block:: text
$ python3 -m cantools monitor tests/files/dbc/motohawk.dbc
.. image:: https://github.com/cantools/cantools/raw/master/docs/monitor.png (opens in a new tab)
The menu at the bottom of the monitor shows the available commands.
-
Quit: Quit the monitor. Ctrl-C can be used as well.
-
Filter: Only display messages or signals matching given regular expression. Press to return to the menu from the filter input line.
-
Play/Pause: Toggle between playing and paused (or running and freezed).
-
Reset: Reset the monitor to its initial state.
Contributing
#. Fork the repository.
#. Install prerequisites. You can skip this if you use tox_.
.. code-block:: text
python3 -m pip install -e .[dev]
#. Implement the new feature or bug fix.
#. Implement test case(s) to ensure that future changes do not break legacy.
#. Run the linters
.. code-block:: text
ruff check src
mypy src
or
.. code-block:: text
tox -e ruff
tox -e mypy
#. Run the tests.
.. code-block:: text
tox -e py
#. Check test coverage.
.. code-block:: text
tox -e cov
firefox htmlcov/index.html
#. Create a pull request.
.. _small DBC-file: https://github.com/cantools/cantools/blob/master/tests/files/dbc/motohawk.dbc (opens in a new tab)
.. _motohawk.dbc: https://github.com/cantools/cantools/blob/master/tests/files/dbc/motohawk.dbc (opens in a new tab)
.. _python-can: https://python-can.readthedocs.io/en/master/ (opens in a new tab)
.. _DBC: http://www.socialledge.com/sjsu/index.php?title=DBC_Format (opens in a new tab)
.. _KCD: https://github.com/julietkilo/kcd (opens in a new tab)
.. _tester: http://cantools.readthedocs.io/en/latest/#cantools.tester.Tester (opens in a new tab)
.. _encoding: http://cantools.readthedocs.io/en/latest/#cantools.database.can.Message.encode (opens in a new tab)
.. _encode_message(): http://cantools.readthedocs.io/en/latest/#cantools.database.can.Database.encode_message (opens in a new tab)
.. _decodes: http://cantools.readthedocs.io/en/latest/#cantools.database.can.Database.decode_message (opens in a new tab)
.. _examples: https://github.com/cantools/cantools/blob/master/examples (opens in a new tab)
.. _structs: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk.h#L58 (opens in a new tab)
.. _unpack: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk.h#L102 (opens in a new tab)
.. _encode: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk.h#L116 (opens in a new tab)
.. _decode: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk.h#L125 (opens in a new tab)
.. _defines: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk.h#L42 (opens in a new tab)
.. _motohawk.h: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk.h (opens in a new tab)
.. _motohawk.c: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk.c (opens in a new tab)
.. _my_database_name.h: https://github.com/cantools/cantools/blob/master/tests/files/c_source/my_database_name.h (opens in a new tab)
.. _my_database_name.c: https://github.com/cantools/cantools/blob/master/tests/files/c_source/my_database_name.c (opens in a new tab)
.. _motohawk_no_floating_point_numbers.h: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk_no_floating_point_numbers.h (opens in a new tab)
.. _motohawk_no_floating_point_numbers.c: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk_no_floating_point_numbers.c (opens in a new tab)
.. _motohawk_sender_node.h: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk_sender_node.h (opens in a new tab)
.. _motohawk_sender_node.c: https://github.com/cantools/cantools/blob/master/tests/files/c_source/motohawk_sender_node.c (opens in a new tab)
.. _matplotlib: https://matplotlib.org/ (opens in a new tab)
Robotheca lists software as a reference: nothing is hosted or run here, and every fact links to where it was read.