logo

使用usb虚拟串口传输数据的问题

2020-09-23 17:19
L
oh41

使用例程中的传输代码

import sensor, image, time, ustruct
from pyb import USB_VCP

usb = USB_VCP()
sensor.reset()                      # 复位并初始化感光元件。
sensor.set_pixformat(sensor.RGB565) # 设置像素格式为RGB565(或GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # 将图像大小设置为QVGA (320x240)
sensor.skip_frames(time = 2000)     # 等待设置生效。

while(True):
    cmd = usb.recv(4, timeout=5000)
    if (cmd == b'snap'):
        img = sensor.snapshot().compress()
        usb.send(ustruct.pack("<L", img.size()))
        usb.send(img)

以及接收代码

#!/usr/bin/env python2.7
import sys, serial, struct
port = 'COM5'
sp = serial.Serial(port, baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=True)
sp.setDTR(True)
sp.write("snap")
sp.flush()
size = struct.unpack('<L', sp.read(4))[0]
img = sp.read(size)
sp.close()

with open("test.jpg", "w") as f:
    f.write(img)

一开始接收速度很慢

而且接收的图片有问题

4c16bc3c-b8eb-4be2-8910-9c3d45c21684.jpg

比赛要用,希望能快点解决

2020-09-23 18:12
I
网红小智智 星瞳科技

https://book.openmv.cc/example/02-Board-Control/usb-vcp.html

首先把代码保存到OpenMV里,脱机运行的步骤:https://singtown.com/learn/49976/

然后插入USB,不要连接IDE,

然后python2中运行第二段程序。

很快就保存下来了。

a212c11b-dc8c-427b-8b64-68eccc11702e.jpg
2020-09-23 19:10
L
oh41
54f53c90-1179-4e88-af24-ddf7af2812a9.png

我保存的图片变成这样了

L
oh41

@kidswong999 win10

2020-09-24 18:56
I
网红小智智 星瞳科技

win10得过几天测试,现在手边没有win电脑。

2020-09-24 20:45
L
oh41

@kidswong999 可能是传输时序或者编码方面有问题

2020-10-04 18:13
D
bgfp

串口发送的python是3.0版本,串口接收的python也必须是3.0版本.用python2接受openMV用python3发送的图片就花

2020-10-05 08:10
D
bgfp

@bgfp 更正: 将with open("img.jpg", "w") as f:

改为with open("img.jpg", "wb") as f:

python2和python3均可正常保存图片,只不过对于python3,ser.write("snap\r\n")要改成ser.write("snap\r\n".encode())

2021-04-15 10:19
N
ekgz

想请教一下这个例程里面发送图片使用的方式算是用了虚拟COM口用UART发的呢还是直接USB的方式发的呢?小白搞不太懂

2021-04-15 13:12
I
网红小智智 星瞳科技

与UART没有任何关系,OpenMV的板上也没有uart转usb芯片。

OpenMV使用的硬件USB,协议是虚拟串口,也叫VCP。