Example: 50-Arduino-Boards/Portenta-H7/50-Board-Control/usb_vcp.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# USB VCP示例。
# 此示例展示了如何使用USB VCP类按需将图像发送到PC。
#
# 警告:
# 此脚本不应从IDE或命令行运行,应保存为main.py
# 注意以下注释的脚本展示了如何从主机端接收图像。
#
# #!/usr/bin/env python2.7
# 导入 sys, serial, struct
# port = '/dev/ttyACM0'
# 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) # dsrdtr 在 Windows 上被忽略。
# sp.write("snap")
# sp.flush()
# size = struct.unpack('<L', sp.read(4))[0]
# img = sp.read(size)
# sp.close()
#
# with open("img.jpg", "w") as f:
# f.write(img)
import sensor
import struct
from pyb import USB_VCP
usb = USB_VCP()
sensor.reset() # 重置并初始化传感器。
sensor.set_pixformat(sensor.GRAYSCALE) # 将像素格式设置为RGB565 (or 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().to_jpeg()
usb.send(struct.pack("<L", img.size()))
usb.send(img)