pyusb:无法设置配置
我正在尝试在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()
这个代码运行得很好,但是如果我在claim_interface(dev, interface)之后尝试使用dev.set_configuration(),
脚本就会报错:usb.core.USBError: Resource busy。
为什么在我已经卸载了它的内核驱动后,它还会显示忙碌呢?
2 个回答
4
我也遇到过这个问题。如果你先“设置配置”,然后再声明接口,你的代码就能正常工作。这也是这里推荐的顺序:http://libusb.sourceforge.net/api-1.0/caveats.html
7
不确定这样做是否能解决问题,但你的鼠标的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
来运行你的脚本。如果这样能正常工作,那几乎可以肯定是权限设置的问题,上面的规则就能解决这个问题。