The Code Project View our advertisersUltimate Grid 5.0 for MFC - Now available!Advertise on the CodeProject
Home >> System >> General

Another serial port enumerator
By Joaquín M López Muñoz

A library for enumeration of serial ports that works on 9x, NT 4.0 and 2000 platforms 
 VC 4-6, Win95-98, NT4, W2K
 Posted 17 Jul 2001
Articles by this author
Send to a friend
Printer friendly version
Views: 594
FAQ
What's New
Lounge
Contribute
Message Boards
5 users have rated this article. result:
4.4 out of 5.

Introduction

ListPorts is a function that lists all serial ports available on the system, along with some descriptive text suitable to be shown to the user in place of the somewhat terse "COM1", "COM2", etc. You can see an example of this on the "Port" combobox at the system Modem properties dialog box.

PJ Naughter has already written some code with this very purpose (see EnumSerialPorts at CodeProject, or click here for the latest version of his library). ListPorts has, IMHO, some advantages with respect to PJ Naughter's approach:

Anyway, I wouldn't like to be blamed for proselytism: please compare both approaches and make your own choice.

Usage

listports.h header provides the following C language definitions:

typedef struct
{
  LPCTSTR lpPortName;
  LPCTSTR lpFriendlyName;
}LISTPORTS_PORTINFO;

typedef BOOL (CALLBACK* LISTPORTS_CALLBACK)(LPVOID              lpCallbackValue,
                                            LISTPORTS_PORTINFO* lpPortInfo);

BOOL ListPorts(LISTPORTS_CALLBACK lpCallback,LPVOID lpCallbackValue);

LISTPORTS_PORTINFO holds information about a particular serial port: lpPortName holds the typical "COMn" string with which one can get a handle to the port via CreateFile(), whereas lpFriendlyName holds a fuller description of the port (vg., "Infrared Communications Port (COM4)").

LISTPORTS_CALLBACK defines a user-supplied callback routine that is provided the information on each serial port available on the system on succesive calls from ListPorts. Do in your callback whatever that suits your needs: dump the information on to the console, plug it into a listbox or store it for later use. Please note, however, that the strings stored on the LISTPORTS_PORTINFO are not to be referenced after the callbacks returns: so, if you plan on storing the values you should make private copies of them instead.

If your callback returns FALSE, the enumeration is aborted.

ListPorts accepts an additional parameter named lpCallbackValue. This is treated opaquely by the library and passed to your callback, so that you can use it for your particular purposes (telling between different invocations to ListPorts or storing a pointer to some object responsible of using the results, for instance). This is a standard technique used in many callback-based APIs, anyway.

Please see the demo project for a particularly simple example of use of the library.

Cross-platform issues

ListPorts works on the following OSs:

The functionality is less rich on Windows NT 4.0, however. On this plaftorm, I haven't been able to locate the description text for the serial ports (actually I suspect NT 4.0 does not have these). Instead, the bare "COMn" strings are supplied.

I don't know if this library works well on Windows XP. If someone out there makes the test, please let me know.

Unicode

The code compiles and works just fine in Unicode, with macros UNICODE and _UNICODE defined.

Technical

If you don't have curiosity about the internals of the library, skip this section.

ListPorts finds the information on the serial ports available on the system by scanning the registry. On Windows 9x platforms, information on installed devices is stored under the HKEY_LOCAL_MACHINE\Enum key. The enumeration tree has three levels, and devices are described at the deepest one. For instance, a standard UART serial port built into the motherboard and recognized by the BIOS could be stored like this:

HKLM\ENUM
  |-BIOS
    |-*PNP0501
      |-0D (or any other value, this is not important for us)
        · CLASS=        "Ports"
        · PORTNAME=     "COM1"
        · FRIENDLYNAME= "Communications Port (COM1)"

The value CLASS identifies the type of device and is used by ListPorts to pinpoint the communications ports.

On Windows 2000, the situation is very similar, except that the enumeration tree is located at HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum, and CLASS is deprecated in favor of a CLASSGUID identifier based on COM unique identifiers.

Windows NT 4.0 lacks a fully developed device enumeration tree. Information on serial ports can be found at HKEY_LOCAL_MACHINE\Hardware\DEVICEMAP\SERIALCOMM, but no FRIENDLYNAME is provided here.

The method used by the library is discussed in greater detail at the comments of the code in listports.c file.

[Top] Sign in to vote for this article:     PoorExcellent  
MSDN Magazine - Your guide to Microsoft tools, development environments, and technologies for Windows and the Web.
Premium Sponsor

View our advertisersUltimate Toolbox is the must have tool for MFC Developers with hundreds of MFC extensions.Advertise on the CodeProject
Hint: For a faster board use IE 4+, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
 Search (exact phrase)
 View   Per page   Messages since
New threadMsgs 1 to 3 of 3 (Total: 3)[First] [Prev] [Next] [Last]
Subject 
Author 
Date 
  Not so good...
 Christian B 7:21 25 Jul '01 
  Not so good...
 Christian B 7:21 25 Jul '01 
  Re: Not so good...
 Joaquín M López Muñoz 8:29 25 Jul '01 
Last Visit: 12:00 Friday 1st January, 1999[First] [Prev] [Next] [Last]
Home >> System >> General
Updated: 17 Jul 2001
Editor: Chris Maunder
Article content copyright Joaquín M López Muñoz, 2001
everything else © CodeProject, 1999-2002.
Advertise on The Code Project

DevelopersDexDevGuruProgrammers HeavenTek-Tips ForumsTopXMLVisualBuilder.comW3SchoolsXMLPitstopZVONSearch all Partners