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
相关推荐
天码-行空7 分钟前
【大数据环境安装指南】HBase集群环境搭建教程
大数据·linux·运维·hbase
天空之外1368 分钟前
生成一个带 IP 的自签证书、制作Http证书
linux·运维·服务器
释怀不想释怀34 分钟前
linux常见安装(JDK,mysql,nginx)
linux·运维·服务器
杰克崔36 分钟前
do_exit的hungtask问题及coredump的实验及原理分析一
linux·运维·服务器·车载系统
pengdott43 分钟前
Linux进程数据结构与组织方式深度解析
linux·运维·服务器
Java 码农44 分钟前
gitlab gitrunner springboot 多环境多分支部署 (非容器方式,使用原生linux 环境)
linux·spring boot·gitlab
LongQ30ZZ1 小时前
Linux的常见指令
linux·服务器
走向IT1 小时前
vdbench在Centos系统上联机测试环境搭建
linux·运维·centos
阳宗德1 小时前
基于CentOS Linux release 7.1实现了Oracle Database 11g R2 企业版容器化运行
linux·数据库·docker·oracle·centos
liulilittle1 小时前
libxdp: No bpffs found at /sys/fs/bpf
linux·运维·服务器·开发语言·c++