Scan PC for Serial Ports

Started by
4 comments, last by iMalc 16 years, 5 months ago
Is there a simple way to scan for available serial ports on a WinXP PC? How about simply trying to open a port connection (maybe 1 to 10) & adding it to a list if the port connects? Sounds dodgy to me. I'm using VC++ with MFC.
Advertisement
Potentially helpful
Been there, tried that :)

The trouble with test-opening each port is that there might be some unusual ports in the system, eg. BlueTooth / GPS, USB-to-Serial converters, Serial-over-Ethernet and forwarded serial ports in a virtual PC, some of them hanging the application for several seconds when you open them.

Your best bet is to enumerate the registry keys under HKLM\HARDWARE\DEVICEMAP\SERIALCOMM. It's not officially documented, but the .NET Framework 2.0 has a method that returns the available serial ports in the system which does exactly that.

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
I managed to find some sample code to do it. However, it was super complicated & it would not work when the serial port was a USB-to-Serial variant? Cygon, your method seems to work best. And, it's fairly strait forward & simple.
In my experience (even under Linux), assuming anything about serial ports will crash, hang, dead-lock or mess up the system badly.

For anything port related, I don't try to be smart in any way whatsoever. Let user enter the exact information - if it's wrong, it's their fault.

COM ports are bad and unreliable enough when using supported drivers with correct settings.

If you do use some automated enumeration, I'd strongly suggest against automatically probing anything.
Quote:Original post by Antheus
In my experience (even under Linux), assuming anything about serial ports will crash, hang, dead-lock or mess up the system badly.

For anything port related, I don't try to be smart in any way whatsoever. Let user enter the exact information - if it's wrong, it's their fault.

COM ports are bad and unreliable enough when using supported drivers with correct settings.

If you do use some automated enumeration, I'd strongly suggest against automatically probing anything.
I work on a program at work that enumerates the ports and then probes them in turn. After a few years of no reported problems with this, we only found a problem about a month ago where bluetooth COM ports would lockup the program for a very long period of time.
It worked just fine with USB to serial devices of a number of different kinds.
The problem is though that there isn't any reliable way to determine if a port is a non-bluetooth port before opening the port.

COM port numbers actually go all the way up to 256 btw!

So my advice is that you can enumerate them as Cygon explains no worries. However, only use that to populate some list for the user to select from.
At the absolute most, provide a search button that probes ports as a last resort if you must, but don't ever do the probing without being asked to!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement