-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_gui.py
272 lines (216 loc) · 9.73 KB
/
main_gui.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from PyQt5.QtWidgets import (QLabel, QVBoxLayout, QHBoxLayout, QApplication,
QComboBox, QMainWindow, QWidget, QTextEdit,
QPushButton, QAction, QLineEdit, QShortcut)
from PyQt5.QtCore import Qt, QThread, QObject, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QKeySequence
from src import single, setup, file_data
from serial import Serial, SerialException
from time import ctime, time, localtime
import sys
class Communicator(QObject):
finished = pyqtSignal() # give worker class a finished signal
out_signal = pyqtSignal(str) # Need to overload with type of data to be passed
def __init__(self, parent=None):
QObject.__init__(self, parent=parent)
self.continue_run = True # provide a bool run condition for the class
self.out_signal[str].connect(main_window.main_widget.update_textbox) # Need details of variable to be sent
def do_work(self):
i = 0
while self.continue_run:
i += 1
# noinspection PyCallByClass
QThread.sleep(1)
self.out_signal[str].emit(str(i)) # and need details here
self.finished.emit() # emit the finished signal when the loop is done
def stop(self):
self.continue_run = False
class Autosave(QObject):
finished = pyqtSignal()
out_signal = pyqtSignal()
def __init__(self, cycle, parent=None):
QObject.__init__(self, parent=parent)
self.out_signal.connect(main_window.main_widget.autosave)
self.cycle = cycle
def do_work(self):
timer = time()
while True:
QThread.sleep(30)
if time() - timer >= self.cycle:
self.out_signal.emit()
timer = time()
def stop(self):
self.continue_run = False
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
# creating roman widget and setting it as central
self.main_widget = Window(parent=self)
self.setCentralWidget(self.main_widget)
self.setup_menu_bar()
self.setWindowTitle("Serial Monitor")
def setup_menu_bar(self):
# filling up a menu bar
bar = self.menuBar()
# adding actions for file menu
open_action = QAction('Open', self)
preferences_action = QAction('Preferences', self)
close_action = QAction('Close', self)
# Setup shortcut for exit
close_shortcut = QShortcut(QKeySequence("ctrl+C"), self)
close_shortcut.activated.connect(self.close)
# File menu
file_menu = bar.addMenu('File')
file_menu.addAction(open_action)
file_menu.addAction(preferences_action)
file_menu.addAction(close_action)
help_action = QAction('Help', self)
help_menu = bar.addMenu("Help")
help_menu.addAction(help_action)
# use `connect` method to bind signals to desired behavior
close_action.triggered.connect(self.close)
# noinspection PyArgumentList,PyArgumentList
class Window(QWidget):
stop_signal = pyqtSignal()
stop_autosave_signal = pyqtSignal()
# noinspection PyArgumentList
def __init__(self, parent):
super(Window, self).__init__(parent=parent)
self.setup_ui()
self.started = False
self.started_autosave = False
def setup_ui(self):
self.first_label = QLabel('Device COM port')
self.combo_first = QComboBox()
self.second_label = QLabel('Computer COM port')
self.combo_second = QComboBox()
self.header_label = QLabel('Header end byte')
self.header_entry = QLineEdit()
self.body_label = QLabel('Body end byte')
self.body_entry = QLineEdit()
self.start_stop_button = QPushButton("Start Communication")
self.save_button = QPushButton("Save commands")
self.clear_button = QPushButton("Clear")
# Creates a scrollable, selectable text field that cannot be edited by the user
self.output_box = QTextEdit()
self.output_box.setReadOnly(True)
self.output_box.setTextInteractionFlags(Qt.TextSelectableByKeyboard | Qt.TextSelectableByMouse)
# Get list of available ports
ports = setup.find_ports()
ports.insert(0, " ")
self.combo_first.addItems(ports)
self.combo_second.addItems(ports)
# Initialise Vertical layout box for Combo boxes labels
v_box_label = QVBoxLayout()
v_box_label.addWidget(self.first_label)
v_box_label.addWidget(self.second_label)
# Initialise Vertical layout box for Combo boxes controlling ports
v_box_combo = QVBoxLayout()
v_box_combo.addWidget(self.combo_first)
v_box_combo.addWidget(self.combo_second)
v_box_label_second = QVBoxLayout()
v_box_label_second.addWidget(self.header_label)
v_box_label_second.addWidget(self.body_label)
v_box_header_body = QVBoxLayout()
v_box_header_body.addWidget(self.header_entry)
v_box_header_body.addWidget(self.body_entry)
# Initialise Horizontal layout box for Combo boxes and labels
h_box_options = QHBoxLayout()
h_box_options.addLayout(v_box_label)
h_box_options.addLayout(v_box_combo)
h_box_options.addLayout(v_box_label_second)
h_box_options.addLayout(v_box_header_body)
# Initialise a Horizontal box for command buttons
h_box_buttons = QHBoxLayout()
h_box_buttons.addWidget(self.start_stop_button)
h_box_buttons.addWidget(self.save_button)
h_box_buttons.addWidget(self.clear_button)
h_box_buttons.addStretch()
v_box_upper_left = QVBoxLayout()
v_box_upper_left.addLayout(h_box_options)
v_box_upper_left.addLayout(h_box_buttons)
# Initialise Vertical layout box for window
v_box_master = QVBoxLayout()
v_box_master.addLayout(v_box_upper_left)
v_box_master.addWidget(self.output_box)
# Start/stop button action
self.start_stop_button.clicked.connect(self.start_stop)
# Save Button action
self.save_button.clicked.connect(self.save_textbox)
# Clear button action
self.clear_button.clicked.connect(self.output_box.clear)
self.setLayout(v_box_master)
self.show()
def start_stop(self):
"""
Controls interactions off the start/stop button. Changes appearance
of button and initiates a new thread for operation and clears the output.
:return:
"""
if not self.started:
self.output_box.clear() # Clears the textbox so its easier to read
self.setup_communication_thread() # This method handles the messy part of setting up
self.start_communication_thread()
self.started = True
self.start_stop_button.setText("Stop Communication")
else:
self.stop_communication_thread()
self.started = False
self.start_stop_button.setText("Start Communication")
def setup_communication_thread(self):
# Create the instances of thread and worker
self.thread_communication = QThread()
self.worker_communication = Communicator()
self.stop_signal.connect(self.worker_communication.stop)
self.setup_thread(self.worker_communication, self.thread_communication)
def setup_autosave_thread(self):
"""
Sets up the autosave thread. Must be initialised after main_window
otherwise the poor darling gets confused (due to referencing in the
__init__ for autosave).
"""
# Create the instance of thread and worker
self.thread_autosave = QThread()
self.worker_autosave = Autosave(120)
self.setup_thread(self.worker_autosave, self.thread_autosave)
self.thread_autosave.start()
@staticmethod
def setup_thread(worker, thread):
# Connect the stop_signal signal to the stop method of worker
worker.moveToThread(thread)
worker.finished.connect(thread.quit) # connect the workers finished signal to stop thread
worker.finished.connect(worker.deleteLater) # connect the workers finished signal to clean up worker
thread.finished.connect(thread.deleteLater) # connect threads finished signal to clean up thread
# Make it so when the thread receives the start command the worker starts
thread.started.connect(worker.do_work)
thread.finished.connect(worker.stop)
def start_communication_thread(self):
self.thread_communication.start()
def stop_communication_thread(self):
self.stop_signal.emit()
def start_autosave_thread(self):
self.thread_autosave.start()
@pyqtSlot(str) # Needed to let it know where the data is being passed is a slot to accept it
def update_textbox(self, data):
self.output_box.append(data)
def save_textbox(self):
"""
Saves the contents of the textbox to a timestamped file. Will not overwrite another, unless you save
in the same minute.
:return:
"""
date = ctime()
date = date[11:16] + date[7:11] + date[4:8]+ date[-4:] # Just changes the format to one I prefer
dated_title = f"Results-{date}.txt".replace(" ", "-").replace(":", ".") # Fixes format for filesystem
with open(dated_title, "w") as file:
file.write(self.output_box.toPlainText())
self.output_box.append("Saved")
def autosave(self):
with open("autosave.txt", "w") as file:
file.write(self.output_box.toPlainText())
print(f"Autosaved data at {ctime()}")
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.main_widget.setup_autosave_thread()
main_window.show()
sys.exit(app.exec_())