-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm_logger.py
39 lines (37 loc) · 1.18 KB
/
stm_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import time
import serial
import datetime
import pandas as pd
PORT = '/dev/ttyACM1'
BAUDRATE = 9600
print('starting')
serial_device = serial.Serial(PORT, baudrate=BAUDRATE, timeout=10)
time.sleep(5)
data = pd.read_csv('data.csv', index_col=False)
appended_rows = 0
measurements = 0
fails = 0
print('running')
while True:
if serial_device.in_waiting:
try:
time.sleep(0.05)
measurement = serial_device.readline()
measurement = measurement[0:len(measurement) - 2].decode("utf-8")
# print(measurement)
measurement = measurement.split(',')
index = len(data)
data = data.append(pd.Series(dtype=int), ignore_index=True)
for i, value in enumerate(measurement):
data.iat[index, i] = int(value)
data.at[index, 'time'] = datetime.datetime.now()
appended_rows += 1
measurements += 1
if appended_rows > 100:
data.to_csv('data.csv', index=False)
appended_rows = 0
print('measurements:\t', measurements, '\tfails:\t', fails, end='\r')
except:
fails += 1
else:
time.sleep(0.5)