I'm working on a project in which a continuous stream of data is coming on the serial port through Arduino in which I have to extract my required information. I have include "AA" in the beginning of my required data and "55" at the ending.
Arduino Serial Monitor output -
AA055
AA155
AA255
AA355
AA455
Arduino code-
int i=0;
void setup()
{
Serial.begin(9600); // Open serial connection at a baud rate of 9600
}
void loop()
{
Serial.print("AA");
Serial.print(i);
Serial.println("55");
i++;
delay(100);
}
PYTHON CODE
import serial
import time
import csv
baudrate = 9600
port = 'COM2' # set the correct port before run it
serial = serial.Serial(port=port, baudrate=baudrate)
serial.flushInput()
with open("test_data.csv","a") as f:
writer = csv.writer(f,delimiter=",")
writer.writerow(["TIME STAMP", "DATA"])
while True:
try:
ser_bytes = serial.readline()
decoded_bytes = float(ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
print(decoded_bytes)
aq = time.strftime('%a %H:%M:%S')
print(aq)
with open("test_data.csv","a") as f:
writer = csv.writer(f,delimiter=",")
writer.writerow([time.strftime('%a %H:%M:%S'),decoded_bytes])
except:
print("Keyboard Interrupt")
break
I want the python code to extract my information by detecting the "AA" at the beginning and "55" at the end.