2021-07-16 11:05:12 -03:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
import serial, time
|
|
|
|
import subprocess
|
|
|
|
from subprocess import call, Popen
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
import re
|
|
|
|
import sys
|
2021-12-31 22:19:17 -04:00
|
|
|
import datetime
|
|
|
|
import serial.tools.list_ports as list_ports
|
2022-01-01 22:04:47 -04:00
|
|
|
import tempfile
|
2021-07-16 11:05:12 -03:00
|
|
|
|
2021-07-16 22:08:08 -03:00
|
|
|
COLOR_RED = "\x1b[31m"
|
2021-07-16 11:46:44 -03:00
|
|
|
COLOR_GREEN = "\x1b[32m"
|
2021-07-16 22:08:08 -03:00
|
|
|
COLOR_YELLOW = "\x1b[33m"
|
2021-07-16 11:46:44 -03:00
|
|
|
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)
|
|
|
|
|
2021-12-31 22:19:17 -04:00
|
|
|
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='')
|
|
|
|
|
2021-07-16 11:46:44 -03:00
|
|
|
|
2022-01-01 22:04:47 -04:00
|
|
|
def do_param_set_cmd(port_url, baudrate, param_name, param_value):
|
|
|
|
ser = serial.serial_for_url(url=port_url, baudrate=baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=3, xonxoff=False, rtscts=False, dsrdtr=False, inter_byte_timeout=1)
|
2021-07-16 11:05:12 -03:00
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_start = time.monotonic()
|
2021-08-23 17:45:46 -03:00
|
|
|
timeout = 30 # 30 seconds
|
2021-07-16 11:05:12 -03:00
|
|
|
|
2022-01-01 22:04:47 -04:00
|
|
|
ser.write("\n\n\n".encode("ascii"))
|
|
|
|
|
2021-07-16 11:05:12 -03:00
|
|
|
# wait for nsh prompt
|
|
|
|
while True:
|
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
|
|
|
|
2022-01-01 22:04:47 -04:00
|
|
|
if len(serial_line) > 0:
|
|
|
|
if "nsh>" in serial_line:
|
|
|
|
break
|
2021-07-16 11:05:12 -03:00
|
|
|
else:
|
2022-01-01 22:04:47 -04:00
|
|
|
if time.monotonic() > timeout_start + timeout:
|
|
|
|
print("Error, timeout waiting for prompt")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
ser.write("\n".encode("ascii"))
|
2021-07-16 11:05:12 -03:00
|
|
|
|
|
|
|
|
2021-07-16 11:46:44 -03:00
|
|
|
# clear
|
2021-12-31 19:55:59 -04:00
|
|
|
ser.reset_input_buffer()
|
2021-07-16 11:46:44 -03:00
|
|
|
|
2021-07-16 11:05:12 -03:00
|
|
|
# run command
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_start = time.monotonic()
|
2021-07-16 11:05:12 -03:00
|
|
|
timeout = 10 # 10 seconds
|
|
|
|
|
|
|
|
cmd = "param set " + param_name + " " + param_value
|
|
|
|
|
|
|
|
# write command (param set) and wait for command echo
|
2021-12-11 14:41:06 -04:00
|
|
|
print("Running command: \'{0}\'".format(cmd))
|
2022-01-01 22:04:47 -04:00
|
|
|
serial_cmd = '{0}\n'.format(cmd)
|
|
|
|
|
2021-07-16 11:05:12 -03:00
|
|
|
ser.write(serial_cmd.encode("ascii"))
|
2022-01-01 22:04:47 -04:00
|
|
|
|
2021-07-16 11:05:12 -03:00
|
|
|
while True:
|
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
|
|
|
|
2022-01-01 22:04:47 -04:00
|
|
|
if len(serial_line) > 0:
|
|
|
|
if cmd in serial_line:
|
|
|
|
break
|
|
|
|
|
2021-07-16 11:46:44 -03:00
|
|
|
print_line(serial_line)
|
2022-01-01 22:04:47 -04:00
|
|
|
|
2021-07-16 11:05:12 -03:00
|
|
|
else:
|
2022-01-01 22:04:47 -04:00
|
|
|
if time.monotonic() > timeout_start + timeout:
|
|
|
|
print("Error, timeout waiting for command echo")
|
|
|
|
break
|
2021-07-16 11:05:12 -03:00
|
|
|
|
2022-01-01 22:04:47 -04:00
|
|
|
# clear
|
|
|
|
ser.reset_input_buffer()
|
2021-07-16 11:05:12 -03:00
|
|
|
|
|
|
|
# verify param value
|
|
|
|
cmd = "param show " + param_name
|
2022-01-01 22:04:47 -04:00
|
|
|
print("Running command: \'{0}\'".format(cmd))
|
|
|
|
serial_cmd = '{0}\n'.format(cmd)
|
2021-07-16 11:05:12 -03:00
|
|
|
ser.write(serial_cmd.encode("ascii"))
|
|
|
|
|
|
|
|
param_show_response = param_name + " ["
|
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_start = time.monotonic()
|
|
|
|
timeout = 3 # 3 seconds
|
2021-07-16 11:05:12 -03:00
|
|
|
|
|
|
|
while True:
|
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
|
|
|
|
2022-01-01 22:04:47 -04:00
|
|
|
if len(serial_line) > 0:
|
2021-07-16 11:46:44 -03:00
|
|
|
print_line(serial_line)
|
2021-07-16 11:05:12 -03:00
|
|
|
|
2022-01-01 22:04:47 -04:00
|
|
|
if param_show_response in serial_line:
|
|
|
|
current_param_value = serial_line.split(":")[-1].strip()
|
|
|
|
|
|
|
|
if (current_param_value == param_value):
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|
2021-07-16 11:05:12 -03:00
|
|
|
else:
|
2022-01-01 22:04:47 -04:00
|
|
|
if time.monotonic() > timeout_start + timeout:
|
|
|
|
if "nsh>" in serial_line:
|
|
|
|
sys.exit(1) # error, command didn't complete successfully
|
|
|
|
elif "NuttShell (NSH)" in serial_line:
|
|
|
|
sys.exit(1) # error, command didn't complete successfully
|
2021-07-16 11:05:12 -03:00
|
|
|
else:
|
2021-12-11 14:41:06 -04:00
|
|
|
if time.monotonic() > timeout_start + timeout:
|
2022-01-01 22:04:47 -04:00
|
|
|
print("Error, timeout")
|
|
|
|
sys.exit(-1)
|
2021-07-16 11:05:12 -03:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2021-12-31 22:19:17 -04:00
|
|
|
|
|
|
|
default_device = None
|
|
|
|
device_required = True
|
|
|
|
|
|
|
|
# select USB UART as default if there's only 1
|
|
|
|
ports = list(serial.tools.list_ports.grep('USB UART'))
|
|
|
|
|
|
|
|
if (len(ports) == 1):
|
|
|
|
default_device = ports[0].device
|
|
|
|
device_required = False
|
|
|
|
|
|
|
|
print("Default USB UART port: {0}".format(ports[0].name))
|
|
|
|
print(" device: {0}".format(ports[0].device))
|
|
|
|
print(" description: \"{0}\" ".format(ports[0].description))
|
|
|
|
print(" hwid: {0}".format(ports[0].hwid))
|
|
|
|
#print(" vid: {0}, pid: {1}".format(ports[0].vid, ports[0].pid))
|
|
|
|
#print(" serial_number: {0}".format(ports[0].serial_number))
|
|
|
|
#print(" location: {0}".format(ports[0].location))
|
|
|
|
print(" manufacturer: {0}".format(ports[0].manufacturer))
|
|
|
|
#print(" product: {0}".format(ports[0].product))
|
|
|
|
#print(" interface: {0}".format(ports[0].interface))
|
|
|
|
|
2021-07-16 11:05:12 -03:00
|
|
|
parser = ArgumentParser(description=__doc__)
|
2021-12-31 22:19:17 -04:00
|
|
|
parser.add_argument('--device', "-d", nargs='?', default=default_device, help='', required=device_required)
|
2022-01-01 22:04:47 -04:00
|
|
|
parser.add_argument("--baudrate", "-b", dest="baudrate", type=int, help="serial port baud rate (default=57600)", default=57600)
|
2021-07-16 11:05:12 -03:00
|
|
|
parser.add_argument("--name", "-p", dest="param_name", help="Parameter name")
|
|
|
|
parser.add_argument("--value", "-v", dest="param_value", help="Parameter value")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-01-01 22:04:47 -04:00
|
|
|
tmp_file = "{0}/pyserial_spy_file.txt".format(tempfile.gettempdir())
|
|
|
|
port_url = "spy://{0}?file={1}".format(args.device, tmp_file)
|
|
|
|
|
|
|
|
print("pyserial url: {0}".format(port_url))
|
|
|
|
|
|
|
|
do_param_set_cmd(port_url, args.baudrate, args.param_name, args.param_value)
|
2021-07-16 11:05:12 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|