-
Notifications
You must be signed in to change notification settings - Fork 8
/
monitor.py
96 lines (85 loc) · 3.02 KB
/
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
# pip3 install pyftdi
from pyftdi.ftdi import Ftdi, FtdiError
from pyftdi import serialext
from serial.serialutil import SerialException
import sys
import time
def hex_dump(d):
idx = 0
s = ""
for val in d:
s += ('%02x'%val) + " "
idx += 1
if idx == 16:
s += "\n"
idx = 0
print (s)
Ftdi.show_devices()
port = serialext.serial_for_url('ftdi://ftdi:2232:ibrEq5Ay/2', baudrate=1000000)
#f = open("log-" + time.ctime() + ".txt", "w")
is_serial = False
is_text_serial = False
bits = 0
bval = 0
serial_data = []
serial_data_nowipe = []
last_data = -1
data = [-1]
while True:
try:
last_data = data[0]
data = port.read(1)
if not is_text_serial:
print ("...",hex(data[0]))
#f.write(hex(data[0]) + "\n")
serial_data_nowipe += [data[0]]
serial_data_nowipe = serial_data_nowipe[-16:]
if data[0] == 0x55 and last_data == 0xaa and not is_serial:
print("")
#f.write("\n")
#f.flush()
is_serial = False
serial_data = []
#if data[0] == 0x88:
# is_serial = False
# serial_data = []
if data[0] == 0x8F and not is_serial:
is_serial = not is_serial
print ("Starting serial!")
hex_dump (serial_data)
#serial_data = []
elif is_text_serial:
if data[0] == ord('\t') or data[0] == ord('\n') or data[0] == ord('\r') or (data[0] >= ord(' ') and data[0] <= ord('~')):
if data[0] == ord('\n'):
port.write(b'\x55')
if data[0] == ord('\r') and serial_data_nowipe[-2] != ord('\n'):
sys.stdout.write('\n')
else:
sys.stdout.write(chr(data[0]))
else:
sys.stdout.write("\\x" + ('%02x'%data[0]))
elif is_serial:
serial_data += [data[0]]
hex_dump (serial_data)
if serial_data[-8:] == [0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa]:
serial_data = []
elif serial_data[-2:] == [0x20, 0x25]:
serial_data = []
elif serial_data[-2:] == [0xaa, 0x55] and len(serial_data) > 9 and serial_data[-9] == 0x55:
serial_data = []
if serial_data_nowipe[-12:] == [0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0xf0, 0x0f, 0xca, 0xfe]:
is_serial = True
is_text_serial = True
serial_data = []
elif serial_data_nowipe[-12:] == [0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0xbe, 0xef, 0xca, 0xfe]:
is_serial = True
is_text_serial = False
serial_data = []
except FtdiError as e:
print(e)
exit(-1)
except KeyboardInterrupt:
exit(-1)
except Exception as e:
print(e)
exit(-1)