pyusb:无法设置配置

2024-04-20 13:57:48 发布

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

我正在尝试制作一个脚本(在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()

这很好,但如果我尝试执行a:dev.set_configuration()

索赔界面(dev,interface)之后

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

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


Tags: coredevimport脚本ifismaindevice
2条回答

不确定这是否能解决问题,但您的鼠标的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文件夹中。

啊!

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

我也有这个问题。如果您先“设置配置”,然后才声明接口,则代码可以正常工作。这也是这里建议的顺序:http://libusb.sourceforge.net/api-1.0/caveats.html

相关问题 更多 >