我试图读取 USB 设备(在本例中为 USB 闪存驱动器)的输入。所以我开始使用这段代码:
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)
我确实获得了设备句柄。但之后会出现一个
NotImplementedError
。 (如果我使用device.get_active_configuratio()
也会发生同样的情况):
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 不在路径中时可能会出现错误,但就我而言,我已经有了它。我想知道如何修复这个错误。我还读到解决方案可能是使用 Zoldig 驱动程序,但我想避免这种解决方案。 Pyserial 可以吗?
这似乎是
pyusb
的一个常见问题,已在 GitHub 上多次提出,例如 https://github.com/pyusb/pyusb/issues/375 和 https://github.com/pyusb/ pyusb/issues/418,此错误的可能原因是安装的驱动程序与 libusb
不兼容,因此您需要安装 libusb 兼容的驱动程序,请参阅如何在 Windows 上使用 libusb。
另外查看 PyPi,看来
pyusb
最适合 Python 版本 3.6 - 3.9。