先展示效果:
[root@raspberrypi ~ 18:24:20]$ls -l /dev/ttyU*
crw-rw---- 1 root dialout 188, 0 Sep 17 18:15 /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 1 Sep 17 18:15 /dev/ttyUSB1
crw-rw---- 1 root dialout 188, 2 Sep 17 18:15 /dev/ttyUSB2
crw-rw---- 1 root dialout 188, 3 Sep 17 18:15 /dev/ttyUSB3
crw-rw---- 1 root dialout 188, 4 Sep 17 18:15 /dev/ttyUSB4
crw-rw---- 1 root dialout 188, 5 Sep 17 18:15 /dev/ttyUSB5
crw-rw---- 1 root dialout 188, 6 Sep 17 18:15 /dev/ttyUSB6
crw-rw---- 1 root dialout 188, 7 Sep 17 18:15 /dev/ttyUSB7
lrwxrwxrwx 1 root root 7 Sep 17 18:15 /dev/ttyUSBEC20M1 -> ttyUSB6
lrwxrwxrwx 1 root root 7 Sep 17 18:15 /dev/ttyUSBEC20M2 -> ttyUSB2
过程
创建文件 /etc/udev/rules.d/99-ec20.rules
# 预先设置一个环境变量接口编号 https://stackoverflow.com/questions/19174482/udev-rule-with-binterfacenumber-doesnt-work
SUBSYSTEMS=="usb", ENV{ID_USB_INTERFACE_NUM}="$attr{bInterfaceNumber}"
# 设备路径需要替换为自己的 使用命令查看 udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB2)
KERNEL=="ttyUSB[0-9]*",ENV{ID_USB_INTERFACE_NUM}=="02",KERNELS=="1-1.3",ATTRS{devpath}=="1.3",ATTRS{idVendor}=="2c7c",ATTRS{idProduct}=="0125",SYMLINK+="ttyUSBEC20M1"
KERNEL=="ttyUSB[0-9]*",ENV{ID_USB_INTERFACE_NUM}=="02",KERNELS=="1-1.4",ATTRS{devpath}=="1.4",ATTRS{idVendor}=="2c7c",ATTRS{idProduct}=="0125",SYMLINK+="ttyUSBEC20M2"
重启服务后,热插拔设备即可
udevadm control --reload && systemctl restart udev && ./usbreset /dev/bus/usb/001/025 && ./usbreset /dev/bus/usb/001/026
附: usbreset 小工具, 将以下内容保存为usbreset.c
(原文: https://blog.csdn.net/w356877795/article/details/120800766)
/* usbreset -- send a USB port reset to a USB device */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main(int argc, char **argv)
{
const char *filename;
int fd;
int rc;
if (argc != 2) {
fprintf(stderr, "Usage: usbreset device-filename\n");
return 1;
}
filename = argv[1];
fd = open(filename, O_WRONLY);
if (fd < 0) {
perror("Error opening output file");
return 1;
}
printf("Resetting USB device %s\n", filename);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0) {
perror("Error in ioctl");
return 1;
}
printf("Reset successful\n");
close(fd);
return 0;
}
编译并赋予权限
gcc usbreset.c -o usbreset && chmod +x ./usbreset
指定设备总线和设备id即可发送重启设备
# lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 025: ID 2c7c:0125 Quectel Wireless Solutions Co., Ltd. EC25 LTE modem
Bus 001 Device 026: ID 2c7c:0125 Quectel Wireless Solutions Co., Ltd. EC25 LTE modem
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
./usbreset /dev/bus/usb/001/025 # 重启指定设备
Q.E.D.