Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a list of available serial ports in Win32?

I have some legacy code that provides a list of the available COM ports on the PC by calling the EnumPorts() function and then filtering for the port names that start with "COM".

For testing purposes it would be very useful if I could use this code with something like com0com, which provides pairs of virtual COM ports looped together as a null-modem.

However the com0com ports are not found by the EnumPorts() function (even without filtering for "COM"). HyperTerminal and SysInternals PortMon can both see them, so I'm sure it is installed correctly.

So is there some other Win32 function that provides a definitive list of available serial ports?

like image 543
GrahamS Avatar asked Sep 07 '09 11:09

GrahamS


People also ask

How do I find serial connections?

Verify your console cable is connected to your router, using the console port on your router. Verify your router is connected to your laptop. Navigate to Applications > Utilities > Terminal. You should see a device with “serial” or “Serial” in the name and possibly the model number of the USB serial adapter being used.

How many serial ports are there?

There are two kinds of serial ports: DB25 and DB9, where DB25 is a 25-pin connection, and DB9 is a 9-pin connection. A serial port is a male port that can only send one bit of data at a time, whereas a parallel port is a female port that can send several bits at the same time.


2 Answers

The EnumSerialPorts v1.20 suggested by Nick D uses nine different methods to list the serial ports! We're certainly not short on choice, though the results seem to vary.

To save others the trouble, I'll list them here and indicate their success in finding the com0com ports on my PC (XP Pro SP2):

  1. CreateFile("COM" + 1->255) as suggested by Wael Dalloul
    ✔ Found com0com ports, took 234ms.

  2. QueryDosDevice()
    ✔ Found com0com ports, took 0ms.

  3. GetDefaultCommConfig("COM" + 1->255)
    ✔ Found com0com ports, took 235ms.

  4. "SetupAPI1" using calls to SETUPAPI.DLL
    ✔ Found com0com ports, also reported "friendly names", took 15ms.

  5. "SetupAPI2" using calls to SETUPAPI.DLL
    ✘ Did not find com0com ports, reported "friendly names", took 32ms.

  6. EnumPorts()
    ✘ Reported some non-COM ports, did not find com0com ports, took 15ms.

  7. Using WMI calls
    ✔ Found com0com ports, also reported "friendly names", took 47ms.

  8. COM Database using calls to MSPORTS.DLL
    ✔/✘ Reported some non-COM ports, found com0com ports, took 16ms.

  9. Iterate over registry key HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
    ✔ Found com0com ports, took 0ms. This is apparently what SysInternals PortMon uses.

Based on those results I think the WMI method probably suits my requirements best as it is relatively fast and as a bonus it also gives the friendly names (e.g. "Communications Port (COM1)", "com0com - serial port emulator").

like image 147
GrahamS Avatar answered Sep 29 '22 04:09

GrahamS


It appears that it's not a simple task.

Check out this: EnumSerialPorts v1.20

like image 29
Nick Dandoulakis Avatar answered Sep 29 '22 05:09

Nick Dandoulakis