-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolect_serial.py
44 lines (36 loc) · 1 KB
/
colect_serial.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
import os
import serial
import json
from pathlib import Path
from collections import defaultdict
# IMPORTANT
# pip install pyserial
def save_json(data, path):
with open(path, "w") as f:
return json.dump(data, f, indent=4)
def main():
save_logs_dir = Path("logs")
os.makedirs(save_logs_dir, exist_ok=True)
ser = serial.Serial('/dev/ttyUSB0', baudrate=115200)
ser.flushInput()
buffer = []
size = 0
while True:
try:
ser_bytes = ser.readline()
decoded_bytes = ser_bytes.decode()
print(decoded_bytes)
buffer.append(decoded_bytes)
print(size)
size += 1
except KeyboardInterrupt:
print("Keyboard Interrupt")
break
ser.close()
save_path = save_logs_dir / "buffer.json"
save_json(buffer, save_path)
# for key, val in buffer.items():
# save_path = save_logs_dir / f"{key}.json"
# save_json(val, save_path)
if __name__ == "__main__":
main()