-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode-capture.py
51 lines (47 loc) · 1.76 KB
/
decode-capture.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
"""
Decoder for serial protocol. Input is a Wireshark .CSV file with the "leftover
data" and "Direction" columns included.
"""
import sys
import colorama
import pandas as pd
from ms40plus import Command, Framing, Response, Window
in_framing = Framing()
out_framing = Framing()
framing = {
'IN': in_framing,
'OUT': out_framing
}
df = pd.read_csv(sys.argv[1])
for index, series in df.iterrows():
data = bytes.fromhex(series["Leftover Capture Data"])
direction = series["Direction"]
if payload := framing[direction].receive(data):
address = payload[0]
payload = payload[1:]
print(data, payload)
if direction == 'OUT':
try:
win = Window(int(payload[0:3]))
except ValueError:
win = int(payload[0:3])
command = Command(payload[3])
print(f"{colorama.Fore.YELLOW}addr={address} win={win} {command}")
elif direction == 'IN':
if len(payload) == 1:
response = Response(payload[0])
print(f"\t{colorama.Fore.GREEN}{response}{colorama.Style.RESET_ALL}")
else:
win = Window(int(payload[0:3]))
response = Response(payload[3])
value_bytes = payload[4:]
if len(value_bytes) == 1:
value = bool(value_bytes[0])
elif len(value_bytes) == 6:
value = int(value_bytes)
elif len(value_bytes) == 10:
value = value_bytes.decode().strip()
else:
raise ValueError("Invalid value length")
print(f"\t{colorama.Fore.GREEN}addr={address} win={win} response={response} value={value}{colorama.Style.RESET_ALL}")
# print(series)