-
Notifications
You must be signed in to change notification settings - Fork 8
/
serial_plot.py
55 lines (50 loc) · 1.8 KB
/
serial_plot.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Plot in real-time the gamma radiation level being logged to the serial
port. Because it's way more fun than reading a terminal.
You will need Python with the matplotlib and pyserial packages installed on
your box.
Configure the serial port to which the Arduino is connected. By default,
we select the first port, but that may not be the good one."""
import serial
import matplotlib.pyplot as plt
import sys
# Edit the port, specifying either a name, or a number.
port = 0
# port = '/dev/ttyACM0'
if len(sys.argv) > 1:
port = sys.argv[1]
fig = plt.figure()
axes = fig.add_subplot(111)
times = []
bottomError = []
radiation = []
topError = []
bottomLine, = axes.plot(times, bottomError, color='green', label=u'Error (-)')
radiationLine, = axes.plot(times, radiation, color='blue', label=u'Radiation dose')
topLine, = axes.plot(times, topError, color='green', label=u'Error (+)')
plt.ion()
plt.legend(loc='best')
plt.xlabel(u'Time (min)')
plt.ylabel(u'Dose (μSv/h)')
plt.title(u'Gamma rays (γ)')
plt.show()
with serial.Serial(port, 9600) as ser:
print('Listening to %s' % (ser.name))
while 1:
line = ser.readline()[:-1].decode()
print(line)
(time, count, cpm, uSvh, uSvhError) = line.split(',')
if time == 'time(ms)':
continue
times.append(float(time) / 1000 / 60)
bottomError.append(float(uSvh) - float(uSvhError))
radiation.append(float(uSvh))
topError.append(float(uSvh) + float(uSvhError))
bottomLine.set_data(times, bottomError)
radiationLine.set_data(times, radiation)
topLine.set_data(times, topError)
axes.relim()
axes.autoscale_view(True,True,True)
plt.draw()
fig.canvas.flush_events()