diff --git a/meshtastic/serial_interface.py b/meshtastic/serial_interface.py index ce19a3b2..3ecc560d 100644 --- a/meshtastic/serial_interface.py +++ b/meshtastic/serial_interface.py @@ -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] diff --git a/meshtastic/test.py b/meshtastic/test.py index c41ee60f..295496fa 100644 --- a/meshtastic/test.py +++ b/meshtastic/test.py @@ -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.") diff --git a/meshtastic/tests/test_smoke1.py b/meshtastic/tests/test_smoke1.py index fee82240..7f58dc12 100644 --- a/meshtastic/tests/test_smoke1.py +++ b/meshtastic/tests/test_smoke1.py @@ -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] diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index 399959c2..548efee5 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -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()""" diff --git a/meshtastic/util.py b/meshtastic/util.py index e3d8a28f..19316210 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -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 @@ -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 @@ -421,7 +424,6 @@ 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: @@ -429,5 +431,4 @@ def is_windows11(): is_win11 = True except Exception as e: print(f'problem detecting win11 e:{e}') - print(f'is_win11:{is_win11}') return is_win11