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

Serial Port Enumeration on Windows Mobile

0.00/5 (No votes)
28 Nov 2007 1  
How to enumerate serial ports in a Windows Mobile device (including the virtual ports created by the Microsoft Bluetooth stack).

Screenshot - WMEnumComPorts.png

Introduction

This short article describes a serial port enumeration method for Windows Mobile devices that includes the Microsoft Bluetooth stack virtual COM ports.

Background

I was recently asked to write a little user interface that would enumerate all COM ports on Windows Mobile devices and that, if possible, provided a brief description of the attached device. After looking around CodeProject for a solution, I found a very promising approach in JoaquĆ­n's article. I followed a similar approach by enumerating all the keys under HKEY_LOCAL_MACHINE\Drivers\Active but found a big problem: on devices with the Microsoft Bluetooth stack, the virtual COM ports are not listed under this key. Interestingly, the Widcomm Bluetooth stack does seem to get this right.

The image above shows two Windows Mobile devices running the sample application. On the right, you see a screen capture of a WM6 Asus P535 with the Widcomm Bluetooth stack. All the enumerated ports were drawn from the active drivers registry key as I stated above. On the left, you see a WM5 i-mate K-Jam device with the Microsoft Bluetooth stack. In order to retrieve the information about the COM6: port, I had to look elsewhere in the registry, namely under the Software\Microsoft\Bluetooth\Serial\Ports key.

Using the code

Using the serial port enumeration code is quite simple. Start by declaring an object of type CSerialPortEnum:

CSerialPortEnum portEnum;

Now, we are ready to enumerate the serial ports:

size_t i;
int    iItem = 0;

for(i = 0; i < portEnum.GetCount(); ++i)
{
    const CSerialPortInfo *pInfo = portEnum[i];

    if(pInfo != NULL)
    {
        iItem = listPort.AddItem(iItem, 0, pInfo->GetPortName());
        listPort.SetItemText(iItem, 1, pInfo->GetDescription());
    }
}

Please note that the enumeration object does not sort the list by COM port number. If you need to reuse the object to enumerate the serial ports again, just call the EnumeratePorts function.

Also note that this code has only been tested on Windows Mobile 5 and 6 devices (Pocket PC).

History

  • 2007-11-29 - First publication on CodeProject.

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