Skip to content

Commit

Permalink
Can look for serial ports via vendor id only, product id not required.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtgee authored Oct 7, 2023
1 parent 69efd27 commit 92e55a5
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/panoptes/utils/serial/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_serial_port_info():
return sorted(get_comports(include_links=True), key=operator.attrgetter('device'))


def find_serial_port(vendor_id, product_id, return_all=False): # pragma: no cover
def find_serial_port(vendor_id: str, product_id: Optional[str] = None, return_all: bool = False): # pragma: no cover
"""Finds the serial port that matches the given vendor and product id.
.. doctest::
Expand All @@ -104,17 +104,19 @@ def find_serial_port(vendor_id, product_id, return_all=False): # pragma: no cov
Returns:
str or list: Either the path to the detected port or a list of all comports that match.
"""
# Get all serial ports.
matched_ports = [p for p in get_serial_port_info() if
p.vid == vendor_id and p.pid == product_id]
# Get all serial ports that match the given vendor.
matched_ports = [p for p in get_serial_port_info() if p.vid == vendor_id]

# If given a product_id, try to match.
if product_id is not None:
matched_ports = [p for p in matched_ports if p.pid == product_id]

if len(matched_ports) == 1:
return matched_ports[0].device
elif return_all:
elif return_all is True or product_id is None:
return matched_ports
else:
raise error.NotFound(
f'No serial ports for vendor_id={vendor_id:x} and product_id={product_id:x}')
raise error.NotFound(f'No serial ports for vendor_id={vendor_id:x} and product_id={product_id:x}')


class SerialDevice(object):
Expand Down

0 comments on commit 92e55a5

Please sign in to comment.