【问题标题】:PyUSB device claimed, detach_kernel_driver return Entity Not FoundPyUSB 设备已声明,detach_kernel_driver 返回未找到实体
【发布时间】:2014-06-05 21:09:39
【问题描述】:

我正在尝试使用 PyUSB 从 Ubuntu 上的 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

有什么想法让我无法访问此设备吗?

【问题讨论】:

    标签: python ubuntu usb pyusb


    【解决方案1】:

    您的问题和我的一样,是您需要先将内核与每个接口分离,然后才能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()
    

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多