Подключение к can python

cantools 39.0.0

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

The example continues encoding a message and sending it on a CAN bus using the python-can package.
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.

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 vcan0  vcan0 0000024A#120527052E051905 vcan0  vcan0 0000024A#8B058B058B059205 vcan0  vcan0 0000024A#44064B0659064406 vcan0  vcan0 0000024A#8B069906A706A706 vcan0  vcan0 0000024A#A006A706C406C406 cat candump-2021-01-04_180521.log  python3 -m cantools plot tests/files/dbc/abs.dbc

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   -  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      ,  --ylabel     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

You can try all available styles with

$ cantools plot --list-styles .  sed -n      -r style  cat candump-2021-01-04_180521.log  cantools plot tests/files/dbc/abs.dbc --style  --title  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

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: -- +-- 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 Comment Example message used as template  MotoHawk models. Frame ID: 0x1f0  Size:  bytes Is extended frame: False Signals: Enable: Type: Integer Start bit:  Length:  bits Unit: - Is signed: False Named values:  Disabled

The generate C source subcommand

Generate C source code from given database.

The generated code contains:

  • Message structs.
  • Message pack and unpack functions.
  • Signal encode and decode functions.
  • Frame id, length, type, cycle time and signal choices defines.

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.

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.

The monitor subcommand

Monitor CAN bus traffic in a text based user interface.

$ python3 -m cantools monitor tests/files/dbc/motohawk.dbc

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

python3 -m pip install -e .[dev]
ruff check cantools mypy cantools

Источник

python-can¶

The python-can library provides Controller Area Network support for Python, providing common abstractions to different hardware devices, and a suite of utilities for sending and receiving messages on a CAN bus.

python-can runs any where Python runs; from high powered computers with commercial CAN to usb devices right down to low powered devices running linux such as a BeagleBone or RaspberryPi.

More concretely, some example uses of the library:

  • Passively logging what occurs on a CAN bus. For example monitoring a commercial vehicle using its OBD-II port.
  • Testing of hardware that interacts via CAN. Modules found in modern cars, motocycles, boats, and even wheelchairs have had components tested from Python using this library.
  • Prototyping new hardware modules or software algorithms in-the-loop. Easily interact with an existing bus.
  • Creating virtual modules to prototype CAN bus communication.

Brief example of the library in action: connecting to a CAN bus, creating and sending a message:

 1#!/usr/bin/env python 2 3""" 4This example shows how sending a single message works. 5""" 6 7import can 8 9 10def send_one(): 11 """Sends a single message.""" 12 13 # this uses the default configuration (for example from the config file) 14 # see https://python-can.readthedocs.io/en/stable/configuration.html 15 with can.interface.Bus() as bus: 16 17 # Using specific buses works similar: 18 # bus = can.interface.Bus(bustype='socketcan', channel='vcan0', bitrate=250000) 19 # bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=250000) 20 # bus = can.interface.Bus(bustype='ixxat', channel=0, bitrate=250000) 21 # bus = can.interface.Bus(bustype='vector', app_name='CANalyzer', channel=0, bitrate=250000) 22 # . 23 24 msg = can.Message( 25 arbitration_id=0xC0FFEE, data=[0, 25, 0, 1, 3, 1, 4, 1], is_extended_id=True 26 ) 27 28 try: 29 bus.send(msg) 30 print(f"Message sent on bus.channel_info>") 31 except can.CanError: 32 print("Message NOT sent") 33 34 35if __name__ == "__main__": 36 send_one() 
Оцените статью