Ubuntu绑定USB接口到固定端口

绑定端口

  1. 打开终端,输入以下命令查看USB端口信息:
bash 复制代码
udevadm info -a -n /dev/ttyUSB0

执行后,可以看到部分输出如下:

找到第一个,a-b:c格式的KERNELS,记住这个值,后面会用到。

bash 复制代码
lin@lin-B660M-D2H-DDR4:~$ udevadm info -a -n /dev/ttyUSB0

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-11/1-11.1/1-11.1:1.0/ttyUSB0/tty/ttyUSB0':
    KERNEL=="ttyUSB0"
    SUBSYSTEM=="tty"
    DRIVER==""

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-11/1-11.1/1-11.1:1.0':
    KERNELS=="1-11.1:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="ch341"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{bInterfaceSubClass}=="01"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{authorized}=="1"
    ATTRS{bInterfaceClass}=="ff"
    ATTRS{bInterfaceProtocol}=="02"
    ATTRS{supports_autosuspend}=="1"
    ATTRS{bNumEndpoints}=="03"

其中需要用到的信息有:

  • KERNELS=="1-11.1:1.0"

同样的,另一个端口如下:

  • KERNELS=="1-11.2:1.0"
  1. 添加udev规则:
bash 复制代码
sudo vim /etc/udev/rules.d/test_usb.rules

添加以下内容:

如果是/dev/ttyUSB*,则KERNEL要改为KERNEL=="ttyUSB*"

如果是/dev/video*,则KERNEL要改为KERNEL=="video*"

bash 复制代码
KERNEL=="ttyUSB*", KERNELS=="1-11.2:1.0", MODE:="0777", SYMLINK+="ttyUSB_test_back"
KERNEL=="ttyUSB*", KERNELS=="1-11.1:1.0", MODE:="0777", SYMLINK+="ttyUSB_test_front"

保存并退出,执行以下命令:

bash 复制代码
sudo udevadm control --reload-rules
sudo udevadm trigger
sudo service udev reload
sudo service udev restart
  1. 重启后,就可以通过以下命令查看端口号:
bash 复制代码
ls /dev/ttyUSB*

输出:

bash 复制代码
/dev/ttyUSB0  /dev/ttyUSB1  /dev/ttyUSB_test_back  /dev/ttyUSB_test_front
相关推荐
~光~~1 天前
【环境配置 升级gcc】RK3588 Ubuntu20.04 gcc9升级为gcc10
ubuntu·gcc
青草地溪水旁1 天前
linux信号(14)——SIGALRM:从“手机闹钟”看SIGALRM:进程的非阻塞定时神器
linux·信号机制
心灵宝贝1 天前
libopenssl-1_0_0-devel-1.0.2p RPM 包安装教程(openSUSE/SLES x86_64)
linux·服务器·数据库
BullSmall1 天前
linux zgrep命令介绍
linux·运维
emma羊羊1 天前
【文件读写】图片木马
linux·运维·服务器·网络安全·靶场
wdfk_prog1 天前
闹钟定时器(Alarm Timer)初始化:构建可挂起的定时器基础框架
java·linux·数据库
芭拉拉小魔仙1 天前
Vue项目中如何实现表格选中数据的 Excel 导出
前端·vue.js·excel
RE-19011 天前
Excel基础知识 - 导图笔记
数据分析·学习笔记·excel·思维导图·基础知识·函数应用
你疯了抱抱我1 天前
【SSH】同一局域网下windows使用Xshell SSH连接另一台 ubuntu 22.04 电脑
运维·ubuntu·ssh
2301_818411551 天前
Ubuntu之apt更新源
linux·运维·ubuntu