我在 python 中使用轮询命令 (glob('/dev/tty[A-Za-z]*')) 为我的应用程序定期检测连接到我的 linux pc 的 usb 设备。有没有办法检测自动连接的 USB 设备?
最佳答案
这是一个开始。您可以找到您的 USB 供应商 here .你必须自己编写一个 current_list_usb
,设置一个时间间隔来检查,这样你就可以比较并查看是否连接了新设备。导入 usb 模块时使用的一些代码:
import usb, usb.core, usb.util, usb.backend.libusb1
...snippet...
# usb.core.find()
# find our device
dev = usb.core.find(idVendor= ...., idProduct= ....)
#dev_1 = usb.util.find_descriptor(cfg, find_all =True)
# was it found?
if dev is None:
raise ValueError('Device not found')
#x = dev.set_configuration()
#print (dev)
#print (help(usb.core))
if usb.core.find(find_all=True, bDeviceClass=7) is None:
raise ValueError('No printer found')
关于python - python 中的 usb 自动检测 for linux env,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47857410/