diff --git a/platformio/builder/tools/platformio.py b/platformio/builder/tools/platformio.py index fc8aa0b6..cc9ca226 100644 --- a/platformio/builder/tools/platformio.py +++ b/platformio/builder/tools/platformio.py @@ -2,11 +2,8 @@ # See LICENSE for details. import re -from os import listdir, walk from os.path import isdir, isfile, join -from time import sleep -from serial import Serial def BuildLibrary(env, variant_dir, library_dir): @@ -116,18 +113,6 @@ def ParseBoardOptions(env, path, name): return data -def ResetDevice(env): - """ Pulse the DTR line and flush serial buffer """ - s = Serial(env.subst("$UPLOAD_PORT")) - s.flushInput() - s.setDTR(False) - s.setRTS(False) - sleep(0.1) - s.setDTR(True) - s.setRTS(True) - s.close() - - def exists(_): return True @@ -140,5 +125,4 @@ def generate(env): env.AddMethod(GetDependentLibraries) env.AddMethod(VariantDirRecursive) env.AddMethod(ParseBoardOptions) - env.AddMethod(ResetDevice) return env diff --git a/platformio/commands/serialports.py b/platformio/commands/serialports.py new file mode 100644 index 00000000..79b3d67d --- /dev/null +++ b/platformio/commands/serialports.py @@ -0,0 +1,17 @@ +# Copyright (C) Ivan Kravets +# See LICENSE for details. + +from click import command, echo, secho + +from platformio.util import get_serialports + + +@command("serialports", short_help="List Serial ports") +def cli(): + + for item in get_serialports(): + secho(item['port'], fg="cyan") + echo("----------") + echo("Hardware ID: %s" % item['hwid']) + echo("Description: %s" % item['description']) + echo("") diff --git a/platformio/exception.py b/platformio/exception.py index e58b35b3..9fe41d1e 100644 --- a/platformio/exception.py +++ b/platformio/exception.py @@ -85,3 +85,8 @@ class ProjectInitialized(PlatformioException): class ProjectEnvsNotAvaialable(PlatformioException): MESSAGE = "Please setup environments in `platformio.ini` file." + + +class GetSerialPortsError(PlatformioException): + + MESSAGE = "No implementation for your platform ('%s') available" diff --git a/platformio/util.py b/platformio/util.py index 87168a6f..a3ad8a11 100644 --- a/platformio/util.py +++ b/platformio/util.py @@ -2,11 +2,14 @@ # See LICENSE for details. from os import getcwd, listdir, utime +from os import name as os_name from os.path import dirname, expanduser, isfile, join, realpath from platform import architecture, system from subprocess import PIPE, Popen +from time import sleep from platformio.exception import NotPlatformProject +from serial import Serial try: from configparser import ConfigParser @@ -57,3 +60,24 @@ def exec_command(args): p = Popen(args, stdout=PIPE, stderr=PIPE, shell=use_shell) out, err = p.communicate() return dict(out=out.strip(), err=err.strip()) + + +def reset_serialport(port): + s = Serial(port) + s.flushInput() + s.setDTR(False) + s.setRTS(False) + sleep(0.1) + s.setDTR(True) + s.setRTS(True) + s.close() + + +def get_serialports(): + if os_name == "nt": + from serial.tools.list_ports_windows import comports + elif os_name == "posix": + from serial.tools.list_ports_posix import comports + else: + raise GetSerialPortsError(os_name) + return[{"port": p, "description": d, "hwid": h} for p, d, h in comports()]