Skip to content

Commit

Permalink
list_devices: prints serial when discovering ppk2
Browse files Browse the repository at this point in the history
  • Loading branch information
wlgrd committed Apr 18, 2024
1 parent e3fb545 commit c5653d8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
9 changes: 5 additions & 4 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions example_mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions src/ppk2_api/ppk2_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c5653d8

Please sign in to comment.