Hi,
in theory, what is the fastest way of serial communication between OpenMV M7 and a Linux computer? I am about to transfer images to the PC. I have tried the serial port through the pins (UART1 with a USB-serial converter, CP2104) and also through the USB cable with USB_VCP.
I was not able to achieve a faster communication than ca. 512 kbit/sec even though as I understood I should be able to reach max.2Mb/s with the CP2104 and over 12Mb/s with the USB wire.
Of course I have tried with different baudrates. I am pasting my codes below which might help for many people playing with serial communication.
Here is my code on cam:
# Cam serial communication speed test - By: @pvadam - Thu Aug 2 2018
import sensor, image, time
from pyb import LED, UART, USB_VCP
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
led_blue = LED(3)
led_green = LED(2)
usb = USB_VCP()
clock = time.clock()
uart = UART(1)
uart.init(1152000, bits=8, parity=None, stop=1, timeout=1000, flow=0, timeout_char=0, read_buf_len=64)
USE_USB_CONVERTER = True
def send(data):
if USE_USB_CONVERTER:
uart.write(data)
else:
usb.send(data)
i = 0
start_signal = '____start____'
end_signal = '____end____'
while(True):
clock.tick()
i += 1
led_blue.on()
img = sensor.snapshot() # Take a picture and return the image.
img_compressed = img.compress(quality=60)
send(b'{0}save_image{1}'.format(start_signal, end_signal))
send(start_signal)
send(img_compressed)
send(end_signal)
led_blue.off()
The code on the receiver part:
import serial
import io
import sys
import time
device = sys.argv[1]
if device == None:
device = '/dev/ttyUSB0'
ser = serial.Serial(device) # open serial port
ser.baudrate = 1152000
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.timeout = 1 # timeout in seconds
ser.xonxoff = False
ser.inter_byte_timeout = None
print(ser.name) # check which port was really used
ser.flushInput()
ser.flushOutput()
separator_start = '____start____'
separator_end = '____end____'
def read_data(ser, buf=b''):
# Read enough data for a message
while separator_start not in buf:
buf += ser.read(ser.inWaiting())
# Remove the garbage before the message start position
start_pos = buf.find(separator_start)
buf = buf[start_pos:]
cnt = 0
# Wait for the whole message
while not (separator_start in buf and separator_end in buf):
buf += ser.read(ser.inWaiting())
cnt += 1
# Locate message separators
end_pos = buf.find(separator_end)
start_pos = buf.find(separator_start)
# Save the beginning of the next message if any
new_msg = buf[end_pos + len(separator_end):]
# Extract the message
msg = buf[start_pos+len(separator_start):end_pos]
return [msg, new_msg]
i = 0
line = []
msgs = [b'', b'']
running = True
try:
while True:
msgs = read_data(ser, msgs[1])
msg = msgs[0]
if msg == 'save_image':
print('save command')
else:
i += 1
ct = time.localtime()
ct_str = '{0}{1}{2}_{3}{4}{5}'.format(ct.tm_year, ct.tm_mon, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec)
f = open("images/img_{1}_{0}_{2}.jpg".format(i, ser.name.replace('/', '_'), ct_str), 'wb') # open in binary
f.write(msg)
f.close()
print(ct_str)
print('image saved', len(msg))
except:
ser.close() # close port
raise
Thank you,
Peter