Skip to main content

CAN BUS tools.

Project description

buildstatus coverage

About

CAN BUS tools.

  • DBC file mangling.

  • candump output decoder.

Installation

pip install cantools

Example usage

Scripting

An example parsing and using a small DBC-file:

>>> import cantools
>>> from pprint import pprint
>>> db = cantools.db.File()
>>> db.add_dbc_file('tests/files/motohawk.dbc')
>>> db
version('1.0')

node('PCM1', None)
node('FOO', None)

message('ExampleMessage', 0x1f0, 8, 'Example message used as template in MotoHawk models.')
  signal('Temperature', 7, 12, 'big_endian', True, 0.01, 250, 229.53, 270.47, 'degK', None, None)
  signal('AverageRadius', 1, 6, 'big_endian', False, 0.1, 0, 0.0, 5.0, 'm', None, '')
  signal('Enable', 0, 1, 'big_endian', False, 1.0, 0, 0.0, 0.0, '-', None, None)

>>> db.messages
[message('ExampleMessage', 0x1f0, 8, 'Example message used as template in MotoHawk models.')]
>>> example_message = db.messages[0]
>>> example_message.comment
'Example message used as template in MotoHawk models.'
>>> pprint(example_message.signals)
[signal('Temperature', 7, 12, 'big_endian', True, 0.01, 250, 229.53, 270.47, 'degK', None, None),
 signal('AverageRadius', 1, 6, 'big_endian', False, 0.1, 0, 0.0, 5.0, 'm', None, ''),
 signal('Enable', 0, 1, 'big_endian', False, 1.0, 0, 0.0, 0.0, '-', None, None)]
>>> db.nodes
[node('PCM1', None), node('FOO', None)]
>>> db.version
'1.0'
>>>

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

See the test suite for additional examples: https://github.com/eerimoq/cantools/blob/master/tests/test_cantools.py

Command line tool

Decode CAN frames captured with the Linux program candump.

$ candump vcan0 | cantools decode motohawk.dbc
  vcan0  000001F0   [7]  01 FA 0F 00 00 00 00 :: ExampleMessage(Temperature: 249.88 degK, AverageRadius: 0.0 m, Enable: 1.0 -)
  vcan0  000001F0   [7]  01 FA 0F 00 00 00 00 :: ExampleMessage(Temperature: 249.88 degK, AverageRadius: 0.0 m, Enable: 1.0 -)
  vcan0  000001F0   [7]  01 FA 0F 00 00 00 00 :: ExampleMessage(Temperature: 249.88 degK, AverageRadius: 0.0 m, Enable: 1.0 -)

Contributing

  1. Fork the repository.

  2. Implement the new feature or bug fix.

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

  4. Run the test suite for Python 2 and Python 3. Make sure all tests pass.

    python setup.py test
    python3 setup.py test
  5. Create a pull request.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

cantools-4.1.1.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

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

cantools-4.1.1-py2.py3-none-any.whl (13.6 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: cantools-4.1.1.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cantools-4.1.1.tar.gz
Algorithm Hash digest
SHA256 6d577371c44730e638d95b23b6bcb599f52bb555fff18cd2af0801f903d5a09d
MD5 803aa850f745ad745eb4024d874041dc
BLAKE2b-256 2b01f676ac1550dd6e2d8f8e84082b145408a2b2e84f3b00c283d38b3abf4169

See more details on using hashes here.

File details

Details for the file cantools-4.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cantools-4.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 92b4ea3455470851679e9fe70fd277396abb52607c5a05e0d65f19402ce910aa
MD5 3ebb1d19eda2bf674c949677541020ad
BLAKE2b-256 c6a2dac46f211cdb026c159a864a7b2ac645ddb893df1b9618a4c28827506357

See more details on using hashes here.

Supported by

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