我正在尝试使用pyusb访问光学鼠标的图像寄存器。我有一台在Linux上使用Python 3.7和pyUsb版本1.0.2的Linux(Ubuntu 19.04)计算机。
我尝试了此处给出的代码: 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鼠标,因为给定的代码块正对编写上述链接的第三个响应的人起作用。 VID和PID相同,但收到相同的错误。