Windows10下使用pyusb读取U盘遭遇NotImplementedError问题求助
问题描述
尝试用pyusb读取U盘设备,代码如下:
import usb.core import usb.backend.libusb1 VENDOR_ID = 0x0951 PRODUCT_ID = 0x1643 device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID) print(device) if device is None: raise ValueError('Device not found') sys.exit(1) usb.util.claim_interface(device, 0)
成功获取设备句柄,但执行usb.util.claim_interface(device, 0)或device.get_active_configuration()时触发NotImplementedError,错误信息如下:
DEVICE ID 0951:1643 on Bus 001 Address 013 ================= bLength : 0x12 (18 bytes) bDescriptorType : 0x1 Device bcdUSB : 0x200 USB 2.0 bDeviceClass : 0x0 Specified at interface bDeviceSubClass : 0x0 bDeviceProtocol : 0x0 bMaxPacketSize0 : 0x40 (64 bytes) idVendor : 0x0951 idProduct : 0x1643 bcdDevice : 0x100 Device 1.0 iManufacturer : 0x1 Error Accessing String iProduct : 0x2 Error Accessing String iSerialNumber : 0x3 Error Accessing String bNumConfigurations : 0x1 CONFIGURATION 1: 100 mA ================================== bLength : 0x9 (9 bytes) bDescriptorType : 0x2 Configuration wTotalLength : 0x20 (32 bytes) bNumInterfaces : 0x1 bConfigurationValue : 0x1 iConfiguration : 0x0 bmAttributes : 0x80 Bus Powered bMaxPower : 0x32 (100 mA) INTERFACE 0: Mass Storage ============================== bLength : 0x9 (9 bytes) bDescriptorType : 0x4 Interface bInterfaceNumber : 0x0 bAlternateSetting : 0x0 bNumEndpoints : 0x2 bInterfaceClass : 0x8 Mass Storage bInterfaceSubClass : 0x6 bInterfaceProtocol : 0x50 iInterface : 0x0 ENDPOINT 0x81: Bulk IN =============================== bLength : 0x7 (7 bytes) bDescriptorType : 0x5 Endpoint bEndpointAddress : 0x81 IN bmAttributes : 0x2 Bulk wMaxPacketSize : 0x200 (512 bytes) bInterval : 0xff ENDPOINT 0x2: Bulk OUT =============================== bLength : 0x7 (7 bytes) bDescriptorType : 0x5 Endpoint bEndpointAddress : 0x2 OUT bmAttributes : 0x2 Bulk wMaxPacketSize : 0x200 (512 bytes) bInterval : 0xff Traceback (most recent call last): File "C:\Users\Usuario\Documents\keylogger\ADU_USB.py", line 17, in <module> usb.util.claim_interface(device, 0) File "C:\Users\Usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\usb\util.py", line 207, in claim_interface device._ctx.managed_claim_interface(device, interface) File "C:\Users\Usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\usb\core.py", line 113, in wrapper return f(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\usb\core.py", line 170, in managed_claim_interface self.managed_open() File "C:\Users\Usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\usb\core.py", line 113, in wrapper return f(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\usb\core.py", line 131, in managed_open self.handle = self.backend.open_device(self.dev) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\usb\backend\libusb1.py", line 804, in open_device return _DeviceHandle(dev) ^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\usb\backend\libusb1.py", line 652, in __init__ _check(_lib.libusb_open(self.devid, byref(self.handle))) File "C:\Users\Usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\usb\backend\libusb1.py", line 600, in _check raise NotImplementedError(_strerror(ret)) NotImplementedError: Operation not supported or unimplemented on this platform
已确认libusb路径正确,不想使用Zadig替换驱动,寻求解决方法并询问是否可用pyserial替代。
解决方案
1. 以管理员权限运行脚本
Windows下普通权限无法访问USB设备底层接口,管理员权限可提升设备访问权限,大概率解决权限不足导致的"操作不支持"问题。
2. 显式指定libusb后端路径
即使路径正确,显式指定后端文件可避免自动查找时的潜在问题,修改代码如下:
import usb.core import usb.backend.libusb1 # 替换为你的libusb-1.0.dll实际路径 backend = usb.backend.libusb1.get_backend(find_library=lambda x: "C:/path/to/libusb-1.0.dll") VENDOR_ID = 0x0951 PRODUCT_ID = 0x1643 device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID, backend=backend) print(device) if device is None: raise ValueError('Device not found') # 先尝试分离内核驱动并设置设备配置 try: if device.is_kernel_driver_active(0): device.detach_kernel_driver(0) device.set_configuration() except usb.core.USBError as e: print(f"配置设备出错: {e}") usb.util.claim_interface(device, 0)
3. 关闭占用设备的进程
U盘作为存储设备,资源管理器、文件管理器等进程可能正在占用设备,关闭这些工具后再运行脚本。
pyserial适用性说明
不能用pyserial解决该问题。pyserial是针对串口设备(含USB转串口设备)的通信库,而U盘属于Mass Storage类设备,遵循SCSI协议,与串口协议完全不兼容,pyserial无法和这类设备建立通信。
内容的提问来源于stack exchange,提问作者tristan ledet ledet
相关产品推荐
相关产品推荐

