-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
53 lines (44 loc) · 1.82 KB
/
monitor.py
File metadata and controls
53 lines (44 loc) · 1.82 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
import sys
import time
import serial
from ms40plus import Command, Framing, Response, Status, Window
def monitor():
port = sys.argv[1]
port = serial.Serial(port, baudrate=9600, timeout=0.33)
framing = Framing()
while True:
for win in [Window.Power, Window.DrivingFrequency, Window.Status]:
cmd = bytes([0x80 + 11, *list(f"{win.value:03d}".encode()), Command.Read.value])
port.write(framing.send(cmd))
time.sleep(0.1)
power = None
driving_frequency = None
status = None
while data := port.read(1):
if payload := framing.receive(data):
# print(payload)
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()
if response_window == Window.Power:
power = value
elif response_window == Window.DrivingFrequency:
driving_frequency = value
elif response_window == Window.Status:
status = Status(value)
if power is not None and driving_frequency is not None and status is not None:
print(f"{power}W\t{driving_frequency}Hz\t{status.name}")
if __name__ == '__main__':
monitor()