pyusb:无法设置配置
我正在试图制作一个脚本(在Linux上),可以打开或关闭我的鼠标光源。pyusb:无法设置配置
这是我的代码至今:
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,接口)
脚本返回错误:usb.core.USBError:资源忙
为什么我分离内核驱动程序后仍然很忙?
不知道这是否会解决,但你的鼠标设置正确的udev规则?我有一个类似的问题与自定义设备的朋友为我做的,我解决了通过添加规则,如:在我/etc/udev/rules.d
文件夹
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"
。
HTH!
编辑:添加规则之前,尝试用sudo
运行脚本。如果它能以这种方式工作,它几乎肯定是一个权限设置,将由上述规则修复。
我也有这个问题。如果你先设置了“set_configuration”,那么你的代码运行良好,然后才声明该接口。这也是为了这里建议:这是根和添加的规则运行的时候:“资源忙usb.core.USBError” http://libusb.sourceforge.net/api-1.0/caveats.html
有完全相同的问题,这为我解决了这个问题。从答案中的链接引用:_上述方法[set config,然后声明接口]工作,因为一旦接口声明,没有应用程序或驱动程序能够选择其他配置。这也解释了为什么会引发错误:原始代码声称该设备,从而锁定其配置。 – jro
我仍然得到。但你的答案让我能够以普通用户的身份运行脚本:) – IronMonkey
@IronMonkey - 是的。这是规则的想法:将设备的控制权分配给您! :)如果这个答案对你有用,不要忘记加注并最终接受它,如果它解决了问题。 – mac