-
Notifications
You must be signed in to change notification settings - Fork 25
/
saleae_serial_to_bin.py
executable file
·45 lines (34 loc) · 1.1 KB
/
saleae_serial_to_bin.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
#!/usr/bin/env python3
import sys
import csv
if len(sys.argv) != 3:
print("Converts Saleae's Logic Analyzer CSV output into a text/bin file.")
print(f"Usage: {sys.argv[0]} <file.csv/.txt> <outfile.txt>")
sys.exit(1)
try:
input_file = open(sys.argv[1], "r")
output_file = open(sys.argv[2], "wb")
parsed_csv = csv.DictReader(input_file)
# print(c["Value"])
for line in parsed_csv:
value = line["Value"]
# Replace space (indicated by ' ') with real space
value = value.replace("' '", " ")
# Non-ASCII characters are encoded with '23'
if value.startswith("'") and len(value) > 1:
value = value.replace("'", "")
value = chr(int(value))
else:
# Commas are encoded as COMMA
value = value.replace("COMMA", ",")
# Needed to parse \r \n etc
value = bytearray(value, 'ascii').decode('unicode_escape')
byte_value = ord(value)
output_file.write(bytes([byte_value]))
print(f"Wrote {output_file.tell()} bytes to {sys.argv[2]}.")
except FileNotFoundError as e:
print(f"Can't open: {e}")
sys.exit(1)
except PermissionError as e:
print(f"Permission problem: {sys.argv[2]}: {e}")
sys.exit(1)