假设您使用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方法.
编辑:
我刚刚得到这段代码(基于你的样本)工作.注意:为简单起见,我将hardcoded 0作为detach_kernel_driver和attach_kernel_driver的接口编号 – 我想你应该让它变得更聪明.
import usb
dev = usb.core.find(idVendor=0x0403, idProd