【问题标题】:How to dynamically connect to a serial port based on `lsusb` so that the devices connect to the right thing如何基于`lsusb`动态连接串口,使设备连接正确
【发布时间】:2022-09-30 20:59:52
【问题描述】:

堆栈溢出,

今天我花了很大一部分时间尝试根据 python 中 \'lsusb\' 的响应动态连接不同的 USB 外围设备。目标是允许设备拔出并重新插入树莓派,软件会自动重新连接。

现在我有这个sn-p的代码:

def __init__(self, timeout = 1):
        self.data = {}
        
        #ports = [\'/dev/ttyACM1\',\'/dev/ttyACM0\',\'/dev/ttyAMA0\']
        device_re = re.compile(b\"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(b\'\\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)
        
        if devices in [\"Boron\']:
# How to connect to the port this is connected to? 
#         for p in ports:
#             try:
#                 self.p = serial.Serial(p, 115200, timeout = timeout, stopbits=1 )
#                 print(\"Connected Outlet on,\", p)
#                 break
#             except:
#                 print(p,\'Taken\')

我得到一个json结构:

 Foundation 3.0 root hub\', \'device\': \"/dev/bus/usb/b\'002\'/b\'001\"}, {\'id\': b\'17ef:608d\', \'tag\': b\'Lenovo Optical Mouse\', \'device\': \"/dev/bus/usb/b\'001\'/b\'006\"}, {\'id\': b\'17ef:6099\', \'tag\': b\'Lenovo Lenovo Traditional USB Keyboard\', \'device\': \"/dev/bus/usb/b\'001\'/b\'011\"}, {\'id\': b\'2b04:c00d\', \'tag\': b\'Particle Boron CDC Mode\', \'device\': \"/dev/bus/usb/b\'001\'/b\'013\"}, {\'id\': b\'2184:0065\', \'tag\': b\'GW Instek GPM-8310 Virtual ComPort\', \'device\': \"/dev/bus/usb/b\'001\'/b\'012\"}, {\'id\': b\'0bc8:5880\', \'tag\': b\'Telmax Communications USB Camera\', \'device\': \"/dev/bus/usb/b\'001\'/b\'010\"}, {\'id\': b\'0bda:5411\', \'tag\': b\'Realtek Semiconductor Corp. RTS5411 Hub\', \'device\': \"/dev/bus/usb/b\'001\'/b\'009\"}, {\'id\': b\'2109:3431\', \'tag\': b\'VIA Labs, Inc. Hub\', \'device\': \"/dev/bus/usb/b\'001\'/b\'002\"}, {\'id\': b\'1d6b:0002\', \'tag\': b\'Linux Foundation 2.0 root hub\', \'device\': \"/dev/bus/usb/b\'001\'/b\'001\"}]

每次在编程脚本期间安装和卸载它时,我都想连接到Particle Boron 设备。

每次运行时如何搜索阵列以连接到正确的串行端口?

    标签: python serial-port usb pyserial


    【解决方案1】:

    我不会直接回答你的问题,而是想提出一个更简单的替代方案。

    其他人通常如何做到这一点,是确保设备在插入时始终映射到文件。这样您就不必动态确定任何内容,这大大简化了您的设置。

    您可以使用 udev 规则来实现这一点。在你的情况下,这样的事情可能会起作用:

    echo 'ACTION=="add", ATTRS{idVendor}=="2b04", ATTRS{idProduct}=="c00d", SYMLINK+="boron"' > /etc/udev/rules.d/99-boron.rules
    

    idVendor 和 idProduct 取自您的 lsusb 输出,这就是 udev 知道将该设备映射到 /dev/boron 的人。

    有了这个,您可以摆脱所有代码来动态获取设备文件。只是 juse /dev/boron 如果它没有连接,文件将不存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 2014-10-15
      相关资源
      最近更新 更多