
Linux锁定USB串口编号
Linux是根据USB串口接入的顺序来编号的。这样一来,如果设备使用USB扩展了串口,由于硬件电路无法控制USB转串口芯片的接入顺序,就会导致串口编号具有随机性,给接下来串口的使用带来困扰。
好在这个问题可以通过编写udev规则来解决,udev规则可以根据USB串口的硬件编号,自动将ttyUSB设备映射成制定的链接,由此应用程序就可以使用制定名称的链接了。同样的方式也可以用于USB以太网扩展所带来的问题。
串口名称映射
查找设备特征
以致远电子的M6Y2C为例,不同的系统指令名称可能会有差异,一些机器上可能是udevinfo指令。
[root@EPC-M6Y2C rules.d]# udevadm info -a -p $(udevadm info -q path -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/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1:1.0/ttyUSB0/tty/ttyUSB0':
KERNEL=="ttyUSB0"
SUBSYSTEM=="tty"
DRIVER==""
looking at parent device '/devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1:1.0/ttyUSB0':
KERNELS=="ttyUSB0"
SUBSYSTEMS=="usb-serial"
DRIVERS=="ftdi_sio"
ATTRS{port_number}=="0"
ATTRS{latency_timer}=="1"
looking at parent device '/devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1:1.0':
KERNELS=="1-1:1.0"
SUBSYSTEMS=="usb"
DRIVERS=="ftdi_sio"
ATTRS{bInterfaceClass}=="ff"
ATTRS{bInterfaceSubClass}=="ff"
ATTRS{bInterfaceProtocol}=="ff"
ATTRS{bNumEndpoints}=="02"
ATTRS{supports_autosuspend}=="1"
ATTRS{bAlternateSetting}==" 0"
ATTRS{bInterfaceNumber}=="00"
ATTRS{interface}=="FT232R USB UART"
重点在于第三段looking at的KERNELS=="1-1:1.0",它标识了这个USB设备的完整路径,如果使用了HUB,这个路径会更长。
编写rules
进入到/etc/udev/rules.d目录后会看到一个后缀为rules的文件。
[root@EPC-M6Y2C ~]# cd /etc/udev/rules.d/
[root@EPC-M6Y2C rules.d]# ls
010.rules zhiyuan_udev.sh*
此处的rules文件为010.rules
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果