px4-firmware/Tools/HIL/monitor_firmware_upload.py

48 lines
1.5 KiB
Python
Raw Normal View History

#! /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
import sys
2018-08-04 17:48:47 -03:00
def monitor_firmware_upload(port, baudrate):
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
timeout = 180 # 3 minutes
timeout_start = time.time()
timeout_newline = time.time()
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:
sys.exit(0)
elif "nsh>" in serial_line:
sys.exit(0)
else:
if len(serial_line) > 0:
print(serial_line, end='')
if time.time() > timeout_start + timeout:
print("Error, timeout")
sys.exit(-1)
# newline every 10 seconds if still running
if time.time() - timeout_newline > 10:
timeout_newline = time.time()
ser.write("\n".encode("ascii"))
ser.flush()
2018-08-04 17:48:47 -03:00
def main():
parser = ArgumentParser(description=__doc__)
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()