我想提取的实际图像由一个单片光学鼠标传感器,特别是ADNS-2700。针对互联网上使用微控制器与成像芯片(像这样)的SPI接口进行通信的各种教程,我试图使用的芯片集成了一个USB接口。
ADNS-2700数据表
系统: Windows 7,Python2.7,PyUSB 1.0
我已经成功地提取了按下按钮、速度和滚动轮跟随这个例子。
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()发布于 2014-04-23 00:26:49
您可能需要执行ctrl_transfer(),如pyUSB教程所示。
您还需要将数据表中的十六进制字节转换为单独的参数到ctrl_transfer。有关格式,请参见此页。
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]发布于 2018-06-22 16:34:36
我只是亲自尝试了这段代码,其中有一个相当大的错误。ADNS-2700的数据表声明没有最重要位集的响应值无效。因此,循环不应该执行完全361读取,而是应该读取直到它获得361有效像素,丢弃没有“有效”位设置的任何字节。
我还做了其他一些小的更改,比如在将返回的数组添加到列表之前对其进行解压缩,以及从返回值中删除MSb,因为它并不代表实际的像素数据。
我使用鼠标测试了这段代码,它输出了一个有效的图像。
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)
# use the first/default configuration
device.set_configuration()
# 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)
pixelArray = np.asarray(pixList)
pixelArray = pixelArray.reshape((19,19))
plt.imshow(pixelArray)
plt.show()https://stackoverflow.com/questions/23229083
复制相似问题