Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions meshtastic/serial_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,17 @@ def __init__(self, devPath=None, debugOut=None, noProto=False, connectNow=True):
"""
self.noProto = noProto

self.devPath = None
self.devPath = devPath

if devPath is None:
ports = meshtastic.util.findPorts()
if self.devPath is None:
ports = meshtastic.util.findPorts(True)
logging.debug(f"ports:{ports}")
if len(ports) == 0:
meshtastic.util.our_exit("Warning: No Meshtastic devices detected.")
elif len(ports) > 1:
tmp_ports = meshtastic.util.eliminate_duplicate_port(ports)
if len(tmp_ports) != 1:
message = "Warning: Multiple serial ports were detected so one serial port must be specified with the '--port'.\n"
message += f" Ports detected:{ports}"
meshtastic.util.our_exit(message)
else:
self.devPath = tmp_ports[0]
message = "Warning: Multiple serial ports were detected so one serial port must be specified with the '--port'.\n"
message += f" Ports detected:{ports}"
meshtastic.util.our_exit(message)
else:
self.devPath = ports[0]

Expand Down
2 changes: 1 addition & 1 deletion meshtastic/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def testAll(numTests=5):
This is called from the cli with the "--test" option.

"""
ports = meshtastic.util.findPorts()
ports = meshtastic.util.findPorts(True)
if len(ports) < 2:
meshtastic.util.our_exit("Warning: Must have at least two devices connected to USB.")

Expand Down
2 changes: 1 addition & 1 deletion meshtastic/tests/test_smoke1.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_smoke1_send_hello():
def test_smoke1_port():
"""Test --port"""
# first, get the ports
ports = findPorts()
ports = findPorts(True)
# hopefully there is just one
assert len(ports) == 1
port = ports[0]
Expand Down
32 changes: 32 additions & 0 deletions meshtastic/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,38 @@ def test_findPorts_when_none_found(patch_comports):
patch_comports.assert_called()


@pytest.mark.unitslow
@patch('serial.tools.list_ports.comports')
def test_findPorts_when_duplicate_found_and_duplicate_option_used(patch_comports):
"""Test findPorts()"""
class TempPort:
""" temp class for port"""
def __init__(self, device=None, vid=None):
self.device = device
self.vid = vid
fake1 = TempPort('/dev/cu.usbserial-1430', vid='fake1')
fake2 = TempPort('/dev/cu.wchusbserial1430', vid='fake2')
patch_comports.return_value = [fake1, fake2]
assert findPorts(eliminate_duplicates=True) == ['/dev/cu.wchusbserial1430']
patch_comports.assert_called()


@pytest.mark.unitslow
@patch('serial.tools.list_ports.comports')
def test_findPorts_when_duplicate_found_and_duplicate_option_not_used(patch_comports):
"""Test findPorts()"""
class TempPort:
""" temp class for port"""
def __init__(self, device=None, vid=None):
self.device = device
self.vid = vid
fake1 = TempPort('/dev/cu.usbserial-1430', vid='fake1')
fake2 = TempPort('/dev/cu.wchusbserial1430', vid='fake2')
patch_comports.return_value = [fake1, fake2]
assert findPorts() == ['/dev/cu.usbserial-1430', '/dev/cu.wchusbserial1430']
patch_comports.assert_called()


@pytest.mark.unitslow
def test_convert_mac_addr():
"""Test convert_mac_addr()"""
Expand Down
7 changes: 4 additions & 3 deletions meshtastic/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ def catchAndIgnore(reason, closure):
logging.error(f"Exception thrown in {reason}: {ex}")


def findPorts():
def findPorts(eliminate_duplicates=False):
"""Find all ports that might have meshtastic devices
eliminate_duplicates will run the eliminate_duplicate_port() on the collection

Returns:
list -- a list of device paths
Expand All @@ -123,6 +124,8 @@ def findPorts():
filter(lambda port: port.vid is not None and port.vid not in blacklistVids,
serial.tools.list_ports.comports())))
l.sort()
if eliminate_duplicates:
l = eliminate_duplicate_port(l)
return l


Expand Down Expand Up @@ -421,13 +424,11 @@ def is_windows11():
if platform.system() == "Windows":
if float(platform.release()) >= 10.0:
patch = platform.version().split('.')[2]
print(f'patch:{patch}')
# in case they add some number suffix later, just get first 5 chars of patch
patch = patch[:5]
try:
if int(patch) >= 22000:
is_win11 = True
except Exception as e:
print(f'problem detecting win11 e:{e}')
print(f'is_win11:{is_win11}')
return is_win11