Solved

How to detect serial ports

Posted on 1998-10-21
9
818 Views
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.
0
Comment
Question by:rosborn051498
  • 3
  • 3
  • 3
9 Comments
 
LVL 4

Expert Comment

by:agreen
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.
0
 

Author Comment

by:rosborn051498
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.
0
 
LVL 4

Expert Comment

by:agreen
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.
0
 

Author Comment

by:rosborn051498
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.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 15

Accepted Solution

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

Expert Comment

by:NickRepin
ID: 1415512
More precisely,
1)Open key HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
2)Enum all values: Serial0="COM1"
                   Serialxxx="COMyyy"
0
 

Author Comment

by:rosborn051498
ID: 1415513
NickRepin,
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?
0
 
LVL 4

Expert Comment

by:agreen
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
0
 
LVL 15

Expert Comment

by:NickRepin
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 search.microsoft.com. 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:

HKEY_LOCAL_MACHINE\Hardware\DeviceMap\SerialComm

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:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Serial\Parameters\Serialyyy

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.



0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now