forked from GrassLab/osdi2020
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsendimg.py
60 lines (45 loc) · 1.36 KB
/
sendimg.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 argparse
import serial
import os
import sys
import numpy as np
import time
parser = argparse.ArgumentParser()
parser.add_argument("image")
parser.add_argument("tty")
args = parser.parse_args()
def checksum(bytecodes):
# convert bytes to int
return int(np.array(list(bytecodes), dtype=np.int32).sum())
def main():
try:
ser = serial.Serial(args.tty, 115200)
except:
print("Serial init failed!")
exit(1)
file_path = args.image
file_size = os.stat(file_path).st_size
with open(file_path, 'rb') as f:
bytecodes = f.read()
file_checksum = checksum(bytecodes)
cmd = "loadimg\n"
for c in cmd:
ser.write(c.encode())
time.sleep(0.01)
ser.write(b'\n')
time.sleep(0.1)
ser.write(file_size.to_bytes(4, byteorder="big"))
ser.write(file_checksum.to_bytes(4, byteorder="big"))
print(f"Image Size: {file_size}, Checksum: {file_checksum}")
per_chunk = 128
chunk_count = file_size // per_chunk
chunk_count = chunk_count + 1 if file_size % per_chunk else chunk_count
for i in range(chunk_count):
sys.stdout.write('\r')
sys.stdout.write("%d/%d" % (i + 1, chunk_count))
sys.stdout.flush()
ser.write(bytecodes[i * per_chunk: (i+1) * per_chunk])
while not ser.writable():
pass
if __name__ == "__main__":
main()