Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
Autodetect serial port as default
Browse files Browse the repository at this point in the history
  • Loading branch information
kmpm committed Dec 15, 2019
1 parent 8394559 commit 0e7bc5f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
33 changes: 33 additions & 0 deletions nodemcu_uploader/serialutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2015-2019 Peter Magnusson <[email protected]>

from platform import system
from os import environ
from serial.tools import list_ports


def default_port(sysname=system()):
"""This returns the default port used for different systems if SERIALPORT env variable is not set"""
system_default = {
'Windows': 'COM1',
'Darwin': '/dev/tty.SLAB_USBtoUART'
}.get(sysname, '/dev/ttyUSB0')
# if SERIALPORT is set then don't even waste time detecting ports
if 'SERIALPORT' not in environ:
try:
ports = list_ports.comports(include_links=False)
if len(ports) == 1:
return ports[0].device
else:
# clever guessing, sort of
# vid/pid
# 4292/60000 adafruit huzzah
for p in ports:
if p.vid == 4292 and p.pid == 60000:
return p.device
# use last port as fallback
return ports[-1].device
except Exception:
pass

return environ.get('SERIALPORT', system_default)
2 changes: 1 addition & 1 deletion nodemcu_uploader/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from serial.tools import miniterm
import sys

from .utils import default_port
from .serialutils import default_port


def terminal(port=default_port(), baud='9600'):
Expand Down
3 changes: 2 additions & 1 deletion nodemcu_uploader/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import serial

from . import validate
from .serialutils import default_port
from .exceptions import CommunicationTimeout, DeviceNotFoundException, \
BadResponseException, VerificationError, NoAckException
from .utils import default_port, system, hexify, from_file, ENCODING
from .utils import system, hexify, from_file, ENCODING
from .luacode import RECV_LUA, SEND_LUA, LUA_FUNCTIONS, \
LIST_FILES, UART_SETUP, PRINT_FILE, INFO_GROUP, REMOVE_ALL_FILES

Expand Down
13 changes: 2 additions & 11 deletions nodemcu_uploader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"""Various utility functions"""

from platform import system
from os import environ

# from wrapt import ObjectProxy
from sys import version_info

__all__ = ['default_port', 'system', 'hexify', 'from_file', 'PY2', 'ENCODING']
__all__ = ['system', 'hexify', 'from_file', 'PY2', 'ENCODING']

PY2 = version_info.major == 2

Expand All @@ -17,15 +17,6 @@
ENCODING = 'latin1'


def default_port(sysname=system()):
"""This returns the default port used for different systems if SERIALPORT env variable is not set"""
system_default = {
'Windows': 'COM1',
'Darwin': '/dev/tty.SLAB_USBtoUART'
}.get(sysname, '/dev/ttyUSB0')
return environ.get('SERIALPORT', system_default)


def to_hex(x):
if isinstance(x, int):
return hex(x)
Expand Down

0 comments on commit 0e7bc5f

Please sign in to comment.