px4-firmware/Tools/HIL/monitor_firmware_upload.py

82 lines
2.6 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
2021-07-17 10:33:39 -03:00
import datetime
2018-08-04 17:48:47 -03:00
COLOR_RED = "\x1b[31m"
COLOR_GREEN = "\x1b[32m"
COLOR_YELLOW = "\x1b[33m"
COLOR_WHITE = "\x1b[37m"
COLOR_RESET = "\x1b[0m"
def print_line(line):
if "WARNING" in line:
line = line.replace("WARNING", f"{COLOR_YELLOW}WARNING{COLOR_RESET}", 1)
elif "WARN" in line:
line = line.replace("WARN", f"{COLOR_YELLOW}WARN{COLOR_RESET}", 1)
elif "ERROR" in line:
line = line.replace("ERROR", f"{COLOR_RED}ERROR{COLOR_RESET}", 1)
elif "INFO" in line:
line = line.replace("INFO", f"{COLOR_WHITE}INFO{COLOR_RESET}", 1)
if "PASSED" in line:
line = line.replace("PASSED", f"{COLOR_GREEN}PASSED{COLOR_RESET}", 1)
if "FAILED" in line:
line = line.replace("FAILED", f"{COLOR_RED}FAILED{COLOR_RESET}", 1)
if "\n" in line:
current_time = datetime.datetime.now()
print('[{0}] {1}'.format(current_time.isoformat(timespec='milliseconds'), line), end='')
else:
print('{0}'.format(line), end='')
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=False, rtscts=False, dsrdtr=False)
2018-08-04 17:48:47 -03:00
timeout = 180 # 3 minutes
timeout_start = time.monotonic()
timeout_newline = time.monotonic()
return_code = 0
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
2021-07-17 10:33:39 -03:00
if len(serial_line) > 0:
if "ERROR" in serial_line:
return_code = -1
2021-07-17 10:33:39 -03:00
print_line(serial_line)
2018-08-04 17:48:47 -03:00
if "NuttShell (NSH)" in serial_line:
sys.exit(return_code)
elif "nsh>" in serial_line:
sys.exit(return_code)
if time.monotonic() > timeout_start + timeout:
print("Error, timeout")
sys.exit(-1)
# newline every 10 seconds if still running
if time.monotonic() - timeout_newline > 10:
timeout_newline = time.monotonic()
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()