Linux 如何找出哪个 USB-RS232 设备在哪个 tty 上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9183880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find out which USB-RS232 device is on which tty?
提问by gabriel_agm
I have two different USB devices based on the same USB-RS232 chips. When I plug those in the USB they are mounted to /dev/ttyUSB0...3
我有两个基于相同 USB-RS232 芯片的不同 USB 设备。当我将它们插入 USB 时,它们将安装到 /dev/ttyUSB0...3
My problem is how, inside a script, I can find out which one is on what tty?
我的问题是如何在脚本中找出哪个在哪个 tty 上?
Using lsusb I can differentiate them:
使用 lsusb 我可以区分它们:
$> lsusb | grep 0403:f850
Bus 004 Device 002: ID 0403:f850 Future Technology Devices International, Ltd
$> lsusb | grep 0403:6001
Bus 004 Device 003: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
And using dmesg I can tell where they were mounted:
使用 dmesg 我可以知道它们的安装位置:
$> dmesg | grep 'FTDI USB Serial Device converter now attached'
[36051.393350] usb 4-2: FTDI USB Serial Device converter now attached to ttyUSB1
[36061.823513] usb 4-1: FTDI USB Serial Device converter now attached to ttyUSB0
But "usb 4-1" does not seem to correspond to "Bus 004 Device 002".
但是“usb 4-1”似乎并不对应于“Bus 004 Device 002”。
Can I assume that "Bus 004 Device 001" will always be an "USB root hub" and thus dmesg will count from 1 onwards and lsusb from 2 onwards?
我可以假设“总线 004 设备 001”将始终是“USB 根集线器”,因此 dmesg 将从 1 开始计数,而 lsusb 将从 2 开始计数?
Or do you have another suggestion on how to correlate device ID to mount point (inside a script)?
或者您对如何将设备 ID 与挂载点相关联(在脚本内)有其他建议吗?
I'm using Ubuntu 10.04 LTS.
我正在使用 Ubuntu 10.04 LTS。
采纳答案by sehe
Find more info using sysfs:
使用 sysfs 查找更多信息:
$ ls /sys/bus/usb-serial/devices/ -ltrah
lrwxrwxrwx 1 root root 0 2012-02-07 22:17 ttyUSB0 -> ../../../devices/pci0000:00/0000:00:0f.4/usb2/2-2/2-2:1.0/ttyUSB0
$ ls -ltrad /sys//devices/pci0000:00/0000:00:0f.4/usb2/2-2/2-2:1.0/ttyUSB0
drwxr-xr-x 4 root root 0 2012-02-07 22:17 /sys//devices/pci0000:00/0000:00:0f.4/usb2/2-2/2-2:1.0/ttyUSB0
$ ls -ltrad /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 0 2012-02-01 00:17 /dev/ttyUSB0
Of course, the linked devices/... node contains a lot of information
当然,链接的 devices/... 节点包含很多信息
Adding information based on the OP's comment:
根据 OP 的评论添加信息:
The device number keeps growing if devices are removed/inserted. lsusb -t
can be used to correlate the device numbers with usb bus/port.
如果移除/插入设备,设备数量会不断增加。lsusb -t
可用于将设备编号与 USB 总线/端口相关联。
Then, 'lsusb -d devID' can determine what device is on which port. Finally 'ls /sys/bus/usb-serial/devices/ -ltrah' will list (by bus/port) where it was mounted.
然后,'lsusb -d devID' 可以确定哪个设备在哪个端口上。最后 'ls /sys/bus/usb-serial/devices/ -ltrah' 将列出(按总线/端口)它的安装位置。
Not very convenient, but it 'works'
不是很方便,但它“有效”
回答by starblue
You can use udev to assign a stable symbolic link to each FTDI dongle. Use device and vendor to match the type, and then the serial number for the specific device. Then use the symbolic link in the script.
您可以使用 udev 为每个 FTDI 加密狗分配一个稳定的符号链接。使用设备和供应商来匹配类型,然后是特定设备的序列号。然后在脚本中使用符号链接。
I named my dongles ttyFTDI0 etc., and wrote the numbers on the dongles with a marker:
我将我的加密狗命名为 ttyFTDI0 等,并用记号笔在加密狗上写下数字:
$ cat /etc/udev/ftdi.rules SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="FTDEN97D", SYMLINK+="ttyFTDI0" SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="A600b30P", SYMLINK+="ttyFTDI1" SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="A600bexU", SYMLINK+="ttyFTDI2" SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="FTFMSA54", SYMLINK+="ttyFTDI3" SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="FTFMUW4A", SYMLINK+="ttyFTDI4"
回答by Eliot Blennerhassett
This one-liner udev rule will give a fixed name (e.g. /dev/ttyUSB_A9YL5BJJ) based on serial number for any device with vendor/product ids as given.
这个单行 udev 规则将根据给定供应商/产品 ID 的任何设备的序列号给出一个固定名称(例如 /dev/ttyUSB_A9YL5BJJ)。
Put this into a udev rules file e.g. /etc/udev/rules.d/60-ftdi-serial-names.rules
将其放入 udev 规则文件中,例如 /etc/udev/rules.d/60-ftdi-serial-names.rules
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ENV{ID_SERIAL_SHORT}=="?*", SYMLINK+="ttyUSB_%E{ID_SERIAL_SHORT}"