The Controller Area Network is a bus standard designed to allow microcontrollers and devices to communicate with each other. It has priority based bus arbitration and reliable deterministic communication. It is used in cars, trucks, boats, wheelchairs and more.
The can
package provides controller area network support for
Python developers; providing common abstractions to
different hardware devices, and a suite of utilities for sending and receiving
messages on a can bus.
The library currently supports CPython as well as PyPy and runs on Mac, Linux and Windows.
Library Version | Python |
2.x | 2.6+, 3.4+ |
3.x | 2.7+, 3.5+ |
4.x | 3.7+ |
- common abstractions for CAN communication
- support for many different backends (see the docs)
- receiving, sending, and periodically sending messages
- normal and extended arbitration IDs
- CAN FD support
- many different loggers and readers supporting playback: ASC (CANalyzer format), BLF (Binary Logging Format by Vector), TRC, CSV, SQLite, and Canutils log
- efficient in-kernel or in-hardware filtering of messages on supported interfaces
- bus configuration reading from a file or from environment variables
- command line tools for working with CAN buses (see the docs)
- more
pip install python-can
# import the library
import can
# create a bus instance using 'with' statement,
# this will cause bus.shutdown() to be called on the block exit;
# many other interfaces are supported as well (see documentation)
with can.Bus(interface='socketcan',
channel='vcan0',
receive_own_messages=True) as bus:
# send a message
message = can.Message(arbitration_id=123, is_extended_id=True,
data=[0x11, 0x22, 0x33])
bus.send(message, timeout=0.2)
# iterate over received messages
for msg in bus:
print(f"{msg.arbitration_id:X}: {msg.data}")
# or use an asynchronous notifier
notifier = can.Notifier(bus, [can.Logger("recorded.log"), can.Printer()])
You can find more information in the documentation, online at python-can.readthedocs.org.
If you run into bugs, you can file them in our issue tracker on GitHub.
Stackoverflow has several
questions and answers tagged with python+can
.
Wherever we interact, we strive to follow the Python Community Code of Conduct.
See doc/development.rst for getting started.