PyUSB设备已声明,detach_kernel_driver返回实体未找到
我正在尝试在Ubuntu上使用PyUSB从USB设备进行批量读写。不过,我到现在为止都没有成功。
import usb.core
import usb.util
dev = usb.core.find(idVendor=0xXXXX,idProduct=0xYYYY)
if dev is None:
raise ValueError('Device not found.')
try:
dev.detach_kernel_driver(0)
except:
print "exception dev.detach_kernel_driver(0)"
pass
dev.set_configuration()
print "all done"
这是我正在使用的简单脚本。我创建了一个文件,路径是/etc/udev/rules.d/40-basic-rules.rules
,里面的内容是:
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device",SYSFS{idVendor}=="XXXX" , SYSFS{idProduct}=="YYYY", MODE="0666"
这是针对我设备的相应设置。
以root身份运行这个脚本时,会出现一个错误:usb.core.USBError: [Errno 16] Resource busy
,因为dev.detach_kernel_driver(0)
抛出了一个异常:usb.core.USBError: [Errno 2] Entity not found
。
在dmesg中,我看到这些信息:
[ 638.007886] usb 1-1: usbfs: interface 1 claimed by usb-storage while 'python' sets config #1
[ 643.425802] usb 1-1: usbfs: interface 1 claimed by usb-storage while 'python' sets config #1
[ 647.957932] usb 1-1: usbfs: interface 1 claimed by usb-storage while 'python' sets config #1
有没有人能告诉我,我缺少了什么才能访问这个设备?
1 个回答
7
你的问题和我一样,就是在调用 set_configuration()
之前,需要把内核从每一个接口上分离开来。下面是我现在用来连接USB音频设备的代码(包括一些基础的设置):
import usb.core
import usb.util
scarlet = usb.core.find(idVendor = 0x1235) # Focusrite
if not scarlet: print"No Scarlet"
c = 1
for config in scarlet:
print 'config', c
print 'Interfaces', config.bNumInterfaces
for i in range(config.bNumInterfaces):
if scarlet.is_kernel_driver_active(i):
scarlet.detach_kernel_driver(i)
print i
c+=1
scarlet.set_configuration()