views:

329

answers:

3

Typically, on a PC, there is some non-standard set of COM ports available for use by a terminal emulator. I would like to be able to scan ports COM1-COM32 to see which (if any) of those ports can be opened as a serial port. I can do this be simply calling CreateFile() on all those ports; however, then I actually have to open/close (and affect DTR/RTS) the port momentarily.

I want to provide an accurate list to the user of the ports that are available. The problem is that I don't want to affect the DTR/RTS lines of the ports that are not currently being used because there may be hardware connected to those ports that does not want to see any transition on DTR/RTS.

Is there a way to just ask the question: "Will CreateFile() succeed?" on specified com ports?

+1  A: 

Several ways

a) QueryDosDevice() - check against COM in the names

b) GetDefaultCommConfig() - Go thru all the possible serial names, eg. try to get config for COM1, COM2, ... COMn

c) HKLM/HARDWARE/DEVICEMAP/SERIALCOMM - Enumerate the keys

d) SetupAPI - if I would remember how to use this without extensively consulting the reference manual I would not be human

e) ???

Edited: as noted by Peter, beyond enumeration, there is no function that can tell you if you can access the device or not.

Marko Teiste
The only one I'm aware of here is GetDefaultCommConfig(), and while the API is simple enough, I have not been able to get that to return successful so far. Is there a trick? Its just a string, followed by a pointer to a structure, followed by a pointer to a populated size value (of the structure). Seems simple enough, but I couldn't get it to work.Thanks,
Ed
The code snippet I use follows... If you see something wrong, shout! :-)voidsomefunc(void){ COMMCONFIG cc; DWORD bsize = sizeof(COMMCONFIG); if (GetDefaultCommConfig("COM1", return; } ...}
Ed
Oops! I was hoping that would format better. :-(
Ed
Looks like the HKLM/HARDWARE/DEVICEMAP/SERIALCOMM approach works best! Thanks much to you and Serge!
Ed
Just because something "works" doesn't mean you should do it. The Setup API (SetupDiXXX family of functions) is the proper, *documented* way to enumerate devices on Windows.
Peter Ruderman
+2  A: 

No, there is no way to ask such a question. The answer wouldn't be meaningful anyway. Even if the OS determined that opening a COM port will succeed at some point in time, that doesn't mean you can open it later. (It might get opened by another application, for instance.) You can use the SetupDiXXX functions to enumerate the COM ports in the system, but this really just returns information about the installed and active drivers. It doesn't provide any guarantees beyond that.

Peter Ruderman
In theory I agree; however, in my situation I can safely assume the WinPC is generally single-user and a quick un-obtrusive scan of the port availability is a useful thing to have. Especially if you have a mix of real COM ports and some USB ports where the COMX values will not be contiguous.Thanks
Ed
A: 

HKLM/HARDWARE/DEVICEMAP/SERIALCOMM is my favorite.

this solution shows the best ease of implementation vs accuracy ratio IMO.

It's accurate enough in most cases although I've seen at least one manufacturer of USB to Serial ports whose HW doesn't always list in there (FTDI IIRC).

Serge - appTranslator