AttributeError:'generator'对象没有属性'set_configuration'

时间:2016-06-06 09:25:27

标签: python raspberry-pi codec

我想从连接到树莓派的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')

这是我的错误: AttributeError:'generator'对象没有属性'set_configuration'

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

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

1 个答案:

答案 0 :(得分:0)

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