Capture all data from raw serial port

I am using Python script to record data from a gyroscope. Arduino-compatible micro-controller collects the data and sends it via a serial connection to the PC. Here is the simplified Arduino and Python code:


void sendToPC(float* data1, float* data2, float* data3) {
  byte* byteData1 = (byte*)(data1);
  byte* byteData2 = (byte*)(data2);
  byte* byteData3 = (byte*)(data3);
  byte buf[12] = {
    byteData1[0], byteData1[1], byteData1[2], byteData1[3],
    byteData2[0], byteData2[1], byteData2[2], byteData2[3],
    byteData3[0], byteData3[1], byteData3[2], byteData3[3]
  Serial.write(buf, 12);

void setup() {

void loop() {
  sendToPC(&val1, &val2, &val3);


#!/usr/bin/env python
import serial
import time
import struct
import pandas as pd

serialPort = 'COM3'
serialBaud = 921600
dataNumBytes = 4
numData = 3
data = [0.0, 0.0, 0.0]
rawData = bytearray(numData * dataNumBytes)
bufferLength = 10
csvBuffer = []

# Connect to serial port
print('Trying to connect to ' + str(serialPort) +
      ' at ' + str(serialBaud) + ' BAUD.')
    s = serial.Serial(serialPort, serialBaud, timeout=4)
    print("Failed to connect with " + str(serialPort) +
          ' at ' + str(serialBaud) + ' BAUD.')

s.reset_input_buffer()  # flush input buffer
    # read n bytes into array

    for i in range(numData):
        bytedata = rawData[(i * dataNumBytes):(dataNumBytes + i * dataNumBytes)]
        value, = struct.unpack('f', bytedata)
        data[i] = value
    csvBuffer.append([data[0], data[1], data[2]])

# record "csvBuffer" into HDF5 file.

Data rate is 300 samples per second and size of the sample is 3x4Bytes floating point values.

What is missing:

  1. Synchronisation between Python script and the serial data coming from Arduino device. When I suddenly start the Python script sometimes it starts in the middle of data stream. It would be nice to make it detect the beginning of the 16 byte sample.
  2. It does not capture everything and sometimes skips the samples.
  3. Many other things which I probably do not know.

What I want to achieve:
My goal is to create a Python logger which can fully capture the serial data from the sensor in any circumstances. Please let me know your tips and advise me how to do it.


1 Like