2

我正在尝试使用 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

对我缺少访问此设备的任何想法有什么想法吗?

4

1 回答 1

6

您的问题和我的一样,是您需要先将内核与每个接口分离,然后才能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()
于 2016-04-08T17:01:16.307 回答