tools/pyboard.py: Refactor so target is not reset between scripts/cmd.
Previous to this patch pyboard.py would open a new serial connection to the target for each script that was run, and for any command that was run. Apart from being inefficient, this meant that the board was soft-reset between scripts/commands, which precludes scripts from accessing variables set in a previous one. This patch changes the behaviour of pyboard.py so that the connection to the target is created only once, and it's not reset between scripts or any command that is sent with the -c option.
This commit is contained in:
parent
1b02565316
commit
29b5879613
|
@ -285,43 +285,66 @@ def main():
|
||||||
cmd_parser.add_argument('files', nargs='*', help='input files')
|
cmd_parser.add_argument('files', nargs='*', help='input files')
|
||||||
args = cmd_parser.parse_args()
|
args = cmd_parser.parse_args()
|
||||||
|
|
||||||
def execbuffer(buf):
|
# open the connection to the pyboard
|
||||||
|
try:
|
||||||
|
pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait)
|
||||||
|
except PyboardError as er:
|
||||||
|
print(er)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# run any command or file(s)
|
||||||
|
if args.command is not None or len(args.files):
|
||||||
|
# we must enter raw-REPL mode to execute commands
|
||||||
|
# this will do a soft-reset of the board
|
||||||
try:
|
try:
|
||||||
pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait)
|
|
||||||
pyb.enter_raw_repl()
|
pyb.enter_raw_repl()
|
||||||
ret, ret_err = pyb.exec_raw(buf, timeout=None, data_consumer=stdout_write_bytes)
|
|
||||||
pyb.exit_raw_repl()
|
|
||||||
pyb.close()
|
|
||||||
except PyboardError as er:
|
except PyboardError as er:
|
||||||
print(er)
|
print(er)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
except KeyboardInterrupt:
|
|
||||||
sys.exit(1)
|
|
||||||
if ret_err:
|
|
||||||
stdout_write_bytes(ret_err)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if args.command is not None:
|
def execbuffer(buf):
|
||||||
execbuffer(args.command.encode('utf-8'))
|
try:
|
||||||
|
ret, ret_err = pyb.exec_raw(buf, timeout=None, data_consumer=stdout_write_bytes)
|
||||||
|
except PyboardError as er:
|
||||||
|
print(er)
|
||||||
|
sys.exit(1)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sys.exit(1)
|
||||||
|
if ret_err:
|
||||||
|
pyb.exit_raw_repl()
|
||||||
|
pyb.close()
|
||||||
|
stdout_write_bytes(ret_err)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
for filename in args.files:
|
# run the command, if given
|
||||||
with open(filename, 'rb') as f:
|
if args.command is not None:
|
||||||
pyfile = f.read()
|
execbuffer(args.command.encode('utf-8'))
|
||||||
execbuffer(pyfile)
|
|
||||||
|
|
||||||
|
# run any files
|
||||||
|
for filename in args.files:
|
||||||
|
with open(filename, 'rb') as f:
|
||||||
|
pyfile = f.read()
|
||||||
|
execbuffer(pyfile)
|
||||||
|
|
||||||
|
# exiting raw-REPL just drops to friendly-REPL mode
|
||||||
|
pyb.exit_raw_repl()
|
||||||
|
|
||||||
|
# if asked explicitly, or no files given, then follow the output
|
||||||
if args.follow or (args.command is None and len(args.files) == 0):
|
if args.follow or (args.command is None and len(args.files) == 0):
|
||||||
try:
|
try:
|
||||||
pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait)
|
|
||||||
ret, ret_err = pyb.follow(timeout=None, data_consumer=stdout_write_bytes)
|
ret, ret_err = pyb.follow(timeout=None, data_consumer=stdout_write_bytes)
|
||||||
pyb.close()
|
|
||||||
except PyboardError as er:
|
except PyboardError as er:
|
||||||
print(er)
|
print(er)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if ret_err:
|
if ret_err:
|
||||||
|
pyb.close()
|
||||||
stdout_write_bytes(ret_err)
|
stdout_write_bytes(ret_err)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
# close the connection to the pyboard
|
||||||
|
pyb.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue