Windows likes to hide behind a curtain.

Sometimes it becomes very clear to me that the early Windows Developer approach to gaining market share was to hide the tricky stuff behind curtains. It makes me think of the wizard in OZ from the movie where he yells into the microphone, “Pay no attention to that man behind the curtain!”

So what am I talking about?

Well, when I plug a new USB Serial adapter into a generic Linux workstation, I can usually type something simple like:

ls /dev/tty*

… and BAM, I have nice list of my serial ports, including the one I just plugged in, be it ttyUSB0 or ttyACM0.

If I need more details, I can use:

lsusb

… and be inundated with more information about what is attached than I ever wanted.

So I started looking for a commend line version of this for Windows. (You know the answer) There isn’t one.

Sure, I can use START–> Control Panel –> System and Security –> System –>Device Manager and select “PORTS” but c’mon. This is where Windows so often fails. You can get what you want, provided you don’t mind clicking your way through a few Windows Gui screens.

Working with Arduino (I’m including all similar devices here too) and attaching or detaching the many varied devices can have you grow tired of this game. I really wanted a commend line option.

Now, I will state that I know that the general opinion about using command line on Windows,  I’m using Windows 7 in my case, is less than fun.  Microsoft did make it better with the addition of PowerShell though. Even with the older and limited version of PowerShell that comes default with Windows 7 can be pretty useful.

So, lets get down to the code. What I wanted was a way to know which Comports my system currently saw as being active.

The code to get the current list is actually pretty simple:

$COMportList = [System.IO.Ports.SerialPort]::getportnames()

Write-Host "Active Serial Port List:"
ForEach ($COMport in $COMportList) {
$temp = new-object System.IO.Ports.SerialPort $COMport
Write-Host (" * " +$temp.PortName) -nonewline
Write-Host (" @ " +$temp.BaudRate) -nonewline
Write-Host " Baud"
$temp.Dispose()
}

 

I named this comports.ps1 and you can run it, provided you are IN the PowerShell command window.

To make this even more handy, you can download a utility called PS2EXE from Technet that lets you convert Powershell scripts into executable files.

Powershell to EXE

Its really easy to use and when you are done, you have a nice local executable tool to get a list of active com ports:

Open your command line, run Comports and there you go:

Active Serial Port List:
* COM1 @ 9600 Baud
* COM31 @ 9600 Baud
* COM32 @ 9600 Baud

Note: FTDI used to have a Windows Gadget to show active com ports, but of course, it only worked on FTDI adapters. Thanks but no thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.