Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Enumerating Serial Ports On Pocket PC

0.00/5 (No votes)
10 Dec 2005 1  
C++ Class to enumerate COM ports on Pocket PC
Sample Image - COMEnum.gif

Introduction

Getting information about available ports on Pocket PC device is troublesome due to the fact that Pocket PC registry is different from the ones found in PC operating systems. Information on how to do it in Pocket PC is scarce and quite often does not work. For instance, code posted here recommends to detect IrDA port by looking it up in "HKEY_LOCAL_MACHINE\\Comm\\IrDA" which does not work on Pocket PC 2002 and Pocket PC 2003.

This article introduces CCOMEnum class as an attempt to overcome these issues.

Background

CCOMEnum class finds all available COM ports by parsing "HKEY_LOCAL_MACHINE\\Drivers\\Active" content. All found ports and related data are stored in the structure. Class offers an option of qualifying found ports by obtaining a handle. Qualifying will reduce the number of found ports. Why not do qualifying always? One example - USB port. It is a slave on Pocket PC. Therefore it will not show up when qualify is enabled.

Using the Code

COM Port enumeration is done by calling EnumerateCOMs() function:

virtual void EnumerateCOMs(BOOL Verify);

Verify parameter will control whether or not to verify port by obtaining a handle to it. Once EnumerateCOMs() is called, other member functions can be used to view the data. GetTotalComs() will return number of ports found. This number can be used in for loops or if statements when reading data from CCOMEnum class:

#include "CCOMEnum.h"

CCOMEnum    Enumerator;
UINT        i;
CString    portName;
CString    portKey;
int        portIndex;

Enumerator.EnumerateCOMs(FALSE);

i = 2;

for(i=0; i < Enumerator.GetTotalComs(); i++)
{
    if(Enumerator.GetPortSimpleKey(i) == "Serial2")
    {
        portName     = Enumerator.GetPortName(i);    // "COM2:"
        portKey        = Enumerator.GetPortKey(i);    // 
        portIndex    = Enumerator.GetPortIndex(i);    // Such as 2
    }
}

GetPortSimpleKey() and GetPortKey() return CString value that can be used to sort ports out by functionality.

History

The current version is 1.0. Although this code will be periodically updated here, for the latest code please check developer's web site http://www.advatronix.com/. There, you can also download a pre-built executable.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here