From c5653d8ef82b732357b698f5966edcc9a047170f Mon Sep 17 00:00:00 2001 From: Christian Wilgaard Date: Thu, 18 Apr 2024 14:57:24 +0200 Subject: [PATCH] list_devices: prints serial when discovering ppk2 --- example.py | 9 +++++---- example_mp.py | 9 +++++---- src/ppk2_api/ppk2_api.py | 15 ++++++++++++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/example.py b/example.py index 5753754..e89ef7b 100644 --- a/example.py +++ b/example.py @@ -10,11 +10,12 @@ from ppk2_api.ppk2_api import PPK2_API ppk2s_connected = PPK2_API.list_devices() -if(len(ppk2s_connected) == 1): - ppk2_port = ppk2s_connected[0] - print(f'Found PPK2 at {ppk2_port}') +if len(ppk2s_connected) == 1: + ppk2_port = ppk2s_connected[0][0] + ppk2_serial = ppk2s_connected[0][1] + print(f"Found PPK2 at {ppk2_port} with serial number {ppk2_serial}") else: - print(f'Too many connected PPK2\'s: {ppk2s_connected}') + print(f"Too many connected PPK2's: {ppk2s_connected}") exit() ppk2_test = PPK2_API(ppk2_port, timeout=1, write_timeout=1, exclusive=True) diff --git a/example_mp.py b/example_mp.py index 183d5ad..523dcd9 100644 --- a/example_mp.py +++ b/example_mp.py @@ -10,11 +10,12 @@ from ppk2_api.ppk2_api import PPK2_MP as PPK2_API ppk2s_connected = PPK2_API.list_devices() -if(len(ppk2s_connected) == 1): - ppk2_port = ppk2s_connected[0] - print(f'Found PPK2 at {ppk2_port}') +if len(ppk2s_connected) == 1: + ppk2_port = ppk2s_connected[0][0] + ppk2_serial = ppk2s_connected[0][1] + print(f"Found PPK2 at {ppk2_port} with serial number {ppk2_serial}") else: - print(f'Too many connected PPK2\'s: {ppk2s_connected}') + print(f"Too many connected PPK2's: {ppk2s_connected}") exit() ppk2_test = PPK2_API(ppk2_port, buffer_max_size_seconds=1, buffer_chunk_seconds=0.01, timeout=1, write_timeout=1, exclusive=True) diff --git a/src/ppk2_api/ppk2_api.py b/src/ppk2_api/ppk2_api.py index fc34a18..c7caab2 100644 --- a/src/ppk2_api/ppk2_api.py +++ b/src/ppk2_api/ppk2_api.py @@ -213,11 +213,20 @@ def _handle_raw_data(self, adc_value): @staticmethod def list_devices(): import serial.tools.list_ports + ports = serial.tools.list_ports.comports() - if os.name == 'nt': - devices = [port.device for port in ports if port.description.startswith("nRF Connect USB CDC ACM")] + if os.name == "nt": + devices = [ + port.device + for port in ports + if port.description.startswith("nRF Connect USB CDC ACM") + ] else: - devices = [port.device for port in ports if port.product == 'PPK2'] + devices = [ + (port.device, port.serial_number[:8]) + for port in ports + if port.product == "PPK2" + ] return devices def get_data(self):