-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialMonitor.py
203 lines (159 loc) · 5.06 KB
/
serialMonitor.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
#!/usr/bin/python3
from serial import Serial
import time
import string
from pathlib import Path
import sys
import threading
import time
import platform
from os import _exit
home = str(Path.home())
## compat
try:
import readline
except ImportError:
import pyreadline as readline
def loadLastSerialPortConf():
try:
with open(home + "/.ictester") as f:
content = f.readlines()
if len(content)>0:
return content[0].strip()
else:
None
except:
pass
def saveSerialPortConf(comNum):
with open(home + "/.ictester", "w") as f:
f.write(comNum)
def selectPort():
default = loadLastSerialPortConf()
print("serial port number [%s] ? " % default, end="")
sys.stdout.flush()
c = sys.stdin.readline().strip()
if c.strip() != "":
com = c
elif default:
com=default
else:
print("must select port")
return selectPort()
saveSerialPortConf(com)
if com.isdigit():
if (platform.system() == "Linux"):
port = '/dev/ttyS' + com
else:
port = 'com' + com
else:
port = com
print("port is " + port)
return port
class TesterInterface():
def _recvResponse(resp):
print("%s" % resp, end='')
sys.stdout.flush()
def __init__(self, *args, **kwargs):
self.responseHandler = None
self.ard = None
self.serialThreadRunning = False
def open(self, serialPort, responseHandler = _recvResponse):
self.responseHandler = responseHandler
self.responseHandler("opening " + serialPort + "\n")
if self.ard:
try:
self.ard.cancel_read()
self.ard.close()
except BaseException as e:
self.responseHandler(str(e))
time.sleep(1)
self.ard = Serial(serialPort, 9600, timeout=1)
self.ard.parity = "O"
self.ard.bytesize = 7
self.responseHandler("opened " + serialPort + "\n")
self.startResponseThread()
def close(self):
self.ard.close()
def _keyboardInput(self):
try:
while True:
line = sys.stdin.readline().strip()
if (line == "q"):
break
# write to arduino
if len(line) > 0:
self.writeToTester(line)
print("QUIT")
_exit(0)
except BaseException as x:
print("ERR READING STDIN\n")
print(x)
_exit(1)
def _readSerial(self):
self.serialThreadRunning = True
current = self.ard
while self.ard and self.ard == current:
msg = None
try:
msg = self.ard.readline()
msg = msg.decode("utf-8") # .strip()
except BaseException as e:
self.responseHandler("ERR: while reading from arduino : %s %s\n" % (type(e), str(e)))
if msg:
self.responseHandler("ERR: read '%s' + \n" % msg)
break
if msg and (len(msg) > 0):
self.responseHandler(msg)
self.serialThreadRunning = False
def startKeyboardThread(self):
if not self.ard:
raise Exception("Tester port not open")
# thread to read local keyboard input
input_thread = threading.Thread(target=self._keyboardInput)
input_thread.daemon = False
input_thread.start()
def startResponseThread(self):
if not self.ard:
raise Exception("Tester port not open")
# thread to read and print data from arduino
sinput_thread = threading.Thread(target=self._readSerial)
sinput_thread.daemon = True
sinput_thread.start()
def writeToTester(self, testcase):
try:
self.ard.write(testcase.encode("utf-8"))
self.ard.write("\n".encode("utf-8"))
except BaseException as x:
self.responseHandler("EXCEPTION : " + str(x))
# Support for CTRL-C needs main thread still running.
# This is actually a crucial step, otherwise all signals sent to your program will be ignored.
# Adding an infinite loop using time.sleep() after the threads have been started will do the trick:
def main():
com = selectPort()
tester = TesterInterface()
try:
tester.open(serialPort=com)
tester.startKeyboardThread()
while True:
if not tester.serialThreadRunning:
print("reopening..")
try:
tester.close()
tester.open(serialPort=com)
tester.startResponseThread()
except BaseException as e:
pass
#print("error while reopening", e)
time.sleep(1)
except KeyboardInterrupt:
_exit(1)
except BaseException as x:
try:
print("ERR3 %s" % x)
tester.close()
sys.stdin.close()
_exit(1)
except:
_exit(1)
if __name__ == '__main__':
main()