Udev 规则将 USB 连接到 CAN Lawicell

Udev 规则将 USB 连接到 CAN Lawicell

我正在使用 Lawicel CAN 适配器连接到传感器,并希望设置 udev 规则以避免手动 can 设置。

手册可以设置:

sudo slcand -o -c -f -s6 /dev/ttyUSB0 slcan0
sudo ifconfig slcan0 up

运行candump slcan0通过打印数据表明连接有效。

因此,我正在遵循设置 udev 规则的指南关联。但这行不通。 udev规则是

# 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"

脚本slcan_add.sh

#!/bin/sh
# Bind the USBCAN device
slcand -o -c -f -s6 /dev/$1 slcan0
sleep 2
ifconfig slcan0 up 

然后我重新加载规则并重新启动 udev,并tail -f /var/log/syslog返回以下内容

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[5967]: starting on TTY device /dev/ttyUSB0
Oct  7 10:56:21 thinkpad slcand[5968]: attached TTY /dev/ttyUSB0 to netdevice slcan0
Oct  7 10:56:21 thinkpad slcand[5968]: netdevice slcan0 renamed to slcan0
Oct  7 10:56:21 thinkpad NetworkManager[649]: <info>  [1633596981.6497] manager: (slcan0): new Generic device (/org/freedesktop/NetworkManager/Devices/8)
Oct  7 10:56:21 thinkpad systemd-udevd[5954]: ethtool: autonegotiation is unset or enabled, the speed and duplex are not writable.
Oct  7 10:56:23 thinkpad snapd[668]: 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[791]: <info>  [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[1997]: Removing a network device that was not added
Oct  7 10:56:27 thinkpad gnome-shell[1426]: Removing a network device that was not added

candump slcan0返回SIOCGIFINDEX: No such device。我应该如何解决这个问题?

笔记:使用ubuntu 20.04。

编辑:udev 规则可以正确识别设备并运行脚本,因此我认为 udev 规则应该有效。如果我手动设置 USB 端口并运行 shell 脚本,sudo ./sclan_add.sh那么它也可以工作,显示以下内容tail -f /var/log/syslog

Oct  7 12:53:26 agrirobot slcand[20933]: starting on TTY device /dev/ttyUSB0
Oct  7 12:53:26 agrirobot slcand[20934]: attached TTY /dev/ttyUSB0 to netdevice slcan0
Oct  7 12:53:26 agrirobot slcand[20934]: netdevice slcan0 renamed to slcan0
Oct  7 12:53:26 agrirobot NetworkManager[649]: <info>  [1633604006.8360] manager: (slcan0): new Generic device (/org/freedesktop/NetworkManager/Devices/22)
Oct  7 12:53:26 agrirobot systemd-udevd[20936]: ethtool: autonegotiation is unset or enabled, the speed and duplex are not writable.

我真的看不出调用 shell 脚本的 udev 规则有什么问题,但由于某种原因,这造成了差异。

答案1

解决方案是多种事物的组合,部分灵感来自

udev规则

# Lawicel CANUSB module
ACTION=="add", ENV{ID_MODEL}=="CANUSB", ENV{SUBSYSTEM}=="tty", RUN+="/usr/local/bin/slcan_add_wrapper.sh $kernel"

slcan_add_wrapper.sh

#!/bin/sh
#  Call shell script binding can device
/usr/bin/logger "[udev] Lawicel CANUSB detected - running /usr/local/bin/slcan_add.sh on $1"
echo /usr/local/bin/slcan_add.sh $1 | at now

slcan_add.sh

#!/bin/sh
# Bind the USBCAN device
sleep 1
/usr/bin/logger "binding $1 to slcan0"
slcand -o -c -f -s6 /dev/$1 slcan0
sleep 2
ifconfig slcan0 up

描述

该解决方案结合了利用at以避免 udev 杀死脚本花费太长时间以及sleepslcan_add.sh.从终端手动运行slcan_add.sh无需睡眠,但由于某种原因 udev 规则拒绝在没有睡眠的情况下运行,我不知道为什么,我不想再对此进行故障排除。

如果有人找到更清洁的解决方案,那么我会接受它。

相关内容