Sample Header Ad - 728x90

Udev rule to connect USB to CAN Lawicell

1 vote
1 answer
971 views
I am using a Lawicel CAN adapter to connect to a sensor, and would like to setup a udev rule to avoid the manual can setup. The manual can setup:
sudo slcand -o -c -f -s6 /dev/ttyUSB0 slcan0
sudo ifconfig slcan0 up
Running candump slcan0 shows, by printing data, that the connection works. I am thus following a guide to setting up the udev rule from [link](http://pascal-walter.blogspot.com/2015/08/installing-lawicel-canusb-on-linux.html) . But this doesn't work. The udev rule is
# Lawicel CANUSB module
ACTION=="add", ENV{ID_MODEL}=="CANUSB", ENV{SUBSYSTEM}=="tty", RUN+="/usr/bin/logger [udev] Lawicel CANUSB detected - running slcan_add.sh!", RUN+="/usr/local/bin/slcan_add.sh $kernel"
ACTION=="remove", ENV{ID_MODEL}=="CANUSB", ENV{SUBSYSTEM}=="usb", RUN+="/usr/bin/logger [udev] Lawicel CANUSB removed - running slcan_remove.sh!", RUN+="/usr/local/bin/slcan_remove.sh"
and the slcan_add.sh script is
#!/bin/sh
# Bind the USBCAN device
slcand -o -c -f -s6 /dev/$1 slcan0
sleep 2
ifconfig slcan0 up
then I reload the rules and restart udev, and tail -f /var/log/syslog returns the following
morten@thinkpad:~$ tail -f /var/log/syslog
Oct  7 10:56:21 thinkpad kernel: [ 1580.917239] usb 1-1: Detected FT232RL
Oct  7 10:56:21 thinkpad kernel: [ 1580.918002] usb 1-1: FTDI USB Serial Device converter now attached to ttyUSB0
Oct  7 10:56:21 thinkpad mtp-probe: checking bus 1, device 10: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-1"
Oct  7 10:56:21 thinkpad mtp-probe: bus: 1, device: 10 was not an MTP device
Oct  7 10:56:21 thinkpad root: [udev] Lawicel CANUSB detected - running slcan_add.sh!
Oct  7 10:56:21 thinkpad slcand: starting on TTY device /dev/ttyUSB0
Oct  7 10:56:21 thinkpad slcand: attached TTY /dev/ttyUSB0 to netdevice slcan0
Oct  7 10:56:21 thinkpad slcand: netdevice slcan0 renamed to slcan0
Oct  7 10:56:21 thinkpad NetworkManager:   [1633596981.6497] manager: (slcan0): new Generic device (/org/freedesktop/NetworkManager/Devices/8)
Oct  7 10:56:21 thinkpad systemd-udevd: ethtool: autonegotiation is unset or enabled, the speed and duplex are not writable.
Oct  7 10:56:23 thinkpad snapd: hotplug.go:199: hotplug device add event ignored, enable experimental.hotplug
Oct  7 10:56:23 thinkpad mtp-probe: checking bus 1, device 10: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-1"
Oct  7 10:56:23 thinkpad mtp-probe: bus: 1, device: 10 was not an MTP device
Oct  7 10:56:26 thinkpad ModemManager:   [base-manager] couldn't check support for device '/sys/devices/pci0000:00/0000:00:14.0/usb1/1-1': not supported by any plugin
Oct  7 10:56:27 thinkpad gnome-shell: Removing a network device that was not added
Oct  7 10:56:27 thinkpad gnome-shell: Removing a network device that was not added
and candump slcan0 returns SIOCGIFINDEX: No such device. How should I fix this? **NOTE**: Using ubuntu 20.04. **EDIT**: The udev rule properly recognizes the device and runs the script, so the udev rule should work I figure. If I manually set the usb port and run the shell script with sudo ./sclan_add.sh then it also works, displaying the following in tail -f /var/log/syslog
Oct  7 12:53:26 agrirobot slcand: starting on TTY device /dev/ttyUSB0
Oct  7 12:53:26 agrirobot slcand: attached TTY /dev/ttyUSB0 to netdevice slcan0
Oct  7 12:53:26 agrirobot slcand: netdevice slcan0 renamed to slcan0
Oct  7 12:53:26 agrirobot NetworkManager:   [1633604006.8360] manager: (slcan0): new Generic device (/org/freedesktop/NetworkManager/Devices/22)
Oct  7 12:53:26 agrirobot systemd-udevd: ethtool: autonegotiation is unset or enabled, the speed and duplex are not writable.
I can't really see what the problem is with the udev rule calling the shell script, but for some reason this makes the difference.
Asked by Morten Nissov (130 rep)
Oct 7, 2021, 08:59 AM
Last activity: Oct 25, 2021, 01:30 PM