【发布时间】: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