Opening putty from the command line

I was pleased to discover that putty can actually be launched via the command line. Most of the guides online make use of this feature to make it easier to launch SSH sessions. However being able to launch putty on the command line is also incredibly helpful for opening serial port connections.

putty is my go-to tool in Windows for connecting to embedded devices that communicate over UARTs, usually in the form of virtual COM ports exposed through serial to USB chips. Due to the somewhat convoluted way that the Windows USB subsystem works the COM port number a device gets when connected over USB can be somewhat unpredictable. If the port settings do not match a saved configuration, quite a bit of keyboard and mouse input is needed to adjust the settings.

The putty documentation describes all of the options. However, the two that are relevant to serial ports are -serial and -sercfg, which control the port name and settings, respectively. Assuming the putty.exe executable is in your path, to open COM23 using speed 115200bps, 8 data bits, no parity, and 1 stop bit, (8-N-1) and no flow control the command line is:

putty.exe -serial COM23 -sercfg 115200,8,n,1,N

The above can be a handful to type, especially if your devices are running with the same port settings and the only thing changing is the port name. I have yet to encounter a device that does not use 8-N-1.

The following powershell script takes in a port name and an optional speed, otherwise defaults to 115200bps.

Open-ComPort.ps1:

param([string]$port, [string]$speed = "115200")
putty.exe -serial $port -sercfg $speed,8,n,1,N

If this script is somewhere in your path then can type either Open-ComPort.ps1 -port COM23 for the default speed (115200bps) or Open-ComPort.ps1 -port COM23 -speed 9600 for a different speed (9600bps in this case).