From 8394559f79997d427ad9f18f6f4132b8214377e9 Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 15 Dec 2019 20:28:07 +0100 Subject: [PATCH] feat: list serial ports --- nodemcu_uploader/main.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/nodemcu_uploader/main.py b/nodemcu_uploader/main.py index 4ff3152..91dfdd2 100644 --- a/nodemcu_uploader/main.py +++ b/nodemcu_uploader/main.py @@ -10,6 +10,7 @@ import os import sys import glob +import serial from .uploader import Uploader from .term import terminal from serial import VERSION as serialversion @@ -95,7 +96,7 @@ def operation_download(uploader, sources, *args, **kwargs): log.info('All done!') -def operation_list(uploader): +def operation_list_files(uploader): """List file on target""" files = uploader.file_list() for f in files: @@ -105,7 +106,7 @@ def operation_list(uploader): def operation_file(uploader, cmd, filename=''): """File operations""" if cmd == 'list': - operation_list(uploader) + operation_list_files(uploader) if cmd == 'do': for path in filename: uploader.file_do(path) @@ -121,6 +122,14 @@ def operation_file(uploader, cmd, filename=''): uploader.file_remove_all() +def operation_port(args): + if args.cmd == 'list': + ports = serial.tools.list_ports.comports(include_links=False) + print('device', 'vid', 'pid') + for p in ports: + print(p.device, p.vid, p.pid) + + def arg_auto_int(value): """parsing function for integer arguments""" return int(value, 0) @@ -275,6 +284,16 @@ def main_func(): help='Run pySerials miniterm' ) + port_parser = subparsers.add_parser( + 'port', + help='serial port stuff' + ) + + port_parser.add_argument( + 'cmd', + choices=('list',) + ) + args = parser.parse_args() default_level = logging.INFO @@ -291,6 +310,9 @@ def main_func(): # uploader can not claim the port terminal(args.port, str(args.start_baud)) return + elif args.operation == 'port': + operation_port(args) + return # let uploader user the default (short) timeout for establishing connection uploader = Uploader(args.port, args.baud, start_baud=args.start_baud, autobaud_time=args.autobaud_time) @@ -325,5 +347,6 @@ def main_func(): elif args.operation == 'backup': uploader.backup(args.path) + # no uploader related commands after this point uploader.close()