Improve company productivity with a Business Account.Sign Up


How to detect serial ports

Posted on 1998-10-21
Medium Priority
Last Modified: 2013-12-03
I want to populate a combo box with a list of serial ports actually available on a system.  Any ideas?  I thought about just attempting to open COM1-COM8 and looking at return code, but I also want to show any ports that have a friendly name.
Question by:rosborn051498
  • 3
  • 3
  • 3

Expert Comment

ID: 1415507
The Win32 API does not provide any mechanism for determining what ports exist on a system. Windows NT and Windows 95 keep track of installed ports differently from one another, so any one method would not be portable across all Win32 platforms. Some systems even have more ports than the traditional maximum of four. Hardware vendors and serial-device-driver writers are free to name the ports anything they like. For this reason, it is best that users have the ability to specify the port name they want to use.

The CreateFile function opens a communications port. If a port does not exist, an error will occur (ERROR_FILE_NOT_FOUND) after attempting to open the port, and the user should be notified that the port isn’t available.

Author Comment

ID: 1415508
I knew all that already.  However, I have seen apps that can somehow "intelligently" display only the ports that are actually installed.  And this app works under both 95 and NT.  Perhaps they do the CreateFile and look for ERROR_FILE_NOT_FOUND, but I'm hoping to find out if there's a better/cleaner way to do it.  Maybe an undocumented VCOMM call?

I can also walk the registry and look in LOCAL_MACHINE\enum\Root\Ports, but some may not show up.

Expert Comment

ID: 1415509
To use the VCOMM (only Win95) services directly you have to write a VxD.
The best way is to walk the port numbers and call the CreateFile function.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 1415510
What a shame.  Remember the good 'ol days when you could just read the BIOS data area (40:0)?  Oh well, thanks for the answer.  I'll give you the points just for responding so quickly.
LVL 15

Accepted Solution

NickRepin earned 300 total points
ID: 1415511
Look for them under registry key LOCAL_MACHINE/devicemap/COM1...
LVL 15

Expert Comment

ID: 1415512
More precisely,
2)Enum all values: Serial0="COM1"

Author Comment

ID: 1415513
Let me provide a litle more background.  I have a static port driver that emulates COM ports.  Currently, it does not create devicemap\serialcomm entries for the COM ports that it creates (it calls VCOMM_Add_Port).  Now, only the physical ports show up in devicemap\serialcomm.

Should the driver add a serialcomm entry for each port it creates?

Expert Comment

ID: 1415514
VCOMM is called to load the COM/LPT port drivers when they are enumerated by the Plug and Play Configuration Manager. This is at boot time for COM/LPT ports.
When the Plug and Play Configuration Manager(CM) calls VCOMM to load an enumerated device, it passes a handle to the "devnode" data structure for the device. The devnode contains information about the device, including its resource allocation and REGISTRY key locations.
Your driver should notify the CM (create the devnode) about the new ports. Use the CONFIGMG_XXX_XXX services such as CONFIGMG_Create_DevNode, CONFIGMG_Get_Child, CONFIGMG_Locate_DevNode, CONFIGMG_Setup_DevNode, etc
LVL 15

Expert Comment

ID: 1415515
rosborn, that is an another question.
I've never added serial ports from my device driver before.
Also, I'm an expert more in NT than in 95.

I can only give you advice to look for information you need in DDK docs or Also, you can try to manually add registry enties and see what happen.

The only thing I'm sure in is that if you want to obtain a full list of serial ports, you should <see my answer>.

Here is some information from Windows NT Resource Kit (regentry.hlp). Of course, it contains a bit more useful information. Hope it helps.

Deleting Serial Ports

If you need to delete one or more COM ports, you can do this only by deleting the related Registry information in the DeviceMap and Services areas of the Registry. To delete entries in the DeviceMap subkey, you must be logged on as a member of the Administrators group.
To delete a COM port

1.      In Registry Editor, delete the appropriate value entry for the COM port under the following Registry path:


The value entries in this subkey are used to determine all the communication ports available on the system. The values are of the form Serialyyy=COMx, where COMx can be any value from COM1 through COM256, and Serialyyy can be a value from Serial0 through any large positive number, such as Serial15000.
You can identify the value entry you want to delete based on the list you can see by using Ports in Control Panel.

2.      Delete the COM port's related Serialyyy key in the following Registry path:


If you do not delete the related Services subkey for Serialyyy, the COMx port continues to appear in the Ports list in Control Panel each time the system starts, even though there is no related DeviceMap subkey.

DeviceMap Subkey Entries for Serial and Parallel Ports

Remember that the entries in HKEY_LOCAL_MACHINE\Hardware are recreated each time the system is started. The entries in these subkeys are described here for information only.

ParallelX     REG_SZ    

Path:           HKEY_LOCAL_MACHINE\Hardware\Devicemap\Parallel ports

Range:  A string, typically LPTy

Specifies that the Windows NT Parallel device X is the actual device for the MS-DOS name LPTy. These value entries are used to determine all the parallel ports available on the system.

SerialX     REG_SZ    

Path            HKEY_LOCAL_MACHINE\Hardware\Devicemap\Serialcomm
Range: A string, typically COMy

Specifies that the Windows NT Serial device X is the actual device for the MS-DOS name COMy. These value entries are used to determine all the communication ports available on the system.


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Watch the software video of Kernel Import PST to Office 365 tools which can easily import PST and OST files to Office 365 for bulk mailboxes. The process of migration is simple and user can map source and destination mailboxes and easily import data…

584 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question