编程语言
首页 > 编程语言> > python-通过pyusb从USB鼠标(单芯片,ADNS-2700)获取图像

python-通过pyusb从USB鼠标(单芯片,ADNS-2700)获取图像

作者:互联网

我想提取单芯片光学鼠标传感器(特别是ADNS-2700)捕获的实际图像.正如互联网上使用微控制器与成像芯片(like this)的SPI接口进行通讯的其他教程相适应的那样,我尝试使用的芯片集成了USB接口.

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.

可以通过ctrl_transfer()调用来发出Get_Vendor_Test C0 01 00 00 xx 00 01 00,如下所示:

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]

标签:libusb,usb,pyusb,python
来源: https://codeday.me/bug/20191121/2054743.html