如何在Python中通过USB控制多个设备?可行吗?
我想在连接到USB集线器的设备上分别进行一些测试。我的问题是,当我使用usb.core.find
这个功能时,我不知道怎么识别每个设备。有人能帮我吗?
1 个回答
0
这里有一个示例,展示了如何使用循环来列出连接的USB设备,详细内容可以在这个链接找到。
#!/usr/bin/python
import sys
import usb.core
# find USB devices
dev = usb.core.find(find_all=True)
# loop through devices, printing vendor and product ids in decimal and hex
for cfg in dev:
sys.stdout.write('Decimal VendorID=' + str(cfg.idVendor) + ' & ProductID=' + str(cfg.idProduct) + '\n')
sys.stdout.write('Hexadecimal VendorID=' + hex(cfg.idVendor) + ' & ProductID=' + hex(cfg.idProduct) + '\n\n'
你可以查看PyUSB的文档链接,了解更多信息。
还有一个PyUSB的Sourceforge链接,可以获取相关资源。