-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialtofile.py
73 lines (64 loc) · 2.36 KB
/
serialtofile.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
import serial
import time
port = sys.argv[1]
baudrate = int(sys.argv[2])
file_name = sys.argv[3]
timeout = int(sys.argv[4])
imu = int(sys.argv[5])
start_time = time.time()
ser = serial.Serial(port, baudrate, timeout=1)
# Allow Arduino some time to start sending data
time.sleep(2)
output_list = []
if imu==1:
try:
line = ''
temp_list = []
valid_lines_order = ['A_X', 'A_Y', 'A_Z']
index = 0
while time.time() - start_time < timeout: # read until timeout seconds
if ser.in_waiting:
c = ser.read().decode('utf-8', errors='ignore') # read a byte
if c == '\n': # if the byte is a newline character
if line.startswith('A_') and line.count('=') == 1: # if the line is valid
line_prefix = line.split(' ')[0]
if line_prefix == valid_lines_order[index]:
temp_list.append(line.strip() + "\n")
index += 1
else:
# Reset the temporary list and index
temp_list = []
index = 0
# If the group is complete, append to output_list
if index == len(valid_lines_order):
output_list.extend(temp_list)
temp_list = []
index = 0
line = '' # reset the line
else:
line += c # add the byte to the line
ser.close()
except Exception as e:
print(f"An error occurred: {e}")
ser.close()
with open(file_name, "w") as file:
file.writelines(output_list)
else:
output_list = []
try:
line = ''
while time.time() - start_time < timeout: # read until timeout seconds
if ser.in_waiting:
c = ser.read().decode('utf-8', errors='ignore') # read a byte
if c == '\n': # if the byte is a newline character
output_list.append(line.strip() + "\n")
line = '' # reset the line
else:
line += c # add the byte to the line
ser.close()
except Exception as e:
print(f"An error occurred: {e}")
ser.close()
with open(file_name, "w") as file:
file.writelines(output_list)