使用 Python 从 USB 设备读取数据时遇到问题

发布于 2025-01-19 21:25:27 字数 2158 浏览 5 评论 0原文

我正在使用 PyUSB 库从 Windows 中的 USB 鼠标读取数据。我能够在 UBUNTU 虚拟机中运行代码,但无法在 Windows 中复制相同的代码。有人可以告诉我我做错了什么吗?以下是代码及其引发的错误:

import usb.core
import usb.util

# decimal vendor and product values
dev = usb.core.find(idVendor=1133, idProduct=49278)
if dev is None :
    raise ValueError('Device is not found')

print(dev)

# first endpoint
interface = 0
endpoint = dev[0][(0,0)][0]
# if the OS kernel already claimed the device, which is most likely true

if dev.is_kernel_driver_active(interface) is True:
  # tell the kernel to detach
  dev.detach_kernel_driver(interface)
  # claim the device
  usb.util.claim_interface(dev, interface)
collected = 0
attempts = 50
while collected < attempts :
    try:
        data = dev.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)
        collected += 1
        print(data)
    except usb.core.USBError as e:
        data = None
        if e.args == ('Operation timed out',):
            continue
# release the device
usb.util.release_interface(dev, interface)
# reattach the device to the OS kernel
dev.attach_kernel_driver(interface)
Traceback (most recent call last):
  File "c:\BCD\i360\python\usb_reader.py", line 27, in <module>
    if dev.is_kernel_driver_active(interface) is True:
  File "C:\Python\Python38\lib\site-packages\usb\core.py", line 1107, in is_kernel_driver_active
    self._ctx.managed_open()
  File "C:\Python\Python38\lib\site-packages\usb\core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Python\Python38\lib\site-packages\usb\core.py", line 131, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "C:\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 804, in open_device
    return _DeviceHandle(dev)
  File "C:\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 652, in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "C:\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 600, in _check
    raise NotImplementedError(_strerror(ret))
NotImplementedError: Operation not supported or unimplemented on this platform

I am using PyUSB library to read data from my USB mouse in windows. I was able to run the code in a UBUNTU virtual machine, but I can't replicate the same in windows. Can someone tell me what am I doing wrong? Following is the code and the error it throws :

import usb.core
import usb.util

# decimal vendor and product values
dev = usb.core.find(idVendor=1133, idProduct=49278)
if dev is None :
    raise ValueError('Device is not found')

print(dev)

# first endpoint
interface = 0
endpoint = dev[0][(0,0)][0]
# if the OS kernel already claimed the device, which is most likely true

if dev.is_kernel_driver_active(interface) is True:
  # tell the kernel to detach
  dev.detach_kernel_driver(interface)
  # claim the device
  usb.util.claim_interface(dev, interface)
collected = 0
attempts = 50
while collected < attempts :
    try:
        data = dev.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)
        collected += 1
        print(data)
    except usb.core.USBError as e:
        data = None
        if e.args == ('Operation timed out',):
            continue
# release the device
usb.util.release_interface(dev, interface)
# reattach the device to the OS kernel
dev.attach_kernel_driver(interface)
Traceback (most recent call last):
  File "c:\BCD\i360\python\usb_reader.py", line 27, in <module>
    if dev.is_kernel_driver_active(interface) is True:
  File "C:\Python\Python38\lib\site-packages\usb\core.py", line 1107, in is_kernel_driver_active
    self._ctx.managed_open()
  File "C:\Python\Python38\lib\site-packages\usb\core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Python\Python38\lib\site-packages\usb\core.py", line 131, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "C:\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 804, in open_device
    return _DeviceHandle(dev)
  File "C:\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 652, in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "C:\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 600, in _check
    raise NotImplementedError(_strerror(ret))
NotImplementedError: Operation not supported or unimplemented on this platform

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷了相思 2025-01-26 21:25:28

错误说明了问题所在:您使用的是在操作系统(Windows)上没有意义的函数。

所以,不要那样吗?要么,如果您知道这仅在Windows上运行,请删除检查,或者更好,尝试: - 处理异常。

The error says exactly what's wrong: You're using a function that makes no sense on your operating system (Windows).

So, don't do that? Either, if you know this is something only running on Windows, remove the check, or better, try:-handle the exception.

疧_╮線 2025-01-26 21:25:28

Windows 不支持 dev.is_kernel_driver_active(interface)

dev.is_kernel_driver_active(interface) is not supported in Windows

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文