mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-26 09:26:25 +08:00
Tools/HIL/run_nsh_cmd.py fail if command failed
This commit is contained in:
+36
-34
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user