|
""" |
|
Open Putty sessions with all valid COM ports, except those in the |
|
occupied_com_ports set, which are presumed to be used by the PC for other |
|
purposes. |
|
|
|
Running this script can be automated further by creating a shortcut in Windows |
|
explorer, under "Type the location of the item" entering `C:/Python37/python.exe |
|
"C:/Users/Jake/Documents/Python/putty_com.py"` or equivalent, and under "Type a |
|
name for this shortcut" entering a descriptive name, EG "Putty open COM ports". |
|
This shortcut can then be pinned to the start menu for easy access from |
|
anywhere. |
|
|
|
This script has a command-line interface, which allows min_com_port, |
|
max_com_port, occupied_com_ports, and baud_rate to be specified from the |
|
command-line. For example, the following command will only open serial |
|
connections with COM ports 3, 4, 5, 6, 7, and 8: |
|
|
|
python ./putty_com.py --min_com_port 3 --max_com_port 8 |
|
|
|
The following command will not open serial connections to COM ports 4 or 6, and |
|
all COM ports will be opened with a baud rate of 9600 (instead of the default |
|
which is 115200): |
|
|
|
python ./putty_com.py --occupied_com_ports 4,6 --baud_rate 9600 |
|
|
|
For more, use the --help flag, EG: |
|
|
|
python ./putty_com.py --help |
|
""" |
|
import os |
|
import subprocess |
|
from argparse import ArgumentParser |
|
import serial |
|
|
|
def main(min_com_port, max_com_port, occupied_com_ports, baud_rate): |
|
# Iterate through COM ports |
|
for i in range(min_com_port, max_com_port + 1): |
|
# Check if this port number is occupied |
|
if i in occupied_com_ports: |
|
# This COM port is occupied, so skip |
|
continue |
|
|
|
# Create name of COM port |
|
com_name = "COM{}".format(i) |
|
# Try to open and close a serial connection |
|
try: |
|
s = serial.Serial(com_name) |
|
s.close() |
|
# The serial connection was opened successfully, so start Putty |
|
subprocess.Popen("putty -serial {} -sercfg {}".format( |
|
com_name, |
|
baud_rate |
|
)) |
|
except serial.serialutil.SerialException: |
|
# This COM port is unavailable, so skip |
|
pass |
|
|
|
if __name__ == "__main__": |
|
# Define CLI using ArgumentParser |
|
help_str = ( |
|
"Script for automatically opening serial connections to COM ports." |
|
) |
|
parser = ArgumentParser(description=help_str) |
|
|
|
parser.add_argument( |
|
"--min_com_port", |
|
help="Minimum COM port to open. Default is 0", |
|
default=0, |
|
type=int |
|
) |
|
parser.add_argument( |
|
"--max_com_port", |
|
help="Maximum COM port to open. Default is 20", |
|
default=20, |
|
type=int |
|
) |
|
parser.add_argument( |
|
"--occupied_com_ports", |
|
help="Comma-separated list of occupied COM ports, EG 1,2,3", |
|
default="1", |
|
type=str |
|
) |
|
parser.add_argument( |
|
"--baud_rate", |
|
help="Baud rate for serial connection. Default is 115200", |
|
default=115200, |
|
type=int |
|
) |
|
|
|
args = parser.parse_args() |
|
|
|
# Convert comma-separated string to list of ints |
|
occupied_com_ports_list = [ |
|
int(i) for i in args.occupied_com_ports.split(",") |
|
] |
|
|
|
# Call main function using command-line arguments |
|
main( |
|
args.min_com_port, |
|
args.max_com_port, |
|
occupied_com_ports_list, |
|
args.baud_rate |
|
) |