我想提取由单芯片光学鼠标传感器(特别是 ADNS-2700)捕获的实际图像。与互联网上使用微 Controller 与成像芯片( like this )的 SPI 接口(interface)通信的各种其他教程相比,我尝试使用的芯片集成了 USB 接口(interface)。
ADNS-2700 Datasheet
系统:Windows 7、Python2.7、PyUSB 1.0
我已经成功提取了 this example 之后的按钮按下、速度和滚轮:
import usb.core
import usb.util
VENDOR_ID = 6447
PRODUCT_ID = 2326
# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
idProduct=PRODUCT_ID)
# use the first/default configuration
device.set_configuration()
# first endpoint
endpoint = device[0][(0,0)][0]
# read a data packet
attempts = 10
data = None
while attempts > 0:
try:
data = device.read(endpoint.bEndpointAddress,
endpoint.wMaxPacketSize)
print data
except usb.core.USBError as e:
data = None
if e.args == ('Operation timed out',):
attempts -= 1
continue
提取数据如下:
array('B', [0, 0, 16, 0, 0])
array('B', [0, 0, 240, 255, 0])
array('B', [0, 0, 16, 0, 0])
array('B', [0, 0, 240, 255, 0])
我需要帮助提取原始图像数据!
我是一个 USB 菜鸟,这可能是造成大部分问题的原因。
在数据表的第 18 页上,有一个 USB 命令列表。看起来很有希望的一个是:
Mnemonic Command Notes
---------------------------------------------------------------
Get_Vendor_Test C0 01 00 00 xx 00 01 00 Read register xx
然后在第 28 页,有一个看起来很有希望的寄存器列表:
Address Register Name Register Type Access Reset Value
----------------------------------------------------------------------
0x0D PIX_GRAB Device Read only 0x00
但是,我尝试过:
device.write(endpoint.bEndpointAddress,'C0:01:00:00:0A:00:01:00',0)
结果是:
usb.core.USBError: [Errno None] libusb0-dll:err [_usb_setup_async] invalid endpoint 0x81
也:
device.read(endpoint.bEndpointAddress, 0x0D)
这只是超时。
完整解决方案:
import usb.core
import usb.util
import matplotlib.pyplot as plt
import numpy as np
VENDOR_ID = 6447
PRODUCT_ID = 2326
# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
idProduct=PRODUCT_ID)
# use the first/default configuration
device.set_configuration()
# In order to read the pixel bytes, reset PIX_GRAB by sending a write command
response = self.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 = []
for i in range(361):
response = self.device.ctrl_transfer(bmRequestType = 0xC0, #Read
bRequest = 0x01,
wValue = 0x0000,
wIndex = 0x0D, #PIX_GRAB register value
data_or_wLength = 1
)
pixList.append(response)
pixelArray = np.asarray(pixList)
pixelArray = pixelArray.reshape((19,19))
plt.imshow(pixelArray)
plt.show()
最佳答案
您可能需要执行 ctrl_transfer() ,如 pyUSB tutorial 所示。
您还需要将数据表中的十六进制字节转换为 ctrl_transfer 的单个参数。有关格式,请参阅 this page。Get_Vendor_Test
C0 01 00 00 xx 00 01 00
可以通过 ctrl_transfer() 调用发出,如下所示:
ret = dev.ctrl_transfer(bmRequestType=0xc0, # byte[0]
bRequest=0x01, # byte[1]
wValue=0x0000, # byte[2,3]
wIndex=register, # byte[4,5]
data_or_wLength = 1)# byte[6,7]
关于python - 通过 pyusb 从 USB 鼠标(单芯片,ADNS-2700)获取图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23229083/