简体   繁体   English

识别哪个 USB 设备是 /dev/ttyUSB0

[英]Identify which USB device is /dev/ttyUSB0

I'm working with a Linux distribution (Raspbian) and I have two USB-Serial adapters which I'm connecting to the microcontroller.我正在使用 Linux 发行版(Raspbian),我有两个 USB 串行适配器,我正在连接到微控制器。 Whenever I connect both adapters, the serial ports "/dev/ttyUSB0" and "/dev/ttyUSB1" appear;每当我连接两个适配器时,都会出现串行端口“/dev/ttyUSB0”和“/dev/ttyUSB1”; here "/dev/ttyUSB0" is the adapter that was physically connected first (identified first by the system).这里的“/dev/ttyUSB0”是首先物理连接的适配器(系统首先识别)。

Now, I'm looking for a way to figure out which adapter is which in the event of a microcontroller restart.现在,我正在寻找一种方法来确定在微控制器重启的情况下哪个适配器是哪个适配器。 Ie, both adapters remained plugged into their USB ports and I can't physically unplug/replug to force which adapter is "USB0".即,两个适配器仍然插入其 USB 端口,我无法物理拔出/重新插入以强制哪个适配器是“USB0”。

Basic research led me to these commands (pictures for reference):基础研究使我得到了这些命令(图片供参考):

ls /dev/ttyUSB* To list out the USB-serial ports that are active. ls /dev/ttyUSB*列出活动的 USB 串行端口。

在此处输入图像描述

lsusb To get more information about the USB buses and connected devices. lsusb获取有关 USB 总线和连接设备的更多信息。

在此处输入图像描述

Is there a way to relate these two results (or an alternative) to figure out what I need?有没有办法将这两个结果(或替代方法)联系起来以找出我需要什么? For instance, in the pictures above "/dev/ttyUSB0" is "Bus 001 Device 008: ID 1a86:...", but how can I find that out through software (preferably using Python, but a shell script could work too)?例如,在上面的图片中“/dev/ttyUSB0”是“Bus 001 Device 008: ID 1a86:...”,但是我怎样才能通过软件找到它(最好使用 Python,但 shell 脚本也可以工作) ?

Since you mentioned that you want to do it from Python, pyudev has the following example code to access everything udev knows about a device identified by a device file:由于您提到要从 Python 执行此操作, pyudev具有以下示例代码来访问udev知道的有关设备文件标识的设备的所有内容:

from pyudev import Context, Device
context = Context()
device = Devices.from_device_file(context, '/dev/sda')

I believe that should work very nicely with /dev/ttyUSB0 as well.我相信这也应该与/dev/ttyUSB0一起很好地工作。

See https://pyudev.readthedocs.io/en/latest/api/pyudev.html#pyudev.Devices.from_device_filehttps://pyudev.readthedocs.io/en/latest/api/pyudev.html#pyudev.Devices.from_device_file

Once you have the device udev instance in Python, you can access device.attributes and device.properties to get a wealth of information including VID, PID, string descriptors, and so on.在 Python 中拥有设备 udev 实例后,您可以访问device.attributesdevice.properties以获得丰富的信息,包括 VID、PID、字符串描述符等。 The documentation says that文件说

all well-known dictionary methods and operators (eg .keys() , .items() , in ) are available to access device properties.所有著名的字典方法和运算符(例如.keys().items()in )都可用于访问设备属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM