-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOxy_live.py
More file actions
95 lines (69 loc) · 2.25 KB
/
Oxy_live.py
File metadata and controls
95 lines (69 loc) · 2.25 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import serial
reply = ''
# Begin Serial coms
ser= serial.Serial(
port='/dev/ttyUSB0', #serial port the object should read
baudrate= 19200, #rate at which information is transfered over comm channel
parity=serial.PARITY_NONE, #no parity checking
stopbits=serial.STOPBITS_ONE, #pattern of bits to expect which indicates the end of a character
bytesize=serial.EIGHTBITS, #number of data bits
timeout=1
)
# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []
y2 = []
# Begin H2M data mode
ser.write(b'mode0001\r')
timestr = str(time.strftime("%Y%m-%d_%H-%M-%S"))
# File location is in same directory as code, change as needed
file_name = "./OXYBASE" + timestr
file = open(file_name,"a+")
file.write(file_name + "\r\n")
# Time strings must incriment with data read cycle and be unique or else data will not plot
start_time = dt.datetime.now().strftime('%H:%M:%S.%f')
# This function is called periodically from FuncAnimation
def animate(i, xs, ys, y2):
ser.flushInput()
ser.flushOutput()
# Read new data
ser.write(b'data\r')
reply = ser.read_until('\r')
reply = '\n' + reply
file.write(reply)
#print(reply)
# Parse Message for graph
reply = reply.split(';',5)
temp = reply[3]
temp = temp.replace('T','')
oxy = reply[4]
oxy = oxy.replace('O','')
temp = float(temp)/100
oxy = float(oxy)/1000
print('O2%: {0:.3f}'.format(oxy))
print('Temp: {0:.2f}'.format(temp))
# Add x and y to lists
xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
ys.append(oxy)
y2.append(temp)
# Limit x and y lists to 200 items (This is about the sweet spot without overloading my laptop)
xs = xs[-200:]
ys = ys[-200:]
y2 = y2[-200:]
# Draw x and y lists
ax.clear()
ax.plot(xs, ys, y2)
# Format plot
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title('Oxybase Dissolved O2 (Start:' + start_time + ')')
plt.ylabel('O2 (%)')
# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys, y2), interval=500)
plt.show()