尝试使用PyUSB生成产品/供应商id列表

2024-10-04 05:29:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Pyusb生成产品/供应商ID列表,但遇到了问题。我在网上找到了一个来自orangecoat的建议。在

import sys
import usb.core
import usb.util

dev = usb.core.find(find_all=True)

if dev is None:
   raise ValueError('Device not found')

cfg = dev.get_active_configuration()

但是Python给出了以下错误:

^{pr2}$

有人能帮我理解我为什么会犯这个错误吗? 谢谢你


Tags: coredevimportid列表产品错误sys
2条回答

你已经快到了,但是你需要迭代你的dev对象,它是一个生成器。在

dev = usb.core.find(find_all=True)
for d in dev:
    print usb.util.get_string(d,128,d.iManufacturer)
    print usb.util.get_string(d,128,d.iProduct)
    print (d.idProduct,d.idVendor)

保存此脚本

test.py

import usb.core
import usb.util

dev = usb.core.find(find_all=True)

# get next item from the generator
d = dev.next() 
print d.get_active_configuration()

然后,运行这个

sudo python test.py

在使用python3的Windows上,您需要将d = dev.next()更改为d = next(dev)(正如@gabin在评论中指出的那样)

相关问题 更多 >