Skip to content

Commit 29b5879

Browse files
committed
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.
1 parent 1b02565 commit 29b5879

1 file changed

Lines changed: 43 additions & 20 deletions

File tree

‎tools/pyboard.py‎

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -285,43 +285,66 @@ def main():
285285
cmd_parser.add_argument('files', nargs='*', help='input files')
286286
args = cmd_parser.parse_args()
287287

288-
def execbuffer(buf):
288+
# open the connection to the pyboard
289+
try:
290+
pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait)
291+
except PyboardError as er:
292+
print(er)
293+
sys.exit(1)
294+
295+
# run any command or file(s)
296+
if args.command is not None or len(args.files):
297+
# we must enter raw-REPL mode to execute commands
298+
# this will do a soft-reset of the board
289299
try:
290-
pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait)
291300
pyb.enter_raw_repl()
292-
ret, ret_err = pyb.exec_raw(buf, timeout=None, data_consumer=stdout_write_bytes)
293-
pyb.exit_raw_repl()
294-
pyb.close()
295301
except PyboardError as er:
296302
print(er)
297303
sys.exit(1)
298-
except KeyboardInterrupt:
299-
sys.exit(1)
300-
if ret_err:
301-
stdout_write_bytes(ret_err)
302-
sys.exit(1)
303-
304-
if args.command is not None:
305-
execbuffer(args.command.encode('utf-8'))
306-
307-
for filename in args.files:
308-
with open(filename, 'rb') as f:
309-
pyfile = f.read()
310-
execbuffer(pyfile)
311304

305+
def execbuffer(buf):
306+
try:
307+
ret, ret_err = pyb.exec_raw(buf, timeout=None, data_consumer=stdout_write_bytes)
308+
except PyboardError as er:
309+
print(er)
310+
sys.exit(1)
311+
except KeyboardInterrupt:
312+
sys.exit(1)
313+
if ret_err:
314+
pyb.exit_raw_repl()
315+
pyb.close()
316+
stdout_write_bytes(ret_err)
317+
sys.exit(1)
318+
319+
# run the command, if given
320+
if args.command is not None:
321+
execbuffer(args.command.encode('utf-8'))
322+
323+
# run any files
324+
for filename in args.files:
325+
with open(filename, 'rb') as f:
326+
pyfile = f.read()
327+
execbuffer(pyfile)
328+
329+
# exiting raw-REPL just drops to friendly-REPL mode
330+
pyb.exit_raw_repl()
331+
332+
# if asked explicitly, or no files given, then follow the output
312333
if args.follow or (args.command is None and len(args.files) == 0):
313334
try:
314-
pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait)
315335
ret, ret_err = pyb.follow(timeout=None, data_consumer=stdout_write_bytes)
316-
pyb.close()
317336
except PyboardError as er:
318337
print(er)
319338
sys.exit(1)
320339
except KeyboardInterrupt:
321340
sys.exit(1)
322341
if ret_err:
342+
pyb.close()
323343
stdout_write_bytes(ret_err)
324344
sys.exit(1)
325345

346+
# close the connection to the pyboard
347+
pyb.close()
348+
326349
if __name__ == "__main__":
327350
main()

0 commit comments

Comments
 (0)