Skip to content

Commit

Permalink
feat: add filtering based on serial number
Browse files Browse the repository at this point in the history
Closes #1027
  • Loading branch information
Dzarda7 committed Nov 1, 2024
1 parent e0deeac commit 88319db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/en/esptool/advanced-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,6 @@ at least one FilterValue for each specified FilterType to be considered. Example
* ``--port-filter vid=0x303A --port-filter pid=0x0002`` matches Espressif ESP32-S2 in USB-OTG mode by VID and PID.
* ``--port-filter vid=0x303A --port-filter pid=0x1001`` matches Espressif USB-Serial/JTAG unit used by multiple chips by VID and PID.
* ``--port-filter name=ttyUSB`` matches ports where the port name contains the specified text.
* ``--port-filter serial=7c98d1065267ee11bcc4c8ab93cd958c`` matches ports where the serial number contains the specified text.

See also the `Espressif USB customer-allocated PID repository <https://github.com/espressif/usb-pids>`_
16 changes: 13 additions & 3 deletions esptool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def main(argv=None, esp=None):
parser.add_argument(
"--port-filter",
action="append",
help="Serial port device filter, can be vid=NUMBER, pid=NUMBER, name=SUBSTRING",
help="Serial port device filter, can be vid=NUMBER, pid=NUMBER, name=SUBSTRING, serial=SUBSTRING",
type=str,
default=[],
)
Expand Down Expand Up @@ -728,6 +728,7 @@ def add_spi_flash_subparsers(
args.filterVids = []
args.filterPids = []
args.filterNames = []
args.filterSerials = []
for f in args.port_filter:
kvp = f.split("=")
if len(kvp) != 2:
Expand All @@ -738,6 +739,8 @@ def add_spi_flash_subparsers(
args.filterPids.append(arg_auto_int(kvp[1]))
elif kvp[0] == "name":
args.filterNames.append(kvp[1])
elif kvp[0] == "serial":
args.filterSerials.append(kvp[1])
else:
raise FatalError("Option --port-filter argument key not recognized")

Expand Down Expand Up @@ -776,7 +779,9 @@ def add_spi_flash_subparsers(
initial_baud = args.baud

if args.port is None:
ser_list = get_port_list(args.filterVids, args.filterPids, args.filterNames)
ser_list = get_port_list(
args.filterVids, args.filterPids, args.filterNames, args.filterSerials
)
print("Found %d serial ports" % len(ser_list))
else:
ser_list = [args.port]
Expand Down Expand Up @@ -1082,7 +1087,7 @@ def arg_auto_chunk_size(string: str) -> int:
return num


def get_port_list(vids=[], pids=[], names=[]):
def get_port_list(vids=[], pids=[], names=[], serials=[]):
if list_ports is None:
raise FatalError(
"Listing all serial ports is currently not available. "
Expand All @@ -1103,6 +1108,11 @@ def get_port_list(vids=[], pids=[], names=[]):
port.name is None or all(name not in port.name for name in names)
):
continue
if serials and (
port.serial_number is None
or all(serial not in port.serial_number for serial in serials)
):
continue
ports.append(port.device)
return sorted(ports)

Expand Down

1 comment on commit 88319db

@cederom
Copy link

@cederom cederom commented on 88319db Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BIG THANK YOU @Dzarda7 !! :-)

Please sign in to comment.