假设您使用Linux和libusb-1.0作为PyUSB的后端库。// Detach a kernel driver from an interface.
// If successful, you will then be able to claim the interface and perform I/O.
int libusb_detach_kernel_driver (libusb_device_handle *dev,
int interface_number)
// Re-attach an interface's kernel driver, which was previously
// detached using libusb_detach_kernel_driver().
int libusb_attach_kernel_driver(libusb_device_handle *dev,
int interface_number)
因此,基本上,您需要首先调用detach_kernel_driver来从设备接口分离已经连接的内核驱动程序(如果有的话),这样您就可以在代码中与它通信(它要么是您的代码,要么是某个内核驱动程序在与设备接口通信)。完成后,您可能需要调用attach_kernel_driver重新连接内核驱动程序。
我相信如果您可以确保没有为给定的设备加载内核驱动程序(或者在运行代码之前手动卸载它),那么就不需要调用任何这些C函数/Python方法。
编辑:
我刚把这段代码(基于你的示例)处理好。注意:为了简单起见,我将0硬编码为detach_kernel_driver和attach_kernel_driver的接口号-我想您应该使它更智能。import usb
dev = usb.core.find(idVend