首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以使用/dev/bus/usb/<bus>/<device>进行串行通信

是否可以使用/dev/bus/usb/<bus>/<device>进行串行通信
EN

Stack Overflow用户
提问于 2019-02-11 20:10:31
回答 2查看 1.9K关注 0票数 4

我正在做一个项目,在这个项目中,我必须与任何连接的设备(ttyS0、ttyS1或ttyUSB0)进行串行通信。幸运的是,我遇到了一个非常有用的堆栈溢出链接:"Simple way to query connected USB devices info in Python?“。在这个链接中有一个python代码,它工作得很好,它给出了一个正确的设备名称和详细信息。

这里的示例代码:"/dev/bus/usb/005/002“是"FT232串行(UART)”的设备信息。那么,有没有办法找到/dev/ bus /usb/005/002与ttyS0/ ttyUSB0的映射,或者使用设备信息直接访问UART,并使用"/dev/bus/usb/< bus >/< device >“而不是ttyS0或ttyUSB0进行串行通信。

python代码:

代码语言:javascript
复制
import re
import subprocess
device_re = re.compile("Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I)
df = subprocess.check_output("lsusb")
devices = []
for i in df.split('\n'):
    if i:
        info = device_re.match(i)
        if info:
            dinfo = info.groupdict()
            dinfo['device'] = '/dev/bus/usb/%s/%s' % (dinfo.pop('bus'), dinfo.pop('device'))
            devices.append(dinfo)
print devices

结果:

代码语言:javascript
复制
{'device': '/dev/bus/usb/001/001', 'tag': 'Linux Foundation 2.0 root hub', 'id': '1d6b:0002'}
{'device': '/dev/bus/usb/005/002', 'tag': 'Future Technology Devices International, Ltd FT232 Serial (UART) IC', 'id': '0403:6001'}
{'device': '/dev/bus/usb/005/001', 'tag': 'Linux Foundation 1.1 root hub', 'id': '1d6b:0001'}
{'device': '/dev/bus/usb/004/003', 'tag': 'Lite-On Technology Corp. ', 'id': '04ca:0061'}
{'device': '/dev/bus/usb/004/002', 'tag': 'Dell Computer Corp. ', 'id': '413c:2107'}
{'device': '/dev/bus/usb/004/001', 'tag': 'Linux Foundation 1.1 root hub', 'id': '1d6b:0001'}
{'device': '/dev/bus/usb/003/001', 'tag': 'Linux Foundation 1.1 root hub', 'id': '1d6b:0001'}
{'device': '/dev/bus/usb/002/001', 'tag': 'Linux Foundation 1.1 root hub', 'id': '1d6b:0001'}

向Aatif shaikh致谢

EN

回答 2

Stack Overflow用户

发布于 2019-10-12 01:21:54

一个有用的实用程序是udevadm。例如,我有一个USB串行适配器:

代码语言:javascript
复制
$ lsusb|grep -i prolific
Bus 001 Device 077: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port

在上面运行udevadm会产生一堆信息。这是它的开始:

代码语言:javascript
复制
$ udevadm info -a /dev/bus/usb/001/077

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-3/1-3.4/1-3.4.4':
    KERNEL=="1-3.4.4"
    SUBSYSTEM=="usb"
    DRIVER=="usb"
    ATTR{authorized}=="1"
    ATTR{avoid_reset_quirk}=="0"
    ATTR{bConfigurationValue}=="1"
    ATTR{bDeviceClass}=="00"
    ATTR{bDeviceProtocol}=="00"
    ATTR{bDeviceSubClass}=="00"
    ATTR{bMaxPacketSize0}=="64"
    ATTR{bMaxPower}=="100mA"
    ATTR{bNumConfigurations}=="1"
    ATTR{bNumInterfaces}==" 1"
    ATTR{bcdDevice}=="0300"
    ATTR{bmAttributes}=="a0"
    ATTR{busnum}=="1"
    ATTR{configuration}==""
    ATTR{devnum}=="77"
    ATTR{devpath}=="3.4.4"
    ATTR{idProduct}=="2303"
    ATTR{idVendor}=="067b"
    ATTR{ltm_capable}=="no"
    ATTR{manufacturer}=="Prolific Technology Inc."
    ATTR{maxchild}=="0"
    ATTR{product}=="USB-Serial Controller"
    ATTR{quirks}=="0x0"
    ATTR{removable}=="unknown"
    ATTR{speed}=="12"
    ATTR{urbnum}=="22"
    ATTR{version}==" 2.00"

然后,您可以查看sysfs以了解更多详细信息:

代码语言:javascript
复制
$ ls -l '/sys//devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.4/1-3.4.4'
total 0
drwxr-xr-x 7 root root     0 Oct 11 11:03 1-3.4.4:1.0
-r--r--r-- 1 root root  4096 Oct 11 11:03 bcdDevice
-rw-r--r-- 1 root root  4096 Oct 11 11:03 bConfigurationValue
-r--r--r-- 1 root root  4096 Oct 11 11:03 bDeviceClass
-r--r--r-- 1 root root  4096 Oct 11 11:03 bDeviceProtocol
-r--r--r-- 1 root root  4096 Oct 11 11:03 bDeviceSubClass
-r--r--r-- 1 root root  4096 Oct 11 11:03 bmAttributes
-r--r--r-- 1 root root  4096 Oct 11 11:03 bMaxPacketSize0
-r--r--r-- 1 root root  4096 Oct 11 11:03 bMaxPower
-r--r--r-- 1 root root  4096 Oct 11 11:03 bNumConfigurations
-r--r--r-- 1 root root  4096 Oct 11 11:03 bNumInterfaces
-r--r--r-- 1 root root  4096 Oct 11 11:03 busnum
-r--r--r-- 1 root root  4096 Oct 11 11:03 configuration
-r--r--r-- 1 root root 65553 Oct 11 11:03 descriptors
-r--r--r-- 1 root root  4096 Oct 11 11:03 dev
-r--r--r-- 1 root root  4096 Oct 11 11:03 devnum
-r--r--r-- 1 root root  4096 Oct 11 11:03 devpath
lrwxrwxrwx 1 root root     0 Oct 11 11:03 driver -> ../../../../../../../bus/usb/drivers/usb
drwxr-xr-x 3 root root     0 Oct 11 11:03 ep_00
-r--r--r-- 1 root root  4096 Oct 11 11:03 idProduct
-r--r--r-- 1 root root  4096 Oct 11 11:03 idVendor
-r--r--r-- 1 root root  4096 Oct 11 11:03 ltm_capable
-r--r--r-- 1 root root  4096 Oct 11 11:03 manufacturer
-r--r--r-- 1 root root  4096 Oct 11 11:03 maxchild
lrwxrwxrwx 1 root root     0 Oct 11 11:03 port -> ../1-3.4:1.0/1-3.4-port4
drwxr-xr-x 2 root root     0 Oct 11 11:03 power
-r--r--r-- 1 root root  4096 Oct 11 11:03 product
-r--r--r-- 1 root root  4096 Oct 11 11:03 quirks
-r--r--r-- 1 root root  4096 Oct 11 11:03 removable
--w------- 1 root root  4096 Oct 11 11:03 remove
-r--r--r-- 1 root root  4096 Oct 11 11:03 speed
lrwxrwxrwx 1 root root     0 Oct 11 11:03 subsystem -> ../../../../../../../bus/usb
-rw-r--r-- 1 root root  4096 Oct 11 11:03 uevent
-r--r--r-- 1 root root  4096 Oct 11 11:03 urbnum
-r--r--r-- 1 root root  4096 Oct 11 11:03 version

您会注意到,每个已实现的USB设备都有一个子目录(1-3.4.4:1.0)。函数?)在适配器中(在我的例子中只有一个;我还有其他4端口USB串行适配器,有4个子目录)。如果你往里面看,你最终可以找到设备节点:

代码语言:javascript
复制
$ ls -l '/sys//devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.4/1-3.4.4/1-3.4.4:1.0/'
total 0
-rw-r--r-- 1 root root 4096 Oct 11 11:03 authorized
-r--r--r-- 1 root root 4096 Oct 11 11:03 bAlternateSetting
-r--r--r-- 1 root root 4096 Oct 11 11:03 bInterfaceClass
-r--r--r-- 1 root root 4096 Oct 11 11:03 bInterfaceNumber
-r--r--r-- 1 root root 4096 Oct 11 11:03 bInterfaceProtocol
-r--r--r-- 1 root root 4096 Oct 11 11:03 bInterfaceSubClass
-r--r--r-- 1 root root 4096 Oct 11 11:03 bNumEndpoints
lrwxrwxrwx 1 root root    0 Oct 11 11:03 driver -> ../../../../../../../../bus/usb/drivers/pl2303
drwxr-xr-x 3 root root    0 Oct 11 11:03 ep_02
drwxr-xr-x 3 root root    0 Oct 11 11:03 ep_81
drwxr-xr-x 3 root root    0 Oct 11 11:03 ep_83
-r--r--r-- 1 root root 4096 Oct 11 11:03 modalias
drwxr-xr-x 2 root root    0 Oct 11 11:03 power
lrwxrwxrwx 1 root root    0 Oct 11 11:03 subsystem -> ../../../../../../../../bus/usb
-r--r--r-- 1 root root 4096 Oct 11 11:03 supports_autosuspend
drwxr-xr-x 4 root root    0 Oct 11 11:03 ttyUSB0
-rw-r--r-- 1 root root 4096 Oct 11 11:03 uevent

$ ls -l '/sys//devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.4/1-3.4.4/1-3.4.4:1.0/ttyUSB0'
total 0
lrwxrwxrwx 1 root root    0 Oct 11 11:03 driver -> ../../../../../../../../../bus/usb-serial/drivers/pl2303
-r--r--r-- 1 root root 4096 Oct 11 11:03 port_number
drwxr-xr-x 2 root root    0 Oct 11 11:03 power
lrwxrwxrwx 1 root root    0 Oct 11 11:03 subsystem -> ../../../../../../../../../bus/usb-serial
drwxr-xr-x 3 root root    0 Oct 11 11:03 tty
-rw-r--r-- 1 root root 4096 Oct 11 11:03 uevent

$ ls -l '/sys//devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.4/1-3.4.4/1-3.4.4:1.0/ttyUSB0/tty'
total 0
drwxr-xr-x 3 root root 0 Oct 11 11:11 ttyUSB0

$ ls -l '/sys//devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.4/1-3.4.4/1-3.4.4:1.0/ttyUSB0/tty/ttyUSB0'
total 0
-r--r--r-- 1 root root 4096 Oct 11 11:11 dev
lrwxrwxrwx 1 root root    0 Oct 11 11:11 device -> ../../../ttyUSB0
drwxr-xr-x 2 root root    0 Oct 11 11:11 power
lrwxrwxrwx 1 root root    0 Oct 11 11:11 subsystem -> ../../../../../../../../../../../class/tty
-rw-r--r-- 1 root root 4096 Oct 11 11:11 uevent

$ cat '/sys//devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.4/1-3.4.4/1-3.4.4:1.0/ttyUSB0/tty/ttyUSB0/dev'
188:0

最后一行是设备节点的主要/次要编号,您可以在/dev中搜索它,或者用它来创建您自己的设备节点。

目录/sys/class/tty/列出了所有tty,而/sys/bus/usb-serial/devices/列出了所有USB串行适配器。从这些目录(而不是udevadm)开始查找所有相关设备,然后确定其中哪一个与您关心的USB设备匹配可能会更容易。

代码语言:javascript
复制
$ ls -l /sys/bus/usb-serial/devices/
total 0
lrwxrwxrwx 1 root root 0 Oct 11 11:16 ttyUSB0 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.4/1-3.4.4/1-3.4.4:1.0/ttyUSB0/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB1 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.1/ttyUSB1/
lrwxrwxrwx 1 root root 0 Oct 11 11:16 ttyUSB10 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8.4/1-8.4:1.3/ttyUSB10/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB12 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8.6/1-8.6:1.0/ttyUSB12/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB13 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8.6/1-8.6:1.1/ttyUSB13/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB14 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8.6/1-8.6:1.2/ttyUSB14/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB15 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8.6/1-8.6:1.3/ttyUSB15/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB2 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.2/ttyUSB2/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB3 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.3/ttyUSB3/
lrwxrwxrwx 1 root root 0 Oct 11 11:16 ttyUSB4 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8.4/1-8.4:1.0/ttyUSB4/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB5 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.1/ttyUSB5/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB6 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.2/ttyUSB6/
lrwxrwxrwx 1 root root 0 Sep  6 13:48 ttyUSB7 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.3/ttyUSB7/
lrwxrwxrwx 1 root root 0 Oct 11 11:16 ttyUSB8 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8.4/1-8.4:1.1/ttyUSB8/
lrwxrwxrwx 1 root root 0 Oct 11 11:16 ttyUSB9 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8.4/1-8.4:1.2/ttyUSB9/

如果您假设您可以获取这些路径名(链接值),然后转到../..,找到USB设备,并找到它的总线/开发号。I/我认为/这是安全的,但这取决于不同的USB设备/驱动程序是否总是以相同的方式布局它们的sysf,我相信对于给定的设备类型,它们是这样做的。

代码语言:javascript
复制
$ cat '/sys/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.4/1-3.4.4/1-3.4.4:1.0/ttyUSB0/../../busnum'
1
$ cat '/sys/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.4/1-3.4.4/1-3.4.4:1.0/ttyUSB0/../../devnum' 
77

通常,sysfs包含您想要的所有信息;您只需弄清楚如何遍历它。

票数 3
EN

Stack Overflow用户

发布于 2019-10-12 01:32:32

Re:您对您的问题的评论是“我只想找到' device ':'/dev/bus/usb/005/002','tag':'Future Technology Devices International,Ltd FT232 serial (UART) IC','id':'0403:6001'}和ttyUSB0之间的链接。这样我就可以在开始串行通信之前随时检查连接到任何串行端口的设备类型。”:

注意不要过多地读取设备名称或USB设备/供应商ID或名称。这就是为什么。

设备节点名称/路径:-有不同类型(协议)的USB串行适配器。例如,我的系统上有用于不同类型适配器的/dev/ttyUSB0/dev/ttyACM0。-用户可以使用udev规则重命名或符号链接到设备节点。例如,在我的系统上,我有一个创建/dev/console-nano的规则,它是到某个ttyUSB节点的符号链接,比如/dev/ttyUSB0 (确切的值取决于设备被枚举的顺序)。

因此,您应该只检查(通过sysfs)设备节点是否为tty,也许还应该检查它是否由USB子系统或类似的子系统支持。

USB供应商/设备ID:

USB串行适配器的供应商很多,甚至只是芯片级的。例如,FTDI (许多设备,如FT232R、FT4232H等)、CH430、PL2303、Linux USB设备端口(在嵌入式硬件上常见)充当CDC ACM串行设备等。

因此,您应该根据设备的功能(是tty或串行端口)来检测设备,而不是基于特定的USB设备/供应商ID。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54630344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档