Shell-Script

將 USB 連接到 CAN Lawicell 的 Udev 規則

  • October 25, 2021

我正在使用 Lawicel CAN 適配器連接到感測器,並想設置一個 udev 規則以避免手動設置。

手冊可以設置:

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 規則有什麼問題,但出於某種原因,這會有所不同。

解決方案是事物的組合,部分受此啟發

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 規則拒絕沒有睡眠,我不知道為什麼,我不想再解決這個問題。

如果有人找到更清潔的解決方案,那麼我會接受。

引用自:https://unix.stackexchange.com/questions/672228