-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprobe-windows.py
More file actions
75 lines (60 loc) · 1.97 KB
/
probe-windows.py
File metadata and controls
75 lines (60 loc) · 1.97 KB
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
import os
import pickle
import sys
import serial
from ms40plus import Framing, Response, Window
port = sys.argv[1]
port = serial.Serial(port, baudrate=9600, timeout=0.33)
framing = Framing()
session = []
window = bytes([0x80 + 11])
def read_command(win):
return framing.send(window + f"{win:03d}0".encode())
def show_response(win, payload):
length = len(payload)
response_window = payload[0]
if length == 2:
response = Response(payload[1])
print(win.name, response)
else:
response_window = Window(int(payload[1:4]))
response = Response(payload[4])
value_bytes = payload[5:]
if len(value_bytes) == 1:
value = bool(value_bytes[0])
elif len(value_bytes) == 6:
try:
value = int(value_bytes.decode())
except ValueError:
value = value_bytes
elif len(value_bytes) == 10:
value = value_bytes.decode().strip()
else:
print("invalid length", len(value_bytes))
exit()
print(response_window.name + ':', repr(value))
def scan_windows():
for win in Window:
command = read_command(win.value)
# print(command)
port.write(command)
buffer = bytearray()
while data := port.read(1):
buffer.extend(data)
if payload := framing.receive(data):
# print('buffer', buffer, 'payload', payload)
session.append([command, bytes(buffer), payload])
buffer.clear()
show_response(win, payload)
if not buffer:
# print("complete")
break
if buffer:
session.append([win, command, bytes(buffer), None])
print('leftover', buffer)
buffer.clear()
with open("session.part", "wb") as outfile:
pickle.dump(session, outfile)
os.replace("session.part", "session")
if __name__ == '__main__':
scan_windows()