简体   繁体   English

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

[英]is it possible to do the serial communication with /dev/bus/usb/<bus>/<device>

I'm working on a project in which I've to do some serial communication with whichever device is connected (ttyS0, ttyS1 or ttyUSB0).我正在做一个项目,我必须与连接的任何设备(ttyS0、ttyS1 或 ttyUSB0)进行一些串行通信。 Luckily I've come across a very useful stackoverflow link: " Simple way to query connected USB devices info in Python? ".幸运的是,我遇到了一个非常有用的 stackoverflow 链接:“ 在 Python 中查询连接的 USB 设备信息的简单方法? ”。 In this link there is a python code which works perfectly fine and it gives a proper device name and details.在此链接中有一个 python 代码,它运行良好,并提供了正确的设备名称和详细信息。

here in the example code: "/dev/bus/usb/005/002" is the device information of "FT232 Serial (UART)".在示例代码中:“/dev/bus/usb/005/002”是“FT232 Serial (UART)”的设备信息。 SO, is there a way to find either mapping of /dev/bus/usb/005/002 with ttyS0/ ttyUSB0 or direct access of the UART with the device information and do the serial communication using "/dev/bus/usb/< bus >/< device >" instead of ttyS0 or ttyUSB0.所以,有没有办法找到 /dev/bus/usb/005/002 与 ttyS0/ttyUSB0 的映射或使用设备信息直接访问 UART 并使用“/dev/bus/usb/<总线 >/< 设备 >" 而不是 ttyS0 或 ttyUSB0。

python code:蟒蛇代码:

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

result:结果:

{'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'}

Thanking with regards Aatif shaikh感谢 Aatif shaikh

A useful utilities is udevadm .一个有用的工具是udevadm For example, I have this USB serial adapter:例如,我有这个 USB 串行适配器:

$ lsusb|grep -i prolific
Bus 001 Device 077: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port

Running udevadm on it yields a bunch of information.在其上运行udevadm会产生大量信息。 Here's the start of it:这是它的开始:

$ 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"

You can then look into sysfs for more details:然后,您可以查看sysfs以获取更多详细信息:

$ 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

You'll notice a sub-directory ( 1-3.4.4:1.0 ) for each implemented USB device(interface? function?) within the adapter (in my case just one; I have other 4-port USB serial adapter with 4 sub-directories too).您会注意到1-3.4.4:1.0中每个实现的 USB 设备(接口?功能?)的子目录( 1-3.4.4:1.0 )(在我的情况下只有一个;我有其他 4 端口 USB 串行适配器,带有 4 个子- 目录)。 If you look in there, you can eventually find the device node:如果您查看那里,您最终可以找到设备节点:

$ 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

That last line is the device node major/minor number, which you can search for in /dev, or use to create your own device node.最后一行是设备节点的主要/次要编号,您可以在 /dev 中搜索,或用于创建您自己的设备节点。

The directory /sys/class/tty/ lists all ttys, and /sys/bus/usb-serial/devices/ lists all USB serial adapters.目录/sys/class/tty/列出所有 tty, /sys/bus/usb-serial/devices/列出所有 USB 串行适配器。 It might be easier to start from those directories (rather than udevadm ) to find all relevant devices, then determine which one of those matches the USB device you care about.从这些目录(而不是udevadm )开始查找所有相关设备可能更容易,然后确定其中哪一个与您关心的 USB 设备相匹配。

$ 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/

If you assume that you can take those path names (the link values) and go up to ../.. and find the USB device, and find its bus/dev number.如果您假设您可以使用这些路径名(链接值)并转到../..并找到 USB 设备,并找到它的总线/开发编号。 I /think/ this is safe, but it depends on whether different USB devices/drivers always lay out their sysfs identically, which I believe they do for a given device type.我/认为/这是安全的,但这取决于不同的 USB 设备/驱动程序是否总是以相同的方式布置它们的 sysfs,我相信它们是为给定的设备类型所做的。

$ 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

In general, sysfs contains all the information you want;一般来说, sysfs包含你想要的所有信息; you just have to work out how to traverse it.你只需要弄清楚如何遍历它。

Re: your comment on your question of "I'm just trying to find the link between 'device': '/dev/bus/usb/005/002', 'tag': 'Future Technology Devices International, Ltd FT232 Serial (UART) IC', 'id': '0403:6001'} and ttyUSB0. so that I can always check what type of device is connected to any serial port before I begin the serial communication.":回复:您对“我只是想找到‘设备’之间的链接的评论:‘/dev/bus/usb/005/002’,‘标签’:‘Future Technology Devices International, Ltd FT232 Serial ( UART) IC', 'id': '0403:6001'} 和 ttyUSB0。这样我就可以在开始串行通信之前检查连接到任何串行端口的设备类型。":

Be careful not to read too much into the device name or USB device/vendor ID or name.注意不要过多地读取设备名称或 USB 设备/供应商 ID 或名称。 Here's why.这是为什么。

Device node name/path: - There are different types (protocols) of USB serial adapters.设备节点名称/路径: - USB 串行适配器有不同类型(协议)。 For example, I have both /dev/ttyUSB0 and /dev/ttyACM0 on my system for different types of adapter.例如,我的系统上有/dev/ttyUSB0/dev/ttyACM0用于不同类型的适配器。 - The user can use udev rules to rename or symlink to the device node. - 用户可以使用 udev 规则重命名或符号链接到设备节点。 For example, on my system, I have a rule that created /dev/console-nano which is a symlink to some ttyUSB node such as /dev/ttyUSB0 (the exact value depends on the order the devices are enumerated).例如,在我的系统上,我有一个创建/dev/console-nano的规则,它是指向某个 ttyUSB 节点(例如/dev/ttyUSB0 )的符号链接(确切值取决于枚举设备的顺序)。

Thus, you should only check (via sysfs ) whether the device node is a tty and perhaps also whether it's backed by the USB subsystem, or similar subsystems such as ISA/PCI/PCIe serial ports.因此,您应该只检查(通过sysfs )设备节点是否是 tty,也许还应该检查它是否由 USB 子系统或类似的子系统(如 ISA/PCI/PCIe 串行端口)支持。

USB vendor/device ID: USB 供应商/设备 ID:

There are many many vendors of USB serial adapters, even just at the chip level. USB 串行适配器的供应商有很多,甚至只是在芯片级别。 For example, FTDI (many devices eg FT232R, FT4232H, ...), CH430, PL2303, Linux USB device port (common on embedded hardware) acting as a CDC ACM serial device, etc.例如,FTDI(许多设备,例如 FT232R、FT4232H、...)、CH430、PL2303、充当 CDC ACM 串行设备的 Linux USB 设备端口(常见于嵌入式硬件)等。

Thus, you should detect devices based on their capability (being a tty or serial port), rather than based on their specific USB device/vendor ID.因此,您应该根据设备的能力(是 tty 或串行端口)来检测设备,而不是根据它们特定的 USB 设备/供应商 ID。

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

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