7171# MacOS: 'USB VID:PID=1366:1059 SER=001051246141 LOCATION=0-1.4.2.3'
7272# Windows: [(1, 'USB VID:PID=1366:1059 SER=001057731013'), (2, 'USB VID:PID=1366:1059 SER=001057731013 LOCATION=1-21:x.2')]
7373
74-
75- # Returns a list of printable name, serial number and serial port for connected Nordic boards
76- def get_connected_nordic_boards () -> List [Tuple [str , Union [str , int ], ListPortInfo ]]:
74+ def get_comports_fixed_ordering () -> List [ListPortInfo ]:
7775 if platform .system () == 'Darwin' :
7876 ports = sorted (list_ports .comports (), key = lambda x : x .device )
7977 else :
8078 ports = sorted (list_ports .comports (), key = lambda x : x .hwid )
79+ return ports
80+
81+
82+ # Returns a list of printable name, serial number and serial port for connected Nordic boards
83+ def get_connected_nordic_boards () -> List [Tuple [str , Union [str , int ], ListPortInfo ]]:
84+ ports = get_comports_fixed_ordering ()
8185 nordic_boards = defaultdict (list )
8286 for port in ports :
8387 # Get serial number from hwid, because port.serial_number is not always available
@@ -166,7 +170,7 @@ def select_jlink(jlinks : List[int], list_all: bool) -> int:
166170def select_device_by_serial (serial_number : Union [str , int ], list_all : bool ) -> Tuple [ListPortInfo , Union [str , int ]]:
167171 serial_devices = [
168172 x
169- for x in list_ports . comports ()
173+ for x in get_comports_fixed_ordering ()
170174 if extract_serial_number_from_serial_device (x ) == serial_number
171175 ]
172176 if len (serial_devices ) == 0 :
@@ -207,7 +211,7 @@ def select_device(rtt : bool, serial_number : Optional[Union[str, int]], port :
207211
208212 if port :
209213 # Serial ports are unique, so we just check if it exists and try to get a serial number
210- serial_devices = [x for x in list_ports . comports () if x .device == port ]
214+ serial_devices = [x for x in get_comports_fixed_ordering () if x .device == port ]
211215 if len (serial_devices ) == 0 :
212216 raise Exception (f"No device found with port { port } " )
213217 extracted_serial_number = extract_serial_number_from_serial_device (
@@ -225,7 +229,7 @@ def select_device(rtt : bool, serial_number : Optional[Union[str, int]], port :
225229
226230 if list_all :
227231 # Show all ports, no filtering
228- ports = list_ports . comports ()
232+ ports = get_comports_fixed_ordering ()
229233 question = inquirer .List (
230234 "port" ,
231235 message = "Select a serial port" ,
0 commit comments