Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

CAN BUS tools.

Project description

buildstatus coverage

About

CAN BUS tools.

  • DBC, KCD and SYM file parsing.
  • CAN message encoding and decoding.
  • Simple and extended signal multiplexing.
  • candump output decoder.
  • Node tester.

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

Documentation: http://cantools.readthedocs.org/en/latest

Installation

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.db.load_file('tests/files/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.rc['interface'] = 'socketcan_ctypes'
>>> can.rc['channel'] = 'vcan0'
>>> can_bus = can.interface.Bus()
>>> data = example_message.encode({'Temperature': 250.1, 'AverageRadius': 3.2, 'Enable': 1})
>>> message = can.Message(arbitration_id=example_message.frame_id, 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

Decode CAN frames captured with the Linux program candump.

$ candump vcan0 | cantools decode tests/files/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 | cantools decode --single-line tests/files/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)

Contributing

  1. Fork the repository.

  2. Install prerequisites.

    pip install -r requirements.txt
    
  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 tests.

    make test
    
  6. Create a pull request.

Project details


Release history Release notifications

This version
History Node

22.0.0

History Node

21.0.3

History Node

21.0.2

History Node

21.0.1

History Node

21.0.0

History Node

20.9.0

History Node

20.8.0

History Node

20.7.0

History Node

20.6.0

History Node

20.5.0

History Node

20.4.0

History Node

20.3.2

History Node

20.3.1

History Node

20.3.0

History Node

20.2.0

History Node

20.1.0

History Node

20.0.1

History Node

20.0.0

History Node

19.1.0

History Node

19.0.0

History Node

18.0.1

History Node

18.0.0

History Node

17.0.0

History Node

16.3.0

History Node

16.2.0

History Node

16.1.1

History Node

16.1.0

History Node

16.0.0

History Node

15.0.0

History Node

14.4.0

History Node

14.3.1

History Node

14.3.0

History Node

14.2.0

History Node

14.1.0

History Node

14.0.0

History Node

13.0.0

History Node

12.0.0

History Node

11.0.0

History Node

10.1.0

History Node

10.0.0

History Node

9.1.1

History Node

9.1.0

History Node

9.0.0

History Node

8.0.0

History Node

7.0.0

History Node

6.1.0

History Node

6.0.0

History Node

5.2.0

History Node

5.1.0

History Node

5.0.0

History Node

4.1.1

History Node

4.1.0

History Node

4.0.1

History Node

4.0.0

History Node

3.1.1

History Node

3.1.0

History Node

3.0.0

History Node

2.4.0

History Node

2.3.0

History Node

2.2.0

History Node

2.1.0

History Node

2.0.0

History Node

1.0.0

History Node

0.2.2

History Node

0.2.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
cantools-22.0.0-py2.py3-none-any.whl (34.9 kB) Copy SHA256 hash SHA256 Wheel py2.py3 Jun 18, 2018
cantools-22.0.0.tar.gz (51.9 kB) Copy SHA256 hash SHA256 Source None Jun 18, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page