-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeart readings display.py
More file actions
37 lines (34 loc) · 1.13 KB
/
Copy pathHeart readings display.py
File metadata and controls
37 lines (34 loc) · 1.13 KB
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
import serial
import time
import csv
import matplotlib
matplotlib.use("tkAgg")
import matplotlib.pyplot as plt
import numpy as np
ser = serial.Serial(port='COM3', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=2)
ser.flushInput()
plot_window = 200
y_var = np.array(np.zeros([plot_window]))
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot(y_var)
if ser.isOpen():
try:
while 1:
ser_bytes = ser.readline()
decoded_bytes = float(ser_bytes[0:len(ser_bytes) - 2].decode("utf-8"))
print(decoded_bytes)
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 Exception:
print("error")
else:
print("can't connect to serial")