-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedoPort.py~
361 lines (295 loc) · 14.3 KB
/
speedoPort.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
import re
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QPlainTextEdit, QTabWidget, QVBoxLayout, QGridLayout, QGroupBox, QRadioButton, QSpacerItem
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtSerialPort import QSerialPort, QSerialPortInfo
from PyQt5.QtGui import QKeyEvent, QPalette, QColor, QPainter, QPainterPath
### Port connection tab -- handles serial connection and sending commands
###
class FirstTab(QtWidgets.QMainWindow):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.port = parent.port
self.statusText = parent.statusText
# self.getButton = parent.getButton
# self.saveButton = parent.saveButton
if hasattr(parent, 'statusButtonHoverEnter'):
self.statusButtonHoverEnter = parent.statusButtonHoverEnter
if hasattr(parent, 'statusButtonHoverLeave'):
self.statusButtonHoverLeave = parent.statusButtonHoverLeave
self.serialPayload = parent.serialPayload
self.port_substring = parent.port_substring
self.max_chars = 3000
self.initUI()
def initUI(self):
self.jsonData = QtWidgets.QTextEdit(self) # making this one in the parent
self.jsonDataView = jsonDataView(self)
self.serialDataView = SerialDataView(self) # these make their own QTextEdit boxes
self.serialSendView = SerialSendView(self)
self.widgets = {}
self.widget_index = 0
self.setCentralWidget( QtWidgets.QWidget(self) )
layout = QtWidgets.QVBoxLayout( self.centralWidget() )
self.radio_button1 = QtWidgets.QCheckBox('Show json stream')
self.linkWidget(self.radio_button1, self.toggle_text_edit)
self.jsonData.setVisible(False)
layout.addWidget(self.radio_button1)
layout.addWidget(self.jsonDataView) # uncomment to see a json box
layout.addWidget(self.serialDataView)
layout.addWidget(self.serialSendView)
layout.setContentsMargins(3, 3, 3, 3)
### Tool Bar ###
self.toolBar = ToolBar(self)
self.addToolBar(self.toolBar)
### Signal Connect ###
self.serialSendView.serialSendSignal.connect(self.sendFromPort)
self.port.readyRead.connect(self.readFromPort)
self.widget_index = 0
def linkWidget(self, w, func):
if isinstance(w, (QtWidgets.QPushButton, QtWidgets.QCheckBox)) and callable(func):
self.widgets[self.widget_index] = {'widget': w, 'function': func}
if isinstance(w, QtWidgets.QPushButton):
try:
w.clicked.connect(func)
except TypeError as e:
print(f"Error connecting QPushButton: {e}")
elif isinstance(w, QtWidgets.QCheckBox):
try:
w.stateChanged.connect(func)
except TypeError as e:
print(f"Error connecting QCheckBox: {e}")
self.widget_index += 1
else:
print(f"Warning: Widget {w} or function {func} is not suitable for connection.")
# keypress events not handled in main are handled here
def keyPressEvent(self, event):
key = event.key()
if key == Qt.Key_4:
self.navigateWidgets(-1)
elif key == Qt.Key_6:
self.navigateWidgets(1)
elif key == Qt.Key_Enter or key == Qt.Key_Return:
f = self.widgets[self.widget_index]['function']
f()
else:
super().keyPressEvent(event)
def navigateWidgets(self, direction):
self.widget_index = (self.widget_index + direction) % len(self.widgets)
for i in range(len(self.widgets)):
if i == self.widget_index:
self.widgets[i]['widget'].setFocus()
self.widgets[i]['widget'].setFocusPolicy(Qt.StrongFocus)
else:
self.widgets[i]['widget'].clearFocus()
def toggle_text_edit(self):
self.radio_button1.stateChanged.disconnect(self.toggle_text_edit)
self.radio_button1.setChecked(not self.radio_button1.isChecked())
self.radio_button1.stateChanged.connect(self.toggle_text_edit)
if self.radio_button1.isChecked():
self.jsonData.setVisible(True)
else:
self.jsonData.setVisible(False)
def portOpen(self):
if not self.port.isOpen():
self.port.setBaudRate( self.toolBar.baudRate() )
self.port.setPortName( self.toolBar.portName() )
self.port.setDataBits( 8 )
self.port.setParity( 0 )
self.port.setStopBits( 0 )
self.port.setFlowControl( 0 )
r = self.port.open(QtCore.QIODevice.ReadWrite)
if not r:
# this does not test if it is already open and happy.
# print ( self.toolBar.portName() )
self.statusText.setText('Port open: error')
self.toolBar.portOpenButton.setChecked(False)
# self.toolBar.serialControlEnable(True)
else:
self.statusText.setText('Port opened')
# self.toolBar.serialControlEnable(False)
else:
self.port.close()
self.statusText.setText('Port closed')
# self.toolBar.serialControlEnable(True)
def portRefresh(self):
self.statusText.setText('Port refresh')
l = []
count = 0
for port in [ port.portName() for port in QSerialPortInfo().availablePorts() ]:
l.append(port)
if self.port_substring in port:
l[count] = l[0]
l[0] = port
count = count + 1
self.toolBar.portNames.clear()
self.toolBar.portNames.addItems( l )
# among other things this loads the serial payload
# which is detected by parent and then loaded into
# UI variables`
def readFromPort(self):
data = self.port.readAll().data().decode()
# strip vt100 chars
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
data = ansi_escape.sub('', data)
data = re.sub('\| ', '\t', data)
# get current buffer, add the data
r = self.serialPayload.reportString() + data
# Extract json strings from input buffer
text_inside_braces = ''
pattern = r'(\{[^}]+\}\r\n)' # find text between "{.*}\r\n"
matches = re.findall(pattern, r) # get all matches
text_inside_braces = ''.join(matches) # Concatenate all matches
remaining_text = re.sub(pattern, '', r) # remove all matches
if len(text_inside_braces) > 0:
t = text_inside_braces.replace('\r', '')
self.jsonDataView.appendJsonText(t, QtGui.QColor(250, 250, 250) )
self.parent.updateJsonData(t)
self.serialPayload.resetTimer()
# hoping this means we have a complete command block
if remaining_text.endswith("@MESC>"):
self.serialDataView.appendSerialText( remaining_text, QtGui.QColor(250, 250, 250) )
self.parent.updateTabsWithGet()
# print("\nstring LEN1: :: {0} :: ".format(self.serialPayload.reportString()))
self.serialPayload.resetString()
# print("string LEN2: :: {0} :: ".format(len(self.serialPayload.reportString())))
r = ''
data = ''
remaining_text = ''
self.serialPayload.setString(remaining_text)
def sendFromPort(self, text):
text = text + '\r\n'
self.serialPayload.resetString()
self.port.write( text.encode() )
self.serialDataView.appendSerialText( text, QtGui.QColor(0, 0, 255) )
class jsonDataView(QtWidgets.QWidget):
def __init__(self, parent):
super(jsonDataView, self).__init__(parent)
self.max_chars = parent.max_chars
self.jsonData = parent.jsonData
# self.jsonData = QtWidgets.QTextEdit(self)
self.jsonData.setReadOnly(True)
self.jsonData.setFontFamily('Courier New')
self.jsonData.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.setLayout( QtWidgets.QGridLayout(self) )
self.layout().addWidget(self.jsonData, 0, 0, 1, 1)
self.layout().setContentsMargins(2, 2, 2, 2)
def appendJsonText(self, appendText, color):
self.jsonData.moveCursor(QtGui.QTextCursor.End)
self.jsonData.setFontFamily('Courier New')
self.jsonData.setTextColor(color)
current_text = self.jsonData.toPlainText()
if len(current_text) > self.max_chars:
# If it exceeds, truncate the text
new_text = current_text[len(current_text)-self.max_chars:]
self.jsonData.setPlainText(new_text)
self.jsonData.moveCursor(QtGui.QTextCursor.End)
self.jsonData.insertPlainText(appendText)
self.jsonData.moveCursor(QtGui.QTextCursor.End)
class SerialDataView(QtWidgets.QWidget):
def __init__(self, parent):
super(SerialDataView, self).__init__(parent)
self.port = parent.port
self.max_chars = parent.max_chars
self.serialData = QtWidgets.QTextEdit(self)
self.serialData.setReadOnly(True)
self.serialData.setFontFamily('Courier New')
self.serialData.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.setLayout( QtWidgets.QGridLayout(self) )
self.layout().addWidget(self.serialData, 0, 0, 1, 1)
self.layout().setContentsMargins(2, 2, 2, 2)
def appendSerialText(self, appendText, color):
self.serialData.moveCursor(QtGui.QTextCursor.End)
self.serialData.setFontFamily('Courier New')
self.serialData.setTextColor(color)
current_text = self.serialData.toPlainText()
if len(current_text) > self.max_chars:
# If it exceeds, truncate the text
new_text = current_text[len(current_text)-self.max_chars:]
self.serialData.setPlainText(new_text)
self.serialData.moveCursor(QtGui.QTextCursor.End)
self.serialData.insertPlainText(appendText)
self.serialData.moveCursor(QtGui.QTextCursor.End)
class SerialSendView(QtWidgets.QWidget):
serialSendSignal = QtCore.pyqtSignal(str)
def __init__(self, parent):
super(SerialSendView, self).__init__(parent)
self.port = parent.port
self.sendData = QtWidgets.QLineEdit(self)
self.sendData.returnPressed.connect(self.onReturnPressed)
self.sendData.setMaximumWidth(200)
self.sendData.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
self.sendButton = QtWidgets.QPushButton('Send')
self.sendButton.clicked.connect(self.sendButtonClicked)
self.sendButton.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred)
s = 'Enter string and send cmd to serial'
if hasattr(parent, 'statusButtonHoverEnter'):
self.sendButton.enterEvent = lambda event: parent.statusButtonHoverEnter(event, s)
if hasattr(parent, 'statusButtonHoverLeave'):
self.sendButton.leaveEvent = parent.statusButtonHoverLeave
spacer = QtWidgets.QSpacerItem(400, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
self.setLayout( QtWidgets.QHBoxLayout(self) )
self.layout().addSpacerItem(spacer)
self.layout().addWidget(self.sendData)
self.layout().addWidget(self.sendButton)
def onReturnPressed(self):
text = self.sendData.text()
text = text + '\r\n'
self.port.write( text.encode() )
self.sendData.clear()
def sendButtonClicked(self):
text = self.sendData.text()
text = text + '\r\n'
self.port.write( text.encode() )
self.sendData.clear()
class ToolBar(QtWidgets.QToolBar):
def __init__(self, parent):
super(ToolBar, self).__init__(parent)
self.portOpenButton = QtWidgets.QPushButton('Open')
if hasattr(parent, 'statusButtonHoverEnter'):
self.portOpenButton.enterEvent = lambda event: parent.statusButtonHoverEnter(event, "Open or close selected port")
if hasattr(parent, 'statusButtonHoverLeave'):
self.portOpenButton.leaveEvent = parent.statusButtonHoverLeave
self.portOpenButton.setCheckable(True)
self.portOpenButton.setMinimumHeight(32)
parent.linkWidget(self.portOpenButton, parent.portOpen)
self.portRefreshButton = QtWidgets.QPushButton('Refresh')
if hasattr(parent, 'statusButtonHoverEnter'):
self.portRefreshButton.enterEvent = lambda event: parent.statusButtonHoverEnter(event, "Refreshes available serial ports")
if hasattr(parent, 'statusButtonHoverLeave'):
self.portRefreshButton.leaveEvent = parent.statusButtonHoverLeave
self.portRefreshButton.setMinimumHeight(32)
parent.linkWidget(self.portRefreshButton, parent.portRefresh)
self.portNames = QtWidgets.QComboBox(self)
l = []
count = 0
for port in [ port.portName() for port in QSerialPortInfo().availablePorts() ]:
l.append(port)
if 'cu.usbmodem' in port:
l[count] = l[0]
l[0] = port
count = count + 1
self.portNames.addItems( l )
self.portNames.setMinimumHeight(32)
self.baudRates = QtWidgets.QComboBox(self)
self.baudRates.addItems([
'110', '300', '600', '1200', '2400', '4800', '9600', '14400', '19200', '28800',
'31250', '38400', '51200', '56000', '57600', '76800', '115200', '128000', '230400', '256000', '921600'
])
self.baudRates.setCurrentText('115200')
self.baudRates.setMinimumHeight(30)
self.addWidget( self.portOpenButton )
self.addWidget( self.portRefreshButton )
self.addWidget( self.portNames )
self.addWidget( self.baudRates )
def serialControlEnable(self, flag):
self.portNames.setEnabled(flag)
self.baudRates.setEnabled(flag)
self.dataBits.setEnabled(flag)
self._parity.setEnabled(flag)
self.stopBits.setEnabled(flag)
self._flowControl.setEnabled(flag)
def baudRate(self):
return int(self.baudRates.currentText())
def portName(self):
return self.portNames.currentText()