Skip to content

FIBO-Engineer/ros2_keya_driver

Repository files navigation

Automate CAN Interface Setup using systemd

This guide explains how to automatically set up the CAN interface (can0) using a systemd service file. Follow these steps to enable the CAN interface with a specified bitrate when the device is connected.

Step 1: Create a systemd Service File

  1. Create a new file named /etc/systemd/system/setup-can0.service:

    sudo nano /etc/systemd/system/setup-can0.service
  2. Add the following content to the service file:

    [Unit]
    Description=Setup CAN0 interface
    Wants=network-pre.target
    Before=network-pre.target
    BindsTo=sys-subsystem-net-devices-can0.device
    After=sys-subsystem-net-devices-can0.device
    
    [Service]
    Type=oneshot
    ExecStart=/sbin/ip link set can0 up type can bitrate 500000
    ExecStop=/sbin/ip link set can0 down
    RemainAfterExit=yes
    
    [Install]
    WantedBy=multi-user.target
    • The [Unit] section specifies dependencies and the order of activation.
    • The [Service] section defines the commands to set up and tear down the CAN interface.
    • The [Install] section determines when the service should be activated.

Step 2: Reload systemd and Enable the Service

Run the following commands to reload systemd and enable the service:

sudo systemctl daemon-reload
sudo systemctl enable setup-can0.service

Step 3: Start the Service (Optional)

To start the service immediately without rebooting:

sudo systemctl start setup-can0.service

Verification

After enabling the service, disconnect and reconnect your CAN hardware. The can0 interface should be configured automatically with the specified bitrate.

Troubleshooting

If the service doesn’t start as expected, check the status and logs using:

sudo systemctl status setup-can0.service
sudo journalctl -xe

Summary

This systemd service method is suitable for automatically configuring CAN devices on boot or when the hardware is connected, making it ideal for persistent and reliable configurations.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published