2020-11-10 23:45:50 -04:00
|
|
|
#! /usr/bin/env python3
|
2018-08-04 17:48:47 -03:00
|
|
|
|
|
|
|
import serial, time
|
|
|
|
import subprocess
|
|
|
|
from subprocess import call, Popen
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
import re
|
2021-07-15 20:17:48 -03:00
|
|
|
import sys
|
2018-08-04 17:48:47 -03:00
|
|
|
|
2019-06-15 13:03:37 -03:00
|
|
|
def monitor_firmware_upload(port, baudrate):
|
2021-07-15 20:17:48 -03:00
|
|
|
ser = serial.Serial(port, baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1, xonxoff=True, rtscts=False, dsrdtr=False)
|
2018-08-04 17:48:47 -03:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
timeout = 180 # 3 minutes
|
2019-06-15 13:03:37 -03:00
|
|
|
timeout_start = time.time()
|
|
|
|
timeout_newline = time.time()
|
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
while True:
|
2020-11-13 16:17:31 -04:00
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
2018-08-04 17:48:47 -03:00
|
|
|
|
|
|
|
if "NuttShell (NSH)" in serial_line:
|
2021-07-15 20:17:48 -03:00
|
|
|
sys.exit(0)
|
|
|
|
elif "nsh>" in serial_line:
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
if len(serial_line) > 0:
|
|
|
|
print(serial_line, end='')
|
2019-06-29 17:29:48 -03:00
|
|
|
|
2019-06-15 13:03:37 -03:00
|
|
|
if time.time() > timeout_start + timeout:
|
|
|
|
print("Error, timeout")
|
2021-07-15 20:17:48 -03:00
|
|
|
sys.exit(-1)
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2020-11-10 23:45:50 -04:00
|
|
|
# newline every 10 seconds if still running
|
|
|
|
if time.time() - timeout_newline > 10:
|
2019-06-15 13:03:37 -03:00
|
|
|
timeout_newline = time.time()
|
2021-07-15 20:17:48 -03:00
|
|
|
ser.write("\n".encode("ascii"))
|
2020-11-10 23:45:50 -04:00
|
|
|
ser.flush()
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2018-08-04 17:48:47 -03:00
|
|
|
def main():
|
|
|
|
parser = ArgumentParser(description=__doc__)
|
2020-11-10 23:45:50 -04:00
|
|
|
parser.add_argument('--device', "-d", nargs='?', default=None, help='', required=True)
|
2018-08-04 17:48:47 -03:00
|
|
|
parser.add_argument("--baudrate", "-b", dest="baudrate", type=int, help="Mavlink port baud rate (default=57600)", default=57600)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
monitor_firmware_upload(args.device, args.baudrate)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|