Explication de routine-02-usb_vcp port série virtuel USB
# USB VCP 示例.
# 此示例显示如何使用USB VCP类按需将图像发送到PC。
#
# 警告:
# 不应该从IDE或命令行运行此脚本,它应保存为main.py.
# 注意以下注释脚本显示了如何从主机端接收图像。
#
# #!/usr/bin/env python2.7
# import 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, 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)