Tools/HIL/run_nsh_cmd.py fail if command failed

This commit is contained in:
Daniel Agar
2021-07-15 19:17:48 -04:00
parent c8e6d93cc0
commit 4a0fa08953
5 changed files with 118 additions and 106 deletions
+36 -34
View File
@@ -8,46 +8,50 @@ import re
import sys
def do_nsh_cmd(port, baudrate, cmd):
databits = serial.EIGHTBITS
stopbits = serial.STOPBITS_ONE
parity = serial.PARITY_NONE
ser = serial.Serial(port, baudrate, databits, parity, stopbits, timeout=0.1)
ser = serial.Serial(port, baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=0.1, xonxoff=True, rtscts=False, dsrdtr=False)
timeout_start = time.time()
timeout = 10 # 10 seconds
# wait for nsh prompt
while True:
ser.write("\n".encode("ascii"))
ser.flush()
serial_line = ser.readline().decode("ascii", errors='ignore')
if "nsh>" in serial_line:
break
else:
if len(serial_line) > 0:
print(serial_line, end='')
if time.time() > timeout_start + timeout:
print("Error, timeout waiting for prompt")
sys.exit(1)
# run command
timeout_start = time.time()
timeout = 10 # 10 seconds
# clear
ser.write("\n".encode("ascii"))
ser.flush()
ser.readline()
success_cmd = "cmd succeeded!"
serial_cmd = '{0}; echo "{1}"\n'.format(cmd, success_cmd)
# wait for command echo
serial_cmd = '{0}; echo "{1}"\r\n'.format(cmd, success_cmd)
ser.write(serial_cmd.encode("ascii"))
ser.flush()
ser.readline()
while True:
serial_line = ser.readline().decode("ascii", errors='ignore')
# TODO: require successful command echo
# while True:
# serial_cmd = '{0}; echo "{1}"\n'.format(cmd, success_cmd)
# ser.write(serial_cmd.encode("ascii"))
# ser.flush()
if cmd in serial_line:
break
else:
if len(serial_line) > 0:
print(serial_line, end='')
# serial_line = ser.readline().decode("ascii", errors='ignore')
# if cmd in serial_line:
# break
# else:
# print(serial_line, end='')
# if time.time() > timeout_start + timeout:
# print("Error, timeout")
# sys.exit(-1)
# break
# time.sleep(1)
if time.time() > timeout_start + timeout:
print("Error, timeout waiting for command echo")
break
timeout_start = time.time()
@@ -63,14 +67,12 @@ def do_nsh_cmd(port, baudrate, cmd):
print(serial_line, end='')
if "nsh>" in serial_line:
#sys.exit(-1) # error, command didn't complete successfully
break # TODO: return error on failure
sys.exit(1) # error, command didn't complete successfully
elif "NuttShell (NSH)" in serial_line:
#sys.exit(-1) # error, command didn't complete successfully
break # TODO: return error on failure
sys.exit(1) # error, command didn't complete successfully
if len(serial_line) <= 0:
ser.write("\n".encode("ascii"))
ser.write("\r\n".encode("ascii"))
ser.flush()
if time.time() > timeout_start + timeout: