属性错误: 'generator'对象没有属性'set_configuration'



我想从连接到树莓派的USB音频编解码器收集数据。所以首先我尝试一个简单的程序来写入一些数据

import usb.core
import usb.util
# find our device
dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001)
# was it found?
if dev is None:
raise ValueError('Device not found')
# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()
# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
    intf,
    # match the first OUT endpoint
    custom_match = 
    lambda e: 
        usb.util.endpoint_direction(e.bEndpointAddress) == 
        usb.util.ENDPOINT_OUT)
assert ep is not None
# write the data
ep.write('test')

这是我的错误:属性错误:"生成器"对象没有属性"set_configuration"

以下是教程中关于此函数的内容:之后,我们设置要使用的配置。请注意,没有提供指示我们想要的配置的参数。如您所见,许多 PyUSB 函数对大多数常见设备都有默认值。在这种情况下,配置集是找到的第一个配置集。

所以我不明白为什么我会收到此错误。请问有什么想法吗?

错误消息表明usb.core.find是一个生成器函数。也就是说,它返回一个可迭代的生成器对象,而不是您似乎期望的单个设备对象。您需要以某种方式迭代生成器(例如,使用 for 循环,或将其传递给 list )以获取设备对象。您可能需要在代码中添加逻辑,不仅要处理获取零个设备(例如"Device not found"的情况),还要处理获取多个设备!

最新更新