8000 tools/pyboard.py: Support opening serial port in exclusive mode. · micropython/micropython@313460a · GitHub
[go: up one dir, main page]

Skip to content

Commit 313460a

Browse files
committed
tools/pyboard.py: Support opening serial port in exclusive mode.
This is now the default, but can be overridden with CLI `--no-exclusive`, or constructing `Pyboard(..., exclusive=False)`. Signed-off-by: Damien George <damien@micropython.org>
1 parent 6e0f9b9 commit 313460a

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

tools/pyboard.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2014-2019 Damien P. George
7+
# Copyright (c) 2014-2021 Damien P. George
88
# Copyright (c) 2017 Paul Sokolovsky
99
#
1010
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -252,7 +252,9 @@ def inWaiting(self):
252252

253253

254254
class Pyboard:
255-
def __init__(self, device, baudrate=115200, user="micro", password="python", wait=0):
255+
def __init__(
256+
self, device, baudrate=115200, user="micro", password="python", wait=0, exclusive=True
257+
):
256258
self.use_raw_paste = True
257259
if device.startswith("exec:"):
258260
self.serial = ProcessToSerial(device[len("exec:") :])
@@ -264,10 +266,15 @@ def __init__(self, device, baudrate=115200, user="micro", password="python", wai
264266
else:
265267
import serial
266268

269+
# Set options, and exclusive if pyserial supports it
270+
serial_kwargs = {"baudrate": baudrate, "interCharTimeout": 1}
271+
if serial.__version__ >= "3.3":
272+
serial_kwargs["exclusive"] = exclusive
273+
267274
delayed = False
268275
for attempt in range(wait + 1):
269276
try:
270-
self.serial = serial.Serial(device, baudrate=baudrate, interCharTimeout=1)
277+
self.serial = serial.Serial(device, **serial_kwargs)
271278
break
272279
except (OSError, IOError): # Py2 and Py3 have different errors
273280
if wait == 0:
@@ -650,6 +657,11 @@ def main():
650657
action="store_true",
651658
help="Do not follow the output after running the scripts.",
652659
)
660+
group.add_argument(
661+
"--no-exclusive",
662+
action="store_true",
663+
help="Do not try to open the serial device for exclusive access.",
664+
)
653665
cmd_parser.add_argument(
654666
"-f",
655667
"--filesystem",
@@ -662,7 +674,9 @@ def main():
662674

663675
# open the connection to the pyboard
664676
try:
665-
pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait)
677+
pyb = Pyboard(
678+
args.device, args.baudrate, args.user, args.password, args.wait, not args.no_exclusive
679+
)
666680
except PyboardError as er:
667681
print(er)
668682
sys.exit(1)

0 commit comments

Comments
 (0)
0