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:
Arduino:
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() {
Serial.begin(921600);
}
void loop() {
sendToPC(&val1, &val2, &val3);
}
Python:
#!/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.')
try:
s = serial.Serial(serialPort, serialBaud, timeout=4)
print('Connected!')
except:
print("Failed to connect with " + str(serialPort) +
' at ' + str(serialBaud) + ' BAUD.')
s.reset_input_buffer() # flush input buffer
while(True):
#time.sleep(0.2)
# read n bytes into array
s.readinto(rawData)
for i in range(numData):
bytedata = rawData[(i * dataNumBytes):(dataNumBytes + i * dataNumBytes)]
value, = struct.unpack('f', bytedata)
data[i] = value
print(data)
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:
- 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.
- It does not capture everything and sometimes skips the samples.
- 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.
Thanks!