Click here to Skip to main content
15,880,608 members
Articles / Mobile Apps / Windows Mobile

Enumerating Serial Ports On Pocket PC

Rate me:
Please Sign up or sign in to vote.
3.56/5 (4 votes)
10 Dec 2005CPOL1 min read 49.2K   623   28   5
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:

C++
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:

C++
#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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAnother way Pin
AndrewAZ5-Dec-06 22:33
AndrewAZ5-Dec-06 22:33 
QuestionRe: Another way Pin
markusschroth3-Jan-07 9:26
markusschroth3-Jan-07 9:26 
AnswerRe: Another way [modified] Pin
AndrewAZ8-Jan-07 22:22
AndrewAZ8-Jan-07 22:22 
QuestionUsage of CString::Format Pin
MechJayTi19-Sep-06 0:02
MechJayTi19-Sep-06 0:02 
QuestionAnother possibility Pin
Tony Kmoch12-Dec-05 23:30
Tony Kmoch12-Dec-05 23:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.