This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomports.py
173 lines (130 loc) · 5.04 KB
/
comports.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
from PySide2 import QtCore, QtWidgets
import threading
import codecs
import serial
from serial.tools import list_ports
from serial.tools import hexlify_codec
import status
codecs.register(lambda c: hexlify_codec.getregentry() if c == 'hexlify' else None)
BAUDRATE = 57600
class SerialConnection(QtCore.QObject):
textStream = QtCore.Signal(str)
def __init__(self):
super(SerialConnection, self).__init__()
self.dropdown = QtWidgets.QComboBox()
self.dropdown.currentIndexChanged.connect(self.selectCOMPort)
self.availablePorts = None
self.serial = None
self.alive = False
self._reader_alive = False
self.receiver_thread = None
self.refresh()
# self.rx_decoder = codecs.getincrementaldecoder('UTF-8')('replace')
self.rx_decoder = codecs.getdecoder('UTF-8') #('replace')
# portsString = ''.join([p.device + '\n' for p in self.availablePorts])
@QtCore.Slot(str)
def selectCOMPort(self, index):
if index > 0:
port = self.availablePorts[index-1]
print('select', port)
self.change_port(port)
else:
self._stop_reader()
self.serial = None
def getDropdownWidget(self):
return self.dropdown
def refresh(self):
self.availablePorts = list_ports.comports()
self.dropdown.clear()
self.dropdown.addItem('--- Select COM Port ---')
self.dropdown.addItems([p.device for p in self.availablePorts])
def sendCmd(self, cmd: str):
if self.serial:
line = cmd + '\n'
self.serial.write(line.encode('utf-8'))
def change_port(self, port: serial.Serial):
if port and self.serial and port != self.serial.port:
# reader thread needs to be shut down
self._stop_reader()
self.serial = serial.Serial(port.device, BAUDRATE, timeout=10)
self.sendCmd('reset')
print('open port: ', self.serial, self.serial.port)
status.set_status('open port: {}'.format(self.serial.port))
self._start_reader()
def _start_reader(self):
"""Start reader thread"""
print('Start reader thread')
self._reader_alive = True
# start serial->console thread
self.receiver_thread = threading.Thread(target=self.reader, name='rx')
self.receiver_thread.daemon = True
self.receiver_thread.start()
def _stop_reader(self):
"""Stop reader thread only, wait for clean exit of thread"""
self._reader_alive = False
if self.serial and hasattr(self.serial, 'cancel_read'):
self.serial.cancel_read()
if self.receiver_thread:
self.receiver_thread.join()
def reader(self):
"""loop and copy serial->console"""
try:
while self._reader_alive:
# read all that is there or wait for one byte
# data = self.serial.read(self.serial.in_waiting or 1)
data = self.serial.readline()
if data:
# text = self.rx_decoder.decode(data)
try:
if chr(data[-1]) == '\n':
text, length = self.rx_decoder(data[:-1]) # get rid of newline
# text = ''.join([chr(c) for c in data])
self.textStream.emit(text)
else:
print('PAnIc:', data, data[-1], chr(data[-1]) == '\n')
except e:
print('parse error:', e)
pass
except serial.SerialException:
self.alive = False
# self.console.cancel()
raise # XXX handle instead of re-raise?
START = 'KEY '
END = 'END'
POS = 'POS '
COMMENT = '# '
class SerialParser(QtCore.QObject):
newDataSet = QtCore.Signal(int, list, list)
comment = QtCore.Signal(str)
def __init__(self):
super(SerialParser, self).__init__()
self.started = False
self.timestamps = []
self.positions = []
self.current_encoder = None
@QtCore.Slot(str)
def parse_line(self, line: str):
if line.startswith(END):
self.started = False
self.newDataSet.emit(self.current_encoder, self.timestamps, self.positions)
return
elif line.startswith(START):
self.current_encoder = int(line[len(START):])
self.started = True
self.timestamps = []
self.positions = []
return
elif line.startswith(POS):
s = '# Encoder positions: ' + line[len(POS):]
self.comment.emit(s)
return
elif line.startswith(COMMENT):
self.comment.emit(line)
return
if self.started and ":" in line:
res = line.split(":")
if len(res) == 2:
self.timestamps.append(int(res[0]) / 10)
self.positions.append(int(res[1]))
else:
print('** Received unknown string:', line)