Linux 如何知道哪个设备连接在哪个 /dev/ttyUSB 端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13914226/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:03:42  来源:igfitidea点击:

How to know which device is connected in which /dev/ttyUSB port

linuxubuntuusbttyudev

提问by Mayur

I am using two Wavecom 16-port modems. When I attach the modems to my system, I am able to list of all the /dev/ttyUSBport names, but also I want to know, which modem is containing ports 0 to 16 and which one is containing ports 17 to 32?

我正在使用两个 Wavecom 16 端口调制解调器。当我将调制解调器连接到我的系统时,我能够列出所有/dev/ttyUSB端口名称,但我也想知道哪个调制解调器包含端口 0 到 16,哪个包含端口 17 到 32?

The modems may be attached and removed many times in a single day, so I also want to keep logs when modems get disconnected and connected again.

调制解调器可能会在一天内多次连接和移除,因此我还想在调制解调器断开连接并再次连接时保留日志。

Any idea how to do so using c/c++/php script/node.js ?

知道如何使用 c/c++/php script/node.js 这样做吗?

采纳答案by rodrigo

You can get this information from the sysfilesystem. It is easy to check from the shell, and then do a program that does the same:

您可以从sys文件系统中获取此信息。很容易从 shell 中检查,然后做一个程序来做同样的事情:

  1. cd /sys/devices
  2. Find the directory of the first of your ports: find -name "ttyUSB0". It will probably find them in something like ./pci0000:00/0000:00:1d.0/usb2/2-2/2-2.1/2-2.1:1.0/...
  3. The pci*part is the USB controller. The interesting bit is the 2-2.1which is the USB device. In that directory there are a lot of files that identify your device:

    • serial: The serial number. Probably what you want.
    • idVendorand idProduct: The USB identifier of the device.
  1. cd /sys/devices
  2. 找到第一个端口的目录:find -name "ttyUSB0". 它可能会在类似的东西中找到它们./pci0000:00/0000:00:1d.0/usb2/2-2/2-2.1/2-2.1:1.0/...
  3. pci*部分是USB控制器。有趣的一点是2-2.1这是 USB 设备。在该目录中有很多文件可以识别您的设备:

    • serial: 序列号。大概是你想要的。
    • idVendoridProduct:设备的 USB 标识符。

An easy alternatively to steps 1 and 2 is:

步骤 1 和 2 的一个简单替代方法是:

  1. cd /sys/class/tty/
  2. readlink ttyUSBnwill give you the full path of the device directory.
  1. cd /sys/class/tty/
  2. readlink ttyUSBn将为您提供设备目录的完整路径。

As a footnote, note that some parts of the sysfsare considered API stable and some parts are not. For more information see the official sysfs rules.

作为脚注,请注意 的某些部分sysfs被认为是 API 稳定的,而某些部分则不是。有关更多信息,请参阅官方 sysfs 规则