尝试从pyusb pip访问图像寄存器

2024-06-28 11:28:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用pyusb访问光学鼠标的图像寄存器。我有一台Linux(ubuntu19.04)机器,运行在Anaconda上,python3.7和pyUsb版本1.0.2。你知道吗

我试过这里的代码: Acquiring Images from a USB mouse (Single Chip, ADNS-2700) via pyusb 因为它抛出了一个资源繁忙错误,所以几乎没有修改,但是现在我收到了一个管道错误。你知道吗

以下是我的代码版本:

import usb.core
import usb.util
import matplotlib.pyplot as plt
import numpy as np

VENDOR_ID = 0x04F2
PRODUCT_ID = 0x0939

# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
                       idProduct=PRODUCT_ID)

interface = 0

if device.is_kernel_driver_active(interface) is True:
    # tell the kernel to detach
    device.detach_kernel_driver(interface)

    # use the first/default configuration
    device.set_configuration()


    # claim the device
    usb.util.claim_interface(device, interface)

# In order to read the pixel bytes, reset PIX_GRAB by sending a write command
response = device.ctrl_transfer(bmRequestType = 0x40, #Write
                                     bRequest = 0x01,
                                     wValue = 0x0000,
                                     wIndex = 0x0D, #PIX_GRAB register value
                                     data_or_wLength = None
                                     )

# Read all the pixels (360 in this chip)
pixList = []

while len(pixList) < 361:
    temp = 0
    response = device.ctrl_transfer(bmRequestType = 0xC0, #Read
                                         bRequest = 0x01,
                                         wValue = 0x0000,
                                         wIndex = 0x0D, #PIX_GRAB register value
                                         data_or_wLength = 1
                                         )
    if response[0] >= 0x80:
        temp = response[0] & 0x7F
        pixList.append(temp)


usb.util.release_interface(device, interface)
# reattach the device to the OS kernel
device.attach_kernel_driver(interface)


pixelArray = np.asarray(pixList)
pixelArray = pixelArray.reshape((19,19))

plt.imshow(pixelArray)
plt.show()

这是我收到的错误信息:

---------------------------------------------------------------------------
USBError                                  Traceback (most recent call last)
<ipython-input-11-ac59c3c49326> in <module>
     29                                      wValue = 0x0000,
     30                                      wIndex = 0x0D, #PIX_GRAB register value
---> 31                                      data_or_wLength = None
     32                                      )
     33 

~/anaconda3/lib/python3.7/site-packages/usb/core.py in ctrl_transfer(self, bmRequestType, bRequest, wValue, wIndex, data_or_wLength, timeout)
   1041                                     wIndex,
   1042                                     buff,
-> 1043                                     self.__get_timeout(timeout))
   1044 
   1045         if isinstance(data_or_wLength, array.array) \

~/anaconda3/lib/python3.7/site-packages/usb/backend/libusb1.py in ctrl_transfer(self, dev_handle, bmRequestType, bRequest, wValue, wIndex, data, timeout)
    881                                         cast(addr, POINTER(c_ubyte)),
    882                                         length,
--> 883                                         timeout))
    884 
    885         return ret

~/anaconda3/lib/python3.7/site-packages/usb/backend/libusb1.py in _check(ret)
    593             raise NotImplementedError(_strerror(ret))
    594         else:
--> 595             raise USBError(_strerror(ret), ret, _libusb_errno[ret])
    596 
    597     return ret

USBError: [Errno 32] Pipe error

我最初认为这可能是由于我的鼠标IC的不同架构造成的,所以我买了一个amazon basics鼠标,因为给定的代码块适用于编写上述链接的第三个响应的人。视频和PID是一样的,但我收到了相同的错误。你知道吗


Tags: ortheinimportiddatadevicetimeout