PyUSB设备已声明,detach_kernel_driver返回未找到实体 [英] PyUSB device claimed, detach_kernel_driver return Entity Not Found
问题描述
我正在尝试使用PyUSB从Ubuntu上的USB设备进行批量读取和写入. 但是,我一直做不到这一点.
I'm attempting to do bulk reads and writes from a USB device on Ubuntu using PyUSB. However, i've been unsuccessful at getting that far.
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
其中包含
This is the simple script I'm using. I've created /etc/udev/rules.d/40-basic-rules.rules
which contains
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
Running the script as is as root raises a usb.core.USBError: [Errno 16] Resource busy
error because the dev.detach_kernel_driver(0)
throws the exception usb.core.USBError: [Errno 2] Entity not found
在dmesg中,我看到了这些消息,
in dmesg I see these messages,
[ 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
对我想获得该设备的缺失有何想法?
Any thoughts on what I'm missing to get access to this device?
推荐答案
您的问题(与我的问题一样)是,您需要先从每个接口分离内核,然后才能set_configuration()
.这是我现在使用的代码(包括一些脚手架),用于连接到USB音频设备:
Your problem, like mine, is that you need to detach the kernel from each and every interface before you can set_configuration()
. Here's the code I am using right now (including some scaffolding) for connecting to a USB audio device:
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()
这篇关于PyUSB设备已声明,detach_kernel_driver返回未找到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!