{"id":2731,"library":"python-can","title":"Python CAN Bus Interface","description":"The python-can library provides Controller Area Network (CAN) support for Python developers, offering common abstractions to various hardware devices and a suite of utilities for sending and receiving messages on a CAN bus. It supports CPython and PyPy, running on Mac, Linux, and Windows. The current version is 4.6.1, with frequent updates to the 4.x series.","status":"active","version":"4.6.1","language":"en","source_language":"en","source_url":"https://github.com/hardbyte/python-can","tags":["CAN","automotive","hardware interface","bus communication","socketcan","CAN FD"],"install":[{"cmd":"pip install python-can","lang":"bash","label":"Basic installation"},{"cmd":"pip install 'python-can[vector]' # for Vector hardware\npip install 'python-can[kvaser]' # for Kvaser hardware","lang":"bash","label":"Installation with hardware-specific backends"}],"dependencies":[{"reason":"Required for SocketCAN interface on Linux. Needs system-level configuration using `ip` commands.","package":"Linux kernel modules (e.g., vcan, can)","optional":true},{"reason":"Necessary for interacting with specific CAN hardware on Windows or other OS.","package":"Hardware vendor drivers (e.g., Kvaser CANLib SDK, Vector XL Driver Library)","optional":true}],"imports":[{"symbol":"can","correct":"import can"},{"symbol":"can.Bus","correct":"from can import Bus"},{"symbol":"can.Message","correct":"from can import Message"}],"quickstart":{"code":"import can\nimport os\n\n# For a runnable example without physical hardware, you can set up a virtual CAN interface:\n# On Linux, run in terminal: `sudo modprobe vcan && sudo ip link add dev vcan0 type vcan && sudo ip link set vcan0 up`\n# Then, use channel='vcan0' below.\n\n# Configure bus interface and channel (can be set via environment variables or config file too)\n# Using 'socketcan' with 'vcan0' is a common virtual setup on Linux.\n# Replace 'socketcan' and 'vcan0' with your actual interface and channel if using real hardware.\nBUS_INTERFACE = os.environ.get('CAN_INTERFACE', 'socketcan')\nCAN_CHANNEL = os.environ.get('CAN_CHANNEL', 'vcan0')\n\ntry:\n    # Using 'with' statement ensures the bus is properly shut down\n    with can.Bus(interface=BUS_INTERFACE, channel=CAN_CHANNEL, receive_own_messages=True) as bus:\n        # Create a CAN message\n        message = can.Message(\n            arbitration_id=0x123,  # Standard or extended ID\n            is_extended_id=False, # Set to True for extended IDs\n            data=[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88], # 0-8 bytes for CAN 2.0, up to 64 for CAN FD\n            is_fd=False,          # Set to True for CAN FD messages\n            bitrate_switch=False, # Relevant for CAN FD\n            error_state_indicator=False # Relevant for CAN FD\n        )\n\n        print(f\"Attempting to send message: {message}\")\n        bus.send(message)\n        print(f\"Message sent successfully on {bus.channel} using {bus.interface} interface.\")\n\n        # Optional: Receive a message\n        # received_message = bus.recv(10.0) # Wait up to 10 seconds for a message\n        # if received_message:\n        #    print(f\"Received message: {received_message}\")\n        # else:\n        #    print(\"No message received.\")\n\nexcept can.CanError as e:\n    print(f\"Error sending message: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to initialize a CAN bus and send a single message. It uses a `with` statement for proper resource management. For testing without physical hardware, a virtual CAN interface (like `vcan0` on Linux) can be used."},"warnings":[{"fix":"Users migrating from versions prior to 4.0.0 should update their code to use the unified `socketcan` interface.","message":"The `socketcan_ctypes` and `socketcan_native` implementations were removed in version 4.0.0 after a deprecation period.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Always use the `with` statement when creating a `can.Bus` instance to ensure the bus is automatically shut down upon exiting the block, e.g., `with can.Bus(...) as bus:`.","message":"Failing to properly shut down the CAN bus can lead to resource leaks or unexpected behavior, especially in long-running applications.","severity":"gotcha","affected_versions":"<all>"},{"fix":"Before using `python-can` with hardware, ensure all necessary vendor drivers are installed and system-level configurations (e.g., `sudo ip link set can0 up type can bitrate 500000` for SocketCAN) are applied. Refer to the hardware interfaces section of the official documentation.","message":"Hardware-specific drivers and system-level configuration are often required for `python-can` to function with physical CAN interfaces.","severity":"gotcha","affected_versions":"<all>"},{"fix":"Standardize on a single configuration method where possible. If using multiple, understand the precedence rules (typically in-code configuration overrides environment variables and config files). Always explicitly set critical parameters like interface and channel.","message":"Configuration of `python-can` can be done via code, environment variables, or a configuration file. Inconsistent or conflicting settings across these methods can lead to unexpected bus behavior.","severity":"gotcha","affected_versions":"<all>"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}