-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial-monitor.py
executable file
·277 lines (222 loc) · 7.89 KB
/
serial-monitor.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
#!/usr/bin/python
import serial
import signal
import subprocess
import argparse
import json
import re
import unicodedata
import stat
import os
from datetime import datetime
from sys import stdout, stderr
from time import sleep
unlock = False
clearFormat = '\033[0m'
def printError(message):
stderr.write("\033[38;5;9m" + message + "\033[0m\n\n")
def userSignalHandler(sig, frame):
global unlock
unlock = True
def isDeviceAvailable(name):
result = subprocess.run(['fuser', name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return result.returncode == 1
def doesDeviceExist(name):
try:
os.stat(name)
return True
except FileNotFoundError:
return False
def pushFormatter(formatters, formatter, index):
if index in formatters:
formatters[index].append(formatter)
else:
formatters[index] = [formatter]
def handleFormatting(line, regexes):
formatters = {}
sweep = 0
stop = False
for regex in regexes:
index = 0
sweep += 1
while index < len(line):
match = regex["re"].search(line[index:])
if match is None:
break
start = match.start() + index
end = match.end() + index
index = end
formatter = { 'sweep': sweep, 'regex': regex, 'start': start, 'end': end }
if len(regex['prefix']) > 0:
pushFormatter(formatters, formatter, start)
pushFormatter(formatters, formatter, end)
if regex['continueOnMatch'] is False:
stop = True
break
if stop:
break
lastFormatterText = ''
activeFormatters = []
formattedLine = ''
for i in range(len(line) + 1):
if i in formatters:
formatterText = ''
formatterList = formatters[i]
topActiveFormatter = None
if len(activeFormatters) > 0:
topActiveFormatter = activeFormatters[-1]
for formatter in formatterList:
if formatter['start'] == i:
if topActiveFormatter is None or topActiveFormatter['sweep'] <= formatter['sweep']:
activeFormatters.append(formatter)
if len(formatter['regex']['prefix']) > 0 and (topActiveFormatter is None or topActiveFormatter['sweep'] < formatter['sweep']):
formatterText = formatter['regex']['prefix']
if formatter['end'] == i:
previousTopFormatter = topActiveFormatter
if formatter in activeFormatters:
activeFormatters.remove(formatter)
if len(activeFormatters) > 0:
topActiveFormatter = activeFormatters[-1]
else:
topActiveFormatter = None
if formatter == previousTopFormatter:
if len(formatter['regex']['suffix']) > 0:
formatterText = formatter['regex']['suffix']
elif formatterText == '':
if topActiveFormatter is not None and i != len(line):
formatterText = topActiveFormatter['regex']['prefix']
else:
formatterText = clearFormat
lastFormatterText = formatterText
formattedLine += formatterText
if i < len(line):
c = line[i]
formattedLine += c
if len(lastFormatterText) > 0 and lastFormatterText != clearFormat:
formattedLine += clearFormat
return formattedLine
def replaceControlChars(match):
# https://stackoverflow.com/a/13928029
return r'\x{0:02x}'.format(ord(match.group()))
def getControlCharsRe():
chars = ''
for i in range(31):
if i != 10 and i != 13:
chars += chr(i)
return re.compile('[' + chars + ']')
def reset(serialInstance):
serialInstance.setDTR(False)
sleep(1)
serialInstance.flushInput()
serialInstance.setDTR(True)
def connect(name, baudrate, regexes, showTimestamps, showDeltas, wdreset):
with serial.Serial(name, baudrate=baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=0.1, xonxoff=0, rtscts=0) as ser:
stdout.write('Connecting...')
stdout.flush()
reset(ser)
stdout.write(' Done\n\n')
hiddenRe = getControlCharsRe()
lastTimestamp = None
lastChar = '\n'
lastOutputTime = datetime.now()
while not unlock:
line = ser.readline().decode("utf-8", "backslashreplace")
line = hiddenRe.sub(replaceControlChars, line)
now = datetime.now()
if len(line) > 0:
if lastChar == '\n' and showTimestamps:
if showDeltas and lastTimestamp is not None:
timeDiff = now - lastTimestamp
stdout.write('\033[38;5;237m%s ▲%s: \033[0m' % (str(now), str(timeDiff)))
else:
stdout.write('\033[38;5;237m%s: \033[0m' % str(now))
lastTimestamp = now
line = handleFormatting(line, regexes)
stdout.write(line + clearFormat)
lastChar = line[-1]
stdout.flush()
lastOutputTime = datetime.now()
wdTimeDiff = now - lastOutputTime
if wdreset is not None and wdTimeDiff.total_seconds() >= wdreset:
print('\nResetting due to timeout waiting on output (%s)\n' % (str(wdTimeDiff)))
lastOutputTime = datetime.now()
reset(ser)
lastTimestamp = None
def getRegexes(path):
regexes = []
try:
with open(path, 'r') as f:
content = json.load(f)
for record in content:
flags = 0
if 'flags' in record:
recordFlags = record['flags'].lower()
if 'i' in recordFlags:
flags |= re.I
if 'a' in recordFlags:
flags |= re.A
if 'l' in recordFlags:
flags |= re.L
if 'm' in recordFlags:
flags |= re.M
if 's' in recordFlags:
flags |= re.S
if 'x' in recordFlags:
flags |= re.X
regexes.append({
"re": re.compile(record["pattern"], flags),
"prefix": record["prefix"].replace("\\033", "\033") if "prefix" in record else '',
"suffix": record["suffix"].replace("\\033", "\033") if "suffix" in record else '',
"continueOnMatch": record["continueOnMatch"] if "continueOnMatch" in record else True
})
except Exception as err:
printError("An error occurred while parsing the provided json file: " + str(err))
return None
return regexes
def main(name, baudrate, regexesPath, showTimestamps, showDeltas, unlockSleepAmount, wdreset, signalValue):
global unlock
regexes = []
if regexesPath is not None:
regexes = getRegexes(regexesPath)
if regexes is None:
return
signal.signal(signalValue, userSignalHandler)
print(f'Name: {name}')
print(f'Baudrate: {baudrate}')
print(f'Signal: {signalValue.name}')
print()
try:
while True:
try:
if isDeviceAvailable(name):
connect(name, baudrate, regexes, showTimestamps, showDeltas, wdreset)
if unlock:
print('\nUnlocked due to request.')
sleep(unlockSleepAmount)
unlock = False
else:
print('Waiting for device to be available...')
while not isDeviceAvailable(name):
sleep(1)
except serial.serialutil.SerialException as e:
if 'device disconnected' not in str(e):
raise
print('\nLost connection.')
print('Waiting for device to be available...')
while not doesDeviceExist(name):
sleep(1)
except KeyboardInterrupt:
print('Exiting')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Monitors serial activity.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('name', help='The device to connect to.')
parser.add_argument('-b', '--baudrate', type=int, default=9600, help='The baudrate to use.')
parser.add_argument('-r', '--regexes', help='The json file containing regexes to use for style highlighting.')
parser.add_argument('-w', '--wait', type=float, default=5, help='The amount of seconds to unlock and sleep / wait for before retrying to connect when requested to disconnect.')
parser.add_argument('-s', '--signal', default='SIGUSR1', help='The signal to listen to that will trigger a temporary disconnect.')
parser.add_argument('-t', '--timestamps', action='store_true', help='Display timestamps before each new line received.')
parser.add_argument('-d', '--deltas', action='store_true', help='Display deltas in back to back timestamps.')
parser.add_argument('--wdreset', type=float, default=None, help='The amount of seconds to wait for output before auto resetting the device.')
args = parser.parse_args()
signalValue = signal.Signals[args.signal]
main(args.name, args.baudrate, args.regexes, args.timestamps, args.deltas, args.wait, args.wdreset, signalValue)