-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathum24c_gui.py
More file actions
96 lines (88 loc) · 3.08 KB
/
um24c_gui.py
File metadata and controls
96 lines (88 loc) · 3.08 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
96
#!/usr/bin/python3
import tkinter
import serial
import time
import datetime
import sys
import matplotlib
#matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
style.use('ggplot')
serial_port = "/dev/rfcomm1"
try:
rfcomm = serial.Serial(port = serial_port, baudrate = 9600, timeout = 1)
rfcomm.flushInput()
except:
print(serial_port, "not available")
exit(1)
sleeptime = 2.5
top = tkinter.Tk()
frame = tkinter.Frame(top)
frame.pack()
bottomframe = tkinter.Frame(top)
bottomframe.pack(side = tkinter.BOTTOM)
date_gui = str(datetime.datetime.now())
voltage_gui = str("0.000 V")
current_gui = str("0.000 A")
power_gui = str("0.000 W")
temp_gui = str("0 C")
label_port = tkinter.Label(frame, text = serial_port)
label_d = tkinter.Label(frame, text = date_gui, relief = tkinter.RAISED)
label_v = tkinter.Label(frame, text = voltage_gui, relief = tkinter.RAISED)
label_c = tkinter.Label(frame, text = current_gui, relief = tkinter.RAISED)
label_p = tkinter.Label(frame, text = power_gui, relief = tkinter.RAISED)
label_t = tkinter.Label(frame, text = temp_gui, relief = tkinter.RAISED)
label_port.pack(side = tkinter.TOP)
label_d.pack(side = tkinter.TOP)
label_v.pack(side = tkinter.LEFT)
label_c.pack(side = tkinter.LEFT)
label_p.pack(side = tkinter.LEFT)
label_t.pack(side = tkinter.LEFT)
fig = Figure(figsize=(5,4), dpi = 100)
a = fig.add_subplot(111)
a.xaxis.set_tick_params(rotation=30)
canvas = FigureCanvasTkAgg(fig, master = bottomframe)
canvas.draw()
canvas.get_tk_widget().pack(side = 'right', fill = 'both', expand = 1, ipadx = 50, ipady = 50)
dates = []
powers = []
def SerialRun(event = None):
rfcomm.write(bytes([0xf0]))
s = rfcomm.read(130)
if (sys.getsizeof(s) < 130):
top.after(1000, SerialRun)
return
voltage = int((ord(chr(s[2])) <<8)| ord(chr(s[3])))
if (voltage < 2):
top.after(1000, SerialRun)
return
current = int((ord(chr(s[4])) <<8)| ord(chr(s[5])))
power = int((ord(chr(s[6])) <<24)| (ord(chr(s[7])) <<16)| (ord(chr(s[8])) <<8)|ord(chr(s[9])))
tempC = int((ord(chr(s[10])) <<8)| ord(chr(s[11])))
print('"'+str(datetime.datetime.now())+'"'+";"+'"'+str(voltage/100.0)+'"'+";"+'"'+str(current/1000.0)+'"'+";"+'"'+str(power/1000.0)+'"'+";"+'"'+str(tempC)+'"'+';')
date_gui = (str(datetime.datetime.now()))
voltage_gui = (str(voltage/100.0))
current_gui = (str(current/1000.0))
power_gui = (str(power/1000.0))
temp_gui = (str(tempC))
dates.append(datetime.datetime.now())
powers.append(power)
a.clear()
a.xaxis.set_tick_params(rotation=30)
a.margins(0.5)
a.plot_date(dates, powers)
canvas.draw()
label_d.config(text = date_gui)
label_v.config(text = voltage_gui+" V")
label_c.config(text = current_gui+" A")
label_p.config(text = power_gui+" W")
label_t.config(text = temp_gui+" C")
sys.stdout.flush()
top.update()
top.after(5000, SerialRun)
top.after(5000, SerialRun)
top.mainloop()
rfcomm.close()