Repository navigation
Expand file tree
/
Copy pathvoltage_chart.py
More file actions
143 lines (116 loc) · 4.66 KB
/
Copy pathvoltage_chart.py
File metadata and controls
143 lines (116 loc) · 4.66 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import serial
import pathlib
import PySimpleGUI as sg
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Initialize plot
def livePlot():
"""_summary_
"""
global xs, ys, ax, fig, ani
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []
ani = animation.FuncAnimation(fig, update, interval=100)
fig.show()
def connect(serialPort, baudRate):
"""_summary_
Args:
serialPort (_type_): _description_
baudRate (_type_): _description_
"""
# Initialize serial connection
global ser
try:
ser = serial.Serial(serialPort, baudRate)
if not ser.isOpen():
ser.open()
window["statustext"].update("Connected")
window["connect"].update(disabled=True)
window["disconnect"].update(disabled=False)
window["chart"].update(disabled=False)
else:
window["statustext"].update("Connected")
window["connect"].update(disabled=True)
window["disconnect"].update(disabled=False)
window["chart"].update(disabled=False)
except serial.SerialException:
sg.Popup("Error connecting to Arduino. Please check your serial port settings.")
exit()
def createCSV(filename):
"""_summary_
Args:
filename (_type_): _description_
"""
global file
with open(filename, 'w') as file:
file.write("Time,Voltage\n")
for i, y in zip(xs, ys):
file.write(f"{i},{y}\n")
def update(i):
"""_summary_
Args:
i (_type_): _description_
"""
try:
line = ser.readline().decode('utf-8').strip() # Read a line from the serial port
voltage = float(line) # Convert the line to a float
xs.append(i)
ys.append(voltage)
ax.clear()
ax.plot(xs, ys)
if values["createCSV"]:
createCSV(filelocation)
except ValueError:
pass
except serial.serialutil.PortNotOpenError:
sg.popup("Port closed")
plt.close()
window.close()
#*****GUI*****
#------------------------------------------------------------------------------------------------------------------------
sg.theme("DarkBlue5")
layout = [[sg.Text("Filename(without .csv):", key="filenametext"), sg.Input(key="filename")],
[sg.Text("Enter filepath for CSV: "), sg.Input(key="filepath", default_text=os.path.join(str(pathlib.Path.home()), "Desktop")), sg.FolderBrowse(key="filebrowse")],
[sg.Text("Enter device location: ", key="devicepathtext"), sg.Input(key="devicepathinput", default_text="/dev/cu.usbmodem14101")],
[sg.Text("Enter baud rate: ", key="baudratetext"), sg.Input(key="baudrateinput", default_text="9600")],
[sg.Push(), sg.Text("", key="statustext"), sg.Push()],
[sg.Checkbox("Create CSV file", key="createCSV", default=True, enable_events=True)],
[sg.Button("Connect", key="connect"), sg.Button("Disconnect", key="disconnect", disabled=True), sg.Push(), sg.Button("Chart", key="chart", disabled=True)]]
window = sg.Window("CFD Project (github.com/Edzemundo)", layout=layout, size=(555,210))
while True:
event, values = window.read()
filelocation = os.path.join(values["filepath"], values["filename"] + ".csv")
if event == "createCSV":
if values["createCSV"]:
window["filenametext"].update(visible=True)
window["filename"].update(visible=True)
else:
window["filenametext"].update(visible=False)
window["filename"].update(visible=False)
if event == "connect":
print(filelocation)
if values["createCSV"]:
try:
open(filelocation, "w")
connect(values["devicepathinput"], int(values["baudrateinput"]))
print(values["devicepathinput"], values["baudrateinput"])
livePlot()
except (FileNotFoundError, IsADirectoryError, OSError):
window["statustext"].update("Filename not/improperly entered or path not/improperly entered")
elif not values["createCSV"]:
connect(values["devicepathinput"], int(values["baudrateinput"]))
print(values["devicepathinput"], values["baudrateinput"])
livePlot()
if event == "disconnect":
window["statustext"].update("Disconnected")
window["connect"].update(disabled=False)
window["disconnect"].update(disabled=True)
plt.close()
ser.close()
if event == "chart":
fig.show()
if event == sg.WIN_CLOSED:
break