Python Forum
how to startup canbus interface on pi
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to startup canbus interface on pi
#1
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 ,
Reply
#2
searching PyPi for CAN + Buss brings up several packages,
this one, although rather old, looks promising: https://pypi.org/project/can4python/
Reply
#3
(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/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Autostart to run a GUI program at startup. Rpi Edward_ 1 622 Oct-28-2023, 03:19 PM
Last Post: SpongeB0B
  pexpect startup help korenron 2 3,484 Apr-27-2021, 07:23 AM
Last Post: korenron
  Error when running script on startup in Linux NoahTheNerd 0 1,958 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd
  reading canbus data as hex korenron 9 6,284 Dec-30-2020, 01:52 PM
Last Post: korenron
  Run script on startup in Debian with systemd? MrGlasspoole 5 3,662 Jul-12-2020, 11:48 AM
Last Post: MrGlasspoole
  run python script on startup? korenron 1 3,138 May-06-2019, 09:18 PM
Last Post: snippsat
  os.system("netsh interface set interface 'Wi-Fi' enabled") aniyanetworks 12 10,147 Jan-18-2019, 04:07 AM
Last Post: aniyanetworks
  Python script runs on startup but does not register keystrokes. mericanpi 3 3,428 Sep-07-2018, 02:58 PM
Last Post: mericanpi
  Complete Beginner Startup - Simply run github code for hpd20 Image_Engine 8 4,703 Apr-23-2018, 11:15 PM
Last Post: snippsat
  How do I make RPi run script on startup? marciokoko 6 5,591 Feb-07-2017, 03:42 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020