Skip to content

Commit 88319db

Browse files
committed
feat: add filtering based on serial number
Closes #1027
1 parent e0deeac commit 88319db

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

docs/en/esptool/advanced-options.rst

+1
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,6 @@ at least one FilterValue for each specified FilterType to be considered. Example
158158
* ``--port-filter vid=0x303A --port-filter pid=0x0002`` matches Espressif ESP32-S2 in USB-OTG mode by VID and PID.
159159
* ``--port-filter vid=0x303A --port-filter pid=0x1001`` matches Espressif USB-Serial/JTAG unit used by multiple chips by VID and PID.
160160
* ``--port-filter name=ttyUSB`` matches ports where the port name contains the specified text.
161+
* ``--port-filter serial=7c98d1065267ee11bcc4c8ab93cd958c`` matches ports where the serial number contains the specified text.
161162

162163
See also the `Espressif USB customer-allocated PID repository <https://github.com/espressif/usb-pids>`_

esptool/__init__.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def main(argv=None, esp=None):
135135
parser.add_argument(
136136
"--port-filter",
137137
action="append",
138-
help="Serial port device filter, can be vid=NUMBER, pid=NUMBER, name=SUBSTRING",
138+
help="Serial port device filter, can be vid=NUMBER, pid=NUMBER, name=SUBSTRING, serial=SUBSTRING",
139139
type=str,
140140
default=[],
141141
)
@@ -728,6 +728,7 @@ def add_spi_flash_subparsers(
728728
args.filterVids = []
729729
args.filterPids = []
730730
args.filterNames = []
731+
args.filterSerials = []
731732
for f in args.port_filter:
732733
kvp = f.split("=")
733734
if len(kvp) != 2:
@@ -738,6 +739,8 @@ def add_spi_flash_subparsers(
738739
args.filterPids.append(arg_auto_int(kvp[1]))
739740
elif kvp[0] == "name":
740741
args.filterNames.append(kvp[1])
742+
elif kvp[0] == "serial":
743+
args.filterSerials.append(kvp[1])
741744
else:
742745
raise FatalError("Option --port-filter argument key not recognized")
743746

@@ -776,7 +779,9 @@ def add_spi_flash_subparsers(
776779
initial_baud = args.baud
777780

778781
if args.port is None:
779-
ser_list = get_port_list(args.filterVids, args.filterPids, args.filterNames)
782+
ser_list = get_port_list(
783+
args.filterVids, args.filterPids, args.filterNames, args.filterSerials
784+
)
780785
print("Found %d serial ports" % len(ser_list))
781786
else:
782787
ser_list = [args.port]
@@ -1082,7 +1087,7 @@ def arg_auto_chunk_size(string: str) -> int:
10821087
return num
10831088

10841089

1085-
def get_port_list(vids=[], pids=[], names=[]):
1090+
def get_port_list(vids=[], pids=[], names=[], serials=[]):
10861091
if list_ports is None:
10871092
raise FatalError(
10881093
"Listing all serial ports is currently not available. "
@@ -1103,6 +1108,11 @@ def get_port_list(vids=[], pids=[], names=[]):
11031108
port.name is None or all(name not in port.name for name in names)
11041109
):
11051110
continue
1111+
if serials and (
1112+
port.serial_number is None
1113+
or all(serial not in port.serial_number for serial in serials)
1114+
):
1115+
continue
11061116
ports.append(port.device)
11071117
return sorted(ports)
11081118

0 commit comments

Comments
 (0)