-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserial-console.py
368 lines (276 loc) · 12.1 KB
/
serial-console.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import sys
import time
import base64
import serial
import queue
import logging
import threading
from dataclasses import dataclass
from strip_ansi import strip_ansi
from PyQt5 import Qt
from PyQt5.QtCore import QThread, QObject, pyqtSignal
from PyQt5.QtGui import QImage, QPixmap, QFontDatabase
from PyQt5.QtWidgets import QWidget, QPlainTextEdit, \
QLineEdit, QPushButton, QStatusBar,QLabel, QMainWindow, \
QApplication, QHBoxLayout, QVBoxLayout, QErrorMessage, \
QComboBox, QDoubleSpinBox, QCheckBox
from resources import *
logging.getLogger().setLevel(logging.NOTSET)
@dataclass
class SerialReadMessage:
message: str
timestamp: float
@dataclass
class Timer:
currentTimerWindow: float = -1
lastSerialInputTime: float = -1
class SerialReader:
def __init__(self, serial_port, read_Q):
# Serial port must already be open
self.serial_port = serial_port
self.Q = read_Q
def run(self):
self.exit = False
try:
while True:
line = self.serial_port.readline()
if len(line) > 0:
self.Q.put(SerialReadMessage(line, time.time()))
if self.exit:
logging.warning('Stopping serial reader thread')
break
except Exception as ex:
logging.warning('Stopping serial reader thread')
logging.warning(ex)
def start_threaded(self):
logging.info('Starting serial reader thread')
thread = threading.Thread(target=self.run)
thread.start()
def stop(self):
self.exit = True
class SerialWriter:
def __init__(self, serial_port, write_Q):
# Serial port must already be open
self.serial_port = serial_port
self.Q = write_Q
def run(self):
self.exit = False
try:
while True:
try:
if self.exit:
logging.warning('Stopping serial writer thread')
break
line = self.Q.get(block=True, timeout=1)
except queue.Empty:
pass
else:
self.serial_port.write(line)
except Exception as ex:
logging.warning('Stopping serial writer thread')
logging.warning(ex)
def start_threaded(self):
logging.info('Starting serial writer thread')
thread = threading.Thread(target=self.run)
thread.start()
def stop(self):
self.exit = True
class MonitorQ(QThread):
dataAvailable = pyqtSignal(bytes)
def __init__(self, Q):
QThread.__init__(self)
self.Q = Q
def run(self):
while True:
serialReadMessage = self.Q.get()
if Timer.currentTimerWindow != -1 and Timer.lastSerialInputTime != -1:
if serialReadMessage.timestamp <= Timer.lastSerialInputTime + Timer.currentTimerWindow:
self.dataAvailable.emit(serialReadMessage.message)
else:
logging.warn('Discarding message, timer window closed')
else:
self.dataAvailable.emit(serialReadMessage.message)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
monospacedFont = QFontDatabase.systemFont(QFontDatabase.FixedFont)
bottombox = QHBoxLayout()
self.serialPortEdit = QLineEdit()
self.serialPortEdit.setPlaceholderText("Enter full path to the serial port")
self.serialPortEdit.returnPressed.connect(self.on_serialportedit_enter_pressed)
self.serialPortEdit.setFont(monospacedFont)
self.baudrateComboBox = QComboBox()
self.std_baudrates = [50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200]
self.baudrateComboBox.addItems(map(lambda x: str(x), self.std_baudrates))
self.baudrateComboBox.setPlaceholderText("Select Baud rate")
self.baudrateComboBox.setCurrentIndex(-1)
self.connectBtn = QPushButton("Connect")
self.connectBtn.clicked.connect(self.on_connect_btn_clicked)
self.disconnectBtn = QPushButton("Disconnect")
self.disconnectBtn.clicked.connect(self.on_disconnect_btn_clicked)
self.disconnectBtn.setEnabled(False)
bottombox.addWidget(self.serialPortEdit)
bottombox.addWidget(self.baudrateComboBox)
bottombox.addWidget(self.connectBtn)
bottombox.addWidget(self.disconnectBtn)
mainvbox = QVBoxLayout()
self.serialOutputEdit = QPlainTextEdit()
self.serialOutputEdit.setReadOnly(True)
self.serialOutputEdit.setFont(monospacedFont)
self.serialInputEdit = QLineEdit()
self.serialInputEdit.setPlaceholderText("Enter your data you want to send to the port")
self.serialInputEdit.setEnabled(False)
self.serialInputEdit.setFont(monospacedFont)
mainvbox.addWidget(self.serialOutputEdit)
mainvbox.addWidget(self.serialInputEdit)
mainvbox.addLayout(bottombox)
centralWidget = QWidget(self)
centralWidget.setLayout(mainvbox)
self.setCentralWidget(centralWidget)
self.statusBar = QStatusBar()
connectedImage = QImage.fromData(base64.b64decode(connectedImagebase64), "PNG")
disonnectedImage = QImage.fromData(base64.b64decode(disconnectedImagebase64), "PNG")
self.connectedImagePixMap = QPixmap.fromImage(connectedImage)
self.disconnectedImagePixMap = QPixmap.fromImage(disonnectedImage)
self.connectionStatusLabelIcon = QLabel()
self.connectionStatusLabelIcon.setPixmap(self.disconnectedImagePixMap)
self.connectionStatusLabelText = QLabel("Disconnected")
self.statusBar.addWidget(self.connectionStatusLabelIcon)
self.statusBar.addWidget(self.connectionStatusLabelText)
self.timerPushButton = QPushButton("Enable Timer")
self.timerPushButton.setCheckable(True)
self.timerPushButton.toggled.connect(self.on_timerPushButton_toggled)
self.timerWindowDoubleSpinBox = QDoubleSpinBox()
self.timerWindowDoubleSpinBox.setMinimum(0)
self.timerWindowDoubleSpinBox.setMaximum(10)
self.timerWindowDoubleSpinBox.setDecimals(1)
self.timerWindowDoubleSpinBox.setValue(1)
self.timerWindowDoubleSpinBox.setSuffix(" s")
self.clearConsolePushButton = QPushButton("Clear console")
self.clearConsolePushButton.clicked.connect(self.on_clearConsolePushButton_clicked)
self.filterStringCheckBox = QCheckBox("Enable Filter")
self.filterStringCheckBox.stateChanged.connect(self.on_filterStringCheckBox_stateChanged)
self.filterEnabled = False
self.filterString = ""
self.filterStringEdit = QLineEdit()
self.filterStringEdit.setPlaceholderText("Filter String")
self.filterStringEdit.setFont(monospacedFont)
self.filterStringEdit.textEdited.connect(self.on_filterStringEdit_textEdited)
self.statusBar.addPermanentWidget(self.filterStringCheckBox)
self.statusBar.addPermanentWidget(self.filterStringEdit)
self.statusBar.addPermanentWidget(self.clearConsolePushButton)
self.statusBar.addPermanentWidget(self.timerPushButton)
self.statusBar.addPermanentWidget(self.timerWindowDoubleSpinBox)
self.statusBar.layout().setContentsMargins(8, 0, 0, 0)
self.setStatusBar(self.statusBar)
self.setGeometry(300, 300, 800, 495)
self.setWindowTitle('Attify Serial Console')
self.show()
def on_filterStringEdit_textEdited(self, text):
if self.filterStringCheckBox.isChecked():
self.filterString = text
def on_filterStringCheckBox_stateChanged(self, state):
if self.filterStringCheckBox.isChecked():
self.filterString = self.filterStringEdit.text().strip()
self.filterEnabled = True
else:
self.filterEnabled = False
def on_clearConsolePushButton_clicked(self):
self.serialOutputEdit.clear()
def on_timerPushButton_toggled(self, checked):
self.timerWindowDoubleSpinBox.setEnabled(not checked)
if checked:
Timer.currentTimerWindow = self.timerWindowDoubleSpinBox.value()
else:
Timer.currentTimerWindow = -1
def closeEvent(self,event):
try:
self.readMonitorQ.quit()
except:
pass
try:
self.serialReader.stop()
except:
pass
try:
self.serialWriter.stop()
except:
pass
try:
self.serial_port.close()
except:
pass
event.accept()
def on_serialportedit_enter_pressed(self):
self.on_connect_btn_clicked()
def on_serialinput_enter_pressed(self):
cmd = str(self.serialInputEdit.text()).strip()
if len(cmd) > 0:
self.serialOutputEdit.appendPlainText('>> ' + cmd)
cmd = (cmd + '\r\n').encode()
if Timer.currentTimerWindow != -1:
Timer.lastSerialInputTime = time.time()
self.write_Q.put(cmd)
self.serialInputEdit.clear()
def on_connect_btn_clicked(self):
serial_port_txt = self.serialPortEdit.text()
if len(serial_port_txt) > 0:
index = self.baudrateComboBox.currentIndex()
if index == -1:
error_dialog = QErrorMessage(self)
error_dialog.setModal(True)
error_dialog.showMessage("Please select a baud rate")
else:
baud_rate = self.std_baudrates[index]
try:
self.serial_port = serial.Serial(serial_port_txt, timeout=1, baudrate=baud_rate)
except Exception as ex:
error_dialog = QErrorMessage(self)
error_dialog.setModal(True)
error_dialog.showMessage(str(ex))
else:
self.connectBtn.setEnabled(False)
self.disconnectBtn.setEnabled(True)
self.serialInputEdit.setEnabled(True)
self.connectionStatusLabelText.setText("Connected")
self.connectionStatusLabelIcon.setPixmap(self.connectedImagePixMap)
self.baudrateComboBox.setEnabled(False)
self.start_reader()
self.start_writer()
def write_to_Serial_Output_Edit(self, data):
cleaned_text = strip_ansi(data.decode('ascii')).strip()
if (self.filterEnabled and self.filterString not in cleaned_text) or not self.filterEnabled:
self.serialOutputEdit.appendPlainText(cleaned_text)
def start_reader(self):
self.read_Q = queue.Queue()
self.serialReader = SerialReader(self.serial_port, self.read_Q)
self.serialReader.start_threaded()
self.readMonitorQ = MonitorQ(self.read_Q)
self.readMonitorQ.dataAvailable.connect(self.write_to_Serial_Output_Edit)
self.readMonitorQ.start()
def start_writer(self):
self.write_Q = queue.Queue()
self.serialWriter = SerialWriter(self.serial_port, self.write_Q)
self.serialWriter.start_threaded()
self.serialInputEdit.returnPressed.connect(self.on_serialinput_enter_pressed)
def on_disconnect_btn_clicked(self):
self.connectBtn.setEnabled(True)
self.disconnectBtn.setEnabled(False)
self.serialInputEdit.setEnabled(False)
self.connectionStatusLabelText.setText("Disconnected")
self.connectionStatusLabelIcon.setPixmap(self.disconnectedImagePixMap)
self.serialInputEdit.returnPressed.disconnect(self.on_serialinput_enter_pressed)
self.baudrateComboBox.setEnabled(True)
self.readMonitorQ.quit()
self.serialReader.stop()
self.serialWriter.stop()
self.serial_port.close()
def main():
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
#/dev/pts/5