forked from pulasthieka/fyp-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialplotter.py
40 lines (36 loc) · 981 Bytes
/
serialplotter.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
40
import numpy as np
import matplotlib.pyplot as plt
import serial
import time
import csv
import matplotlib
matplotlib.use("tkAgg")
ser = serial.Serial('/dev/ttyACM0')
ser.flushInput()
plot_window = 20
y_var = np.array(np.zeros([plot_window]))
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot(y_var)
while True:
try:
ser_bytes = ser.readline()
try:
decoded_bytes = float(
ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
print(decoded_bytes)
except:
continue
with open("test_data.csv", "a") as f:
writer = csv.writer(f, delimiter=",")
writer.writerow([time.time(), decoded_bytes])
y_var = np.append(y_var, decoded_bytes)
y_var = y_var[1:plot_window+1]
line.set_ydata(y_var)
ax.relim()
ax.autoscale_view()
fig.canvas.draw()
fig.canvas.flush_events()
except:
print("Keyboard Interrupt")
break