Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list_devices: prints serial when discovering ppk2 #43

Merged
merged 1 commit into from
Jul 25, 2024
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
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, port.serial_number[:8])
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