-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.py
60 lines (49 loc) · 1.63 KB
/
write.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
import serial
import time
import pickle
ser = serial.Serial("/dev/ttyS0", baudrate=9600, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=0)
file = open('startup_pickle', 'rb')
startup = pickle.load(file)
file.close()
start = time.time()
beginning = time.time()
def print_batch(batch2print):
time_spent = time.time() - beginning
list_of_bytes = [b['byte'] for b in batch2print]
concatbytes = list_of_bytes[0]
for b in list_of_bytes[1:]:
concatbytes += b
print('TX\t' + f'{time_spent:.2f}' + '\t' + str(concatbytes))
def listen():
time_spent = time.time() - beginning
rx = ser.readline()
print('RX\t' + f'{time_spent:.2f}' + '\t' + str(rx))
return rx
for batch in startup:
for rec in batch:
while time.time() - start < rec['time'] - startup[0][0]['time']:
time.sleep(0.001)
ser.write(rec['byte'])
print_batch(batch)
listen()
print('Startup done')
while 1:
start = time.time() - startup[-4][0]['time']
responses = []
for batch in startup[-4:]:
for rec in batch:
while time.time() - start < rec['time']:
time.sleep(0.001)
ser.write(rec['byte'])
print_batch(batch)
responses.append(listen())
# Switch off if unplugged
if len(responses) >= 4:
if responses[0] == responses[1] == responses[2] == responses[3] == b'':
print('Finished because seems unplugged.')
break
# Do not run over 2 hours
if time.time() - beginning > 2 * 60 * 60:
print('Finished after 2 hours.')
break