AttributeError: 'generator' object has no attribute 'set_configuration'

问题描述 投票:0回答:1

我想从连接到树莓派的 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 函数对于大多数常见设备都有默认值。在这种情况下,配置集是第一个找到的。

所以我不明白为什么会出现此错误。 有什么想法吗?

python raspberry-pi codec
1个回答
0
投票

错误消息表明

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

© www.soinside.com 2019 - 2024. All rights reserved.