Python Forum

Full Version: how to startup canbus interface on pi
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello ,
up until now on startup I use
os.system('ip link set can0 up type can bitrate 250000 restart-ms 10000') 
in order to make the canbus interface UP

is there any way to do it this using
 python-can
?

Thanks ,
searching PyPi for CAN + Buss brings up several packages,
this one, although rather old, looks promising: https://pypi.org/project/can4python/
(Oct-24-2021, 10:20 AM)Larz60+ Wrote: [ -> ]although rather old, looks promising: https://pypi.org/project/can4python/

But he still has to add/configure and bring up the can interface.
The os.system call is the worst you can use.

os.system('ip link set can0 up type can bitrate 250000 restart-ms 10000') 
Make a nice function from it, then you don't add another dependency from pypi and the function could be as minimal as required.

from __future__ import annotations

import subprocess


def configure_can(
    interface: str,
    bitrate: int = 250_000,
    restart_seconds: float | int = 10,
    sudo: bool = False,
) -> bool:
    """
    Configure existing can interface

    Return True if success, otherwise False
    If sudo is True, ip will run with sudo
    """
    restart_ms = int(restart_seconds * 1_000)
    cmd = [
        "ip",
        "link",
        "set",
        interface,
        "up",
        "type",
        "can",
        "bitrate",
        str(bitrate),
        "restart-ms",
        str(restart_ms),
    ]

    if sudo:
        cmd.insert(0, "sudo")

    # to see which command will run
    # print(" ".join(cmd))

    # ip or in general all commands return 0 if the operation was successful
    # in all other cases (errors) the returncode is not equal to 0

    return subprocess.run(cmd, stderr=subprocess.DEVNULL).returncode == 0


print(configure_can("can0"))
# fails on my system (no can)
If you're already using Python 3.10.0, you can remove the __future__ import.
But it's required, if an older Python-Version is used, because of the type annotations.

The same function without annotations:
import subprocess


def configure_can(interface, bitrate=250_000, restart_seconds=10, sudo=False):
    """
    Configure existing can interface

    Return True if success, otherwise False
    If sudo is True, ip will run with sudo
    """
    restart_ms = int(restart_seconds * 1_000)
    cmd = [
        "ip",
        "link",
        "set",
        interface,
        "up",
        "type",
        "can",
        "bitrate",
        str(bitrate),
        "restart-ms",
        str(restart_ms),
    ]

    if sudo:
        cmd.insert(0, "sudo")

    # to see which command will run
    # print(" ".join(cmd))

    # ip or in general all commands return 0 if the operation was successful
    # in all other cases (errors) the returncode is not equal to 0

    return subprocess.run(cmd, stderr=subprocess.DEVNULL).returncode == 0


print(configure_can("can0"))
# fails on my system (no can)
You can also bring up the interface during boot: https://www.pragmaticlinux.com/2021/07/a...e-on-boot/