Skip to main content

Github Actions workflow status Test coverage reports on Coveralls.io

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.

  • candump output decoder.

  • Node tester.

  • C source code generator.

  • CAN bus monitor.

  • Graphical plots of signals.

Project homepage: https://github.com/cantools/cantools

Documentation: https://cantools.readthedocs.io

Installation

python3 -m pip install cantools

Example usage

Scripting

The example starts by parsing a small DBC-file and printing its messages and signals.

>>> import cantools
>>> from pprint import pprint
>>> db = cantools.database.load_file('tests/files/dbc/motohawk.dbc')
>>> db.messages
[message('ExampleMessage', 0x1f0, False, 8, '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, 1.0, 0, 0.0, 0.0, '-', False, None, {0: 'Disabled', 1: 'Enabled'}, None),
 signal('AverageRadius', 6, 6, 'big_endian', False, 0.1, 0, 0.0, 5.0, 'm', False, None, None, ''),
 signal('Temperature', 0, 12, 'big_endian', True, 0.01, 250, 229.53, 270.47, 'degK', False, None, None, None)]

The example continues encoding a message and sending it on a CAN bus using the python-can package.

>>> import can
>>> can_bus = can.interface.Bus('vcan0', bustype='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.

>>> message = can_bus.recv()
>>> db.decode_message(message.arbitration_id, message.data)
{'AverageRadius': 3.2, 'Enable': 'Enabled', 'Temperature': 250.09}

See examples for additional examples.

Command line tool

The decode subcommand

Decode CAN frames captured with the Linux program candump.

$ 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:

$ 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)

The plot subcommand

The plot subcommand is similar to the decode subcommand but messages are visualized using matplotlib instead of being printed to stdout.

$ 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
https://github.com/cantools/cantools/raw/master/docs/plot-1.png

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.

$ cat candump-2021-01-04_180521.log | python3 -m cantools plot tests/files/dbc/abs.dbc '*33.*fl:-<' '*33.*fr:->' - '*33.*rl:-<' '*33.*rr:->'
https://github.com/cantools/cantools/raw/master/docs/plot-2-subplots.png

Signals with a different range of values can be displayed in the same subplot on different vertical axes by separating them with a comma.

$ 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'
https://github.com/cantools/cantools/raw/master/docs/plot-2-axes.png

Matplotlib comes with different preinstalled styles that you can use:

$ cat candump-2021-01-04_180521.log | cantools plot tests/files/dbc/abs.dbc --style seaborn
https://github.com/cantools/cantools/raw/master/docs/plot-seaborn.png

You can try all available styles with

$ 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

$ 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:

$ python3 -m pip install cantools[plot]

The dump subcommand

Dump given database in a human readable format:

$ 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:

$ 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:

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.

$ 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).

$ 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.

$ 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.

$ 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.

$ 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:

The monitor subcommand

Monitor CAN bus traffic in a text based user interface.

$ python3 -m cantools monitor tests/files/dbc/motohawk.dbc
https://github.com/cantools/cantools/raw/master/docs/monitor.png

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 <Enter> 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

  1. Fork the repository.

  2. Install prerequisites. You can skip this if you use tox.

    python3 -m pip install -e .[dev]
  3. Implement the new feature or bug fix.

  4. Implement test case(s) to ensure that future changes do not break legacy.

  5. Run the linters

    ruff check src
    mypy src

    or

    tox -e ruff
    tox -e mypy
  6. Run the tests.

    tox -e py
  7. Check test coverage.

    tox -e cov
    firefox htmlcov/index.html
  8. Create a pull request.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cantools-43.0.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cantools-43.0.0-py3-none-any.whl (165.0 kB view details)

Uploaded Python 3

File details

Details for the file cantools-43.0.0.tar.gz.

File metadata

  • Download URL: cantools-43.0.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for cantools-43.0.0.tar.gz
Algorithm Hash digest
SHA256 4ce239d0d0f69c574a196e1c5d8f9da0802031abb8f861ed6175a0b02d26d893
MD5 b69b0699554c168de1f5b238175a167d
BLAKE2b-256 2ef5455230b00806983bda85c97be8f33bd9e95856ce2b52d952298eb9e8bdfb

See more details on using hashes here.

Provenance

The following attestation bundles were made for cantools-43.0.0.tar.gz:

Publisher: pythonpackage.yml on cantools/cantools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cantools-43.0.0-py3-none-any.whl.

File metadata

  • Download URL: cantools-43.0.0-py3-none-any.whl
  • Upload date:
  • Size: 165.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for cantools-43.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de1e757a4167da926c7fff8763c6886ddeabb0b7c0d1b6b89b2b4fd301571c56
MD5 0396fcfb96351b9b1c1aef44ae2cd0c2
BLAKE2b-256 1f2681d262e10080c0bcd7804e9ee2db02e1f893fdd8816fb05d6a5c2e22f8a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for cantools-43.0.0-py3-none-any.whl:

Publisher: pythonpackage.yml on cantools/cantools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

43.0.1

2 files

This release

43.0.0 This release

2 files

42.0.3

2 files

42.0.2

2 files

42.0.1

2 files

41.4.3

2 files

41.4.2

2 files

41.4.1

2 files

41.4.0

2 files

41.3.1

2 files

41.3.0

2 files

41.2.1

2 files

41.2.0

2 files

41.1.1

2 files

41.0.2

2 files

41.0.1

2 files

41.0.0

2 files

40.7.1

2 files

40.7.0

2 files

40.6.0

2 files

40.5.0

2 files

40.4.0

2 files

40.3.0

2 files

40.2.4

2 files

40.2.3

2 files

40.2.2

2 files

40.2.1

2 files

40.2.0

2 files

40.1.1

2 files

40.1.0

2 files

40.0.0

2 files

39.4.13

2 files

39.4.12

2 files

39.4.11

2 files

39.4.9

2 files

39.4.8

2 files

39.4.7

2 files

39.4.6

2 files

39.4.5

2 files

39.4.4

2 files

39.4.3

2 files

39.4.2

2 files

39.4.1

2 files

39.4.0

2 files

39.3.0

2 files

39.2.0

2 files

39.0.0

2 files

39.0.0rc1

2 files

38.0.2

2 files

38.0.1

2 files

38.0.0

2 files

37.3.0

2 files

37.2.0

2 files

37.1.2

2 files

37.1.0

2 files

37.0.7

2 files

37.0.6

2 files

37.0.5

2 files

37.0.4

2 files

37.0.3

2 files

37.0.2

2 files

37.0.1

2 files

37.0.0

2 files

36.7.0

2 files

36.6.0

2 files

36.5.0

2 files

36.4.0

2 files

36.2.0

2 files

36.1.0

2 files

36.0.0

2 files

35.5.0

2 files

35.4.0

2 files

35.3.0

2 files

35.2.0

2 files

35.1.0

2 files

35.0.0

2 files

34.3.0

2 files

34.2.0

2 files

34.1.0

2 files

34.0.0

2 files

33.2.0

2 files

33.1.1

2 files

33.1.0

2 files

33.0.0

2 files

32.21.0

2 files

32.20.3

2 files

32.20.2

2 files

32.20.1

2 files

32.20.0

2 files

32.19.2

2 files

32.19.1

2 files

32.19.0

2 files

32.18.0

2 files

32.17.0

2 files

32.16.0

2 files

32.15.1

2 files

32.14.0

2 files

32.13.1

2 files

32.13.0

2 files

32.12.0

2 files

32.11.2

2 files

32.11.1

2 files

32.11.0

2 files

32.10.1

2 files

32.10.0

2 files

32.9.0

2 files

32.8.0

2 files

32.7.0

2 files

32.6.3

2 files

32.6.2

2 files

32.6.1

2 files

32.6.0

2 files

32.5.3

2 files

32.5.2

2 files

32.5.1

2 files

32.5.0

2 files

32.4.0

2 files

32.3.0

2 files

32.2.0

2 files

32.1.0

2 files

32.0.0

2 files

31.0.0

2 files

30.0.0

2 files

29.7.0

2 files

29.6.0

2 files

29.5.0

2 files

29.4.0

2 files

29.3.0

2 files

29.2.1

2 files

29.2.0

2 files

29.1.0

2 files

29.0.0

2 files

28.19.0

2 files

28.18.0

2 files

28.17.0

2 files

28.16.0

2 files

28.15.0

2 files

28.14.0

2 files

28.13.0

2 files

28.12.0

2 files

28.11.0

2 files

28.10.0

2 files

28.9.0

2 files

28.8.1

2 files

28.8.0

2 files

28.7.2

2 files

28.7.1

2 files

28.7.0

2 files

28.6.0

2 files

28.5.0

2 files

28.4.0

2 files

28.3.0

2 files

28.2.0

2 files

28.1.0

2 files

28.0.1

2 files

28.0.0

2 files

27.1.0

2 files

27.0.0

2 files

26.0.0

2 files

25.3.0

2 files

25.2.0

2 files

25.1.0

2 files

25.0.0

2 files

24.1.2

2 files

24.1.1

2 files

24.1.0

2 files

24.0.0

2 files

23.1.0

2 files

23.0.0

2 files

22.1.0

2 files

22.0.2

2 files

22.0.1

2 files

22.0.0

2 files

21.0.3

2 files

21.0.2

2 files

21.0.1

2 files

21.0.0

2 files

20.9.0

2 files

20.8.0

2 files

20.7.0

2 files

20.6.0

2 files

20.5.0

2 files

20.4.0

2 files

20.3.2

2 files

20.3.1

2 files

20.3.0

2 files

20.2.0

2 files

20.1.0

2 files

20.0.1

2 files

20.0.0

2 files

19.1.0

2 files

19.0.0

2 files

18.0.1

2 files

18.0.0

2 files

17.0.0

2 files

16.3.0

2 files

16.2.0

2 files

16.1.1

2 files

16.1.0

2 files

16.0.0

2 files

15.0.0

2 files

14.4.0

2 files

14.3.1

2 files

14.3.0

2 files

14.2.0

2 files

14.1.0

2 files

14.0.0

2 files

13.0.0

2 files

12.0.0

2 files

11.0.0

2 files

10.1.0

2 files

10.0.0

2 files

9.1.1

2 files

9.1.0

2 files

9.0.0

2 files

8.0.0

2 files

7.0.0

2 files

6.1.0

2 files

6.0.0

2 files

5.2.0

2 files

5.1.0

2 files

5.0.0

2 files

4.1.1

2 files

4.1.0

2 files

4.0.1

2 files

4.0.0

2 files

3.1.1

2 files

3.1.0

2 files

3.0.0

2 files

2.4.0

2 files

2.3.0

2 files

2.2.0

2 files

2.1.0

2 files

2.0.0

2 files

1.0.0

2 files

0.2.2

2 files

0.2.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page