【问题标题】:pyusb: cannot set configurationpyusb:无法设置配置
【发布时间】:2012-01-03 08:34:51
【问题描述】:

我正在尝试制作一个脚本(在 linux 上),它可以打开或关闭鼠标中的灯。

这是我目前的代码:

import usb.core
import usb.util
import sys
interface = 0
dev = usb.core.find(idVendor=0x1532, idProduct=0x0017)

def main():

        if dev is None:
            print "device not found"

        else:
        print "device found"
        if dev.is_kernel_driver_active(interface) is True:
            print "but we need to detach kernel driver"
            dev.detach_kernel_driver(interface)
            print "claiming device"
            usb.util.claim_interface(dev, interface)


            print "release claimed interface"
            usb.util.release_interface(dev, interface)
            print "now attaching the kernel driver again"
            dev.attach_kernel_driver(interface)
            print "all done"
return 0

if __name__ == '__main__':
    main()

这很好,但如果我尝试执行以下操作:dev.set_configuration()

claim_interface(dev, interface)

之后

脚本返回错误:usb.core.USBError: Resource busy

为什么我分离了它的内核驱动后它仍然很忙?

【问题讨论】:

    标签: python usb libusb pyusb


    【解决方案1】:

    不确定这是否会解决,但是否正确设置了鼠标的 udev 规则?我在朋友为我做的自定义设备上遇到了类似的问题,我通过添加如下规则解决了:

    SUBSYSTEM !="usb_device", ACTION !="add", GOTO="device_rules_end"
    SYSFS{idVendor} =="1532", SYSFS{idProduct} =="0017", SYMLINK+="mydevice"
    MODE="0666", OWNER="<your-username-here>", GROUP="root"
    LABEL="device_rules_end"
    

    在我的/etc/udev/rules.d 文件夹中。

    HTH!

    编辑:在添加规则之前,请尝试使用sudo 运行您的脚本。如果它会以这种方式工作,那几乎可以肯定是由上述规则修复的权限设置。

    【讨论】:

    • 当以 root 身份运行并添加规则时,我仍然会收到“usb.core.USBError: Resource busy”。但是您的回答使我能够以普通用户的身份运行脚本:)
    • @IronMonkey - 是的。这就是规则的想法:将设备的控制权分配给您! :) 如果此答案对您有用,请不要忘记投票并最终接受它,如果它可以永久解决问题。
    【解决方案2】:

    我也有这个问题。如果您首先“set_configuration”然后才声明接口,则您的代码运行良好。这也是这里建议的顺序:http://libusb.sourceforge.net/api-1.0/caveats.html

    【讨论】:

    • 有完全相同的问题,这解决了我的问题。引用答案中的链接:上述方法 [set config, then claim interface] 有效,因为一旦声明接口,应用程序或驱动程序就无法选择另一个配置。 这也解释了为什么抛出错误:原始代码声明了设备,从而锁定了它的配置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2020-05-21
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多