/
Python_loader.py
64 lines (49 loc) · 1.6 KB
/
Python_loader.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
import serial
import time
# Using readlines(), open the .hex file
file1 = open('./build/bootloader_blinky.hex', 'r')
Lines = file1.readlines()
# For IR, use a buad rate of 4800
# Otherwise, use a buad rate of 115200
# Serial port configurations
ser = serial.Serial(
port='/dev/tty.usbserial-0001',
baudrate=4800,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=None
)
# Open the serial port
ser.isOpen()
# Send some hexlines with bad checksums to get into known bootloader state
for i in range(10):
ser.write(":020000041000EB\r".encode()) ;
time.sleep(0.1)
ser.read(1)
# We've accumulated junk in the UART input buffer, clear it
ser.reset_input_buffer()
# Send one final bad checksum, which puts a single char in our
# UART input buffer
ser.write(":020000041000EB\r".encode()) ;
time.sleep(0.1)
# How many lines to we have to send? How many have we sent?
counter = 1
total_lines = float(len(Lines))
# For each line in the hexfile . . .
for line in Lines:
# Keep track of progress
percent_finished = int((counter/total_lines)*100)
counter += 1
# This only returns when we receive a character. It'll either be A or B
# For as long as it's A, loop here sending the same line
while(ord(ser.read(1))==65):
# Report our progress
print(" Progress: " + str(percent_finished) + " percent ", end="\r")
## Write the line
ser.write(line.encode())
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
while ser.inWaiting() > 0:
print(ser.read(1))
ser.close()