-
-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved automatic detection of a testing serial port // Resolve #4076
- Loading branch information
1 parent
d22b479
commit e5e2210
Showing
6 changed files
with
128 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule docs
updated
4 files
+2 −13 | core/userguide/device/cmd_monitor.rst | |
+4 −3 | projectconf/section_env_monitor.rst | |
+3 −13 | projectconf/section_env_test.rst | |
+1 −1 | projectconf/section_env_upload.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Copyright (c) 2014-present PlatformIO <[email protected]> | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from fnmatch import fnmatch | ||
|
||
import serial | ||
|
||
from platformio.compat import IS_WINDOWS | ||
from platformio.device.list import list_serial_ports | ||
|
||
|
||
def is_pattern_port(port): | ||
if not port: | ||
return False | ||
return set(["*", "?", "[", "]"]) & set(port) | ||
|
||
|
||
def is_serial_port_ready(port, timeout=1): | ||
try: | ||
serial.Serial(port, timeout=timeout).close() | ||
return True | ||
except: # pylint: disable=bare-except | ||
pass | ||
return False | ||
|
||
|
||
def scan_serial_port( | ||
initial_port, board_config=None, upload_protocol=None, ensure_ready=False | ||
): | ||
if initial_port: | ||
if not is_pattern_port(initial_port): | ||
return initial_port | ||
return match_serial_port(initial_port) | ||
port = None | ||
if upload_protocol and upload_protocol.startswith("blackmagic"): | ||
port = scan_blackmagic_serial_port() | ||
if not port and board_config: | ||
port = scan_board_serial_port(board_config) | ||
if port: | ||
return port | ||
|
||
# pick the first PID:VID USB device | ||
for item in list_serial_ports(): | ||
if ensure_ready and not is_serial_port_ready(item["port"]): | ||
continue | ||
port = item["port"] | ||
if "VID:PID" in item["hwid"]: | ||
return port | ||
|
||
return port | ||
|
||
|
||
def match_serial_port(pattern): | ||
for item in list_serial_ports(): | ||
if fnmatch(item["port"], pattern): | ||
return item["port"] | ||
return None | ||
|
||
|
||
def scan_blackmagic_serial_port(): | ||
for item in list_serial_ports(): | ||
port = item["port"] | ||
if IS_WINDOWS and port.startswith("COM") and len(port) > 4: | ||
port = "\\\\.\\%s" % port | ||
if "GDB" in item["description"]: | ||
return port | ||
return None | ||
|
||
|
||
def scan_board_serial_port(board_config): | ||
board_hwids = board_config.get("build.hwids", []) | ||
if not board_hwids: | ||
return None | ||
for item in list_serial_ports(filter_hwid=True): | ||
port = item["port"] | ||
for hwid in board_hwids: | ||
hwid_str = ("%s:%s" % (hwid[0], hwid[1])).replace("0x", "") | ||
if hwid_str in item["hwid"]: | ||
return port | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters