Click here to Skip to main content
15,888,521 members
Articles / Desktop Programming / MFC
Article

Enumerating serial ports - W2K style.

Rate me:
Please Sign up or sign in to vote.
4.56/5 (9 votes)
20 Jul 20021 min read 215.3K   7.6K   37   31
Enumerating the serial ports using the SetupDi* API provided with Win2K and later

Introduction

You might think that determining which serial ports are present on a Windows PC would be an easy task. It seems like a reasonable enough thing for the OS to support. Unfortunately, there was no support for it at all (short of reading the registry yourself) before Win2K, and even then, the API is a bit cumbersome.

The attached serial port enumeration code first determines which operating system it is running under, and then runs the appropriate routine to enumerate the serial ports. In Win 9x (and Me) it uses the registry. In W2K and later it uses the SetupAPI that was included in that version of the WinSDK. It also has support for "brute force" enumeration of serial ports under NT4.

Unfortunately, I statically linked with setupapi.lib, so the provided executable won't actually run under 95 and nt4 (I didn't really need to support those OS's for my application.) This could be finagled by replacing the SetupDi* function calls with dynamic binding via LoadLibrary if needed.

To use the EnumSerial code, simply include EnumSerial.cpp and .h in your project, and link with setupapi.lib in the win32 sdk (this is under "additional dependencies" in the project link settings in Visual Studio).

All you have to do now is #include "EnumSerial.h" in your source code, allocate an empty CArray of SSerInfo structs, and make a call to EnumSerialPorts. It will populate your array with filled-out SSerInfo structs which contain the following information:

CString strDevPath;      // Device path for use with CreateFile()
CString strPortName;     // Simple name (i.e. COM1)
CString strFriendlyName; // Full name to be displayed to a user
BOOL bUsbDevice;         // Provided through a USB connection?
CString strPortDesc;     // friendly name without the COMx

Example

CArray<SSerInfo,SSerInfo&> asi;
EnumSerialPorts(asi,FALSE/*include all*/);

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


Written By
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

 
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pin
Taulie4-Nov-07 21:32
Taulie4-Nov-07 21:32 
GeneralData Structure Item Missing in EnumPortsWdm Pin
avidgator7-Aug-02 5:25
avidgator7-Aug-02 5:25 
GeneralRe: Data Structure Item Missing in EnumPortsWdm Pin
Zach Gorman10-Aug-02 7:36
Zach Gorman10-Aug-02 7:36 
GeneralRe: Data Structure Item Missing in EnumPortsWdm Pin
Christian B21-Apr-03 9:02
Christian B21-Apr-03 9:02 
GeneralRe: Data Structure Item Missing in EnumPortsWdm Pin
Member 74665210-Jun-04 3:40
Member 74665210-Jun-04 3:40 
GeneralToo complicated -> use GetDefaultCommConfig() Pin
Andre Gleichner22-Jul-02 2:17
Andre Gleichner22-Jul-02 2:17 
GeneralRe: Too complicated -> use GetDefaultCommConfig() Pin
Zach Gorman22-Jul-02 18:27
Zach Gorman22-Jul-02 18:27 
GeneralRe: Too complicated -> use GetDefaultCommConfig() Pin
Joerg Hoffmann24-Apr-03 21:46
Joerg Hoffmann24-Apr-03 21:46 
Zach Gorman wrote:
If all you need to know is which com #'s exist and whether or not they can be opened, your solution is indeed simpler and more elegant.

Another reason against this method is: it takes to much time (10ms per port).

It will be faster to open all ports with CreateFile(...) and if it fails look at the error code with GetLastError().

For example:

void EnumSerialPortsWithoutFriendlyNames( CArray<SSerInfo,SSerInfo&> &asi, BOOL bIgnoreInvalid )
{
  SSerInfo si;

  // open all possible ports
  for( int i=1; i <= 256; i++ )
  {
    si.strDevPath.Format( _T("\\\\.\\COM%d"), i );
    si.strPortName.Format( _T("COM%d"), i ); 
    si.strFriendlyName = si.strPortName;
    si.bIsInvalid = FALSE;
    si.dwError    = 0L;
    
    HANDLE hSerialPort = CreateFile( si.strDevPath, GENERIC_READ | GENERIC_WRITE, 
                                     0, NULL, OPEN_EXISTING, 0, NULL );
  
    if( hSerialPort == INVALID_HANDLE_VALUE ) 
    {
      // It can't be opened
  
      // Get Reason
      si.dwError = GetLastError();

      // mark it as invalid
      si.bIsInvalid = TRUE;

      // get a description only for debug
      // or possible add a string to SSerInfo-struct
      LPVOID lpMsgBuf;
      FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                     FORMAT_MESSAGE_FROM_SYSTEM | 
                     FORMAT_MESSAGE_IGNORE_INSERTS,
                     NULL,
                     si.dwError,
                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                     (LPTSTR) &lpMsgBuf,
                     0,
                     NULL 
                   );

      // Display the string.
      TRACE( "%s\n", (LPCTSTR)lpMsgBuf );

      // Free the buffer.
      LocalFree( lpMsgBuf );
    }
    else 
    {
      ::CloseHandle(hSerialPort);
    }

    if( (!bIgnoreInvalid && (ERROR_FILE_NOT_FOUND!=si.dwError)) 
        || 0==si.dwError )
    {
		    asi.Add(si);
    }
  }
}



Zach Gorman wrote:
If you would like to know the "friendly name" of the serial port, whether it is built-in or comes from a USB adapter, and the full WDM device name, I know of no better way than what I have. The friendly name is especially useful since this is the only piece of information you can get from a WM_DEVINFO message when a new serial port is connected.

Yes, it would be very nice to have information about the friendly names of the serial ports. But what's about virutal ports which have no friendly name and which will not be listet in the device manager?

Any idea?

I've tried this software from Wiesemann & Theis GmbH to add some virtual ports:

COM Port Redirector for Windows NT/2000/XP

COM port redirector for Win 95 / 98 / Me

The ports will not be found by using your function EnumPortsWdm().

BTW this ports was not found on Windows 98 until I add this to the EnumPortsW9x(...) function:

BOOL bMatch = ( strcmp(acSubSubEnum,"*PNP0500") == 0 ||
                strcmp(acSubSubEnum,"*PNP0501") == 0 ||
                strcmp(acSubSubEnum,"Ports"   ) == 0 ||  // Added for virtual ports 
                bUsbDevice
              );


I don't know if this will be a solution for all virtual ports but for me it works good.

Here is another sugestion to add support for enumerationg serial ports on NT4:
(without friendly names):

void EnumPortsWNt4(CArray<SSerInfo,SSerInfo&> &asi)
{
  //
  // HINT: On NT4 friendly names not existing
  //
  // Enumerating serial ports by searching in the registry key:
  // "HKLM\Hardware\DeviceMap\SerialComm"
  // 
	
  SSerInfo si;
  vector<string> vec;
  
  HKEY hKey;
  TCHAR caDevName[40], caPortName[20];
  DWORD dwDevNameSize, dwPortNameSize, dwType;
  int i;

  try
  {
    if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm", 0, KEY_READ,
            &hKey) == ERROR_SUCCESS) 
    {
      for( i = 0; TRUE; i++ )
      {
        dwDevNameSize = sizeof( caDevName );
        dwPortNameSize = sizeof( caPortName );


        if( RegEnumValue( hKey, i, caDevName, &dwDevNameSize, NULL, 
                          &dwType, (LPBYTE)caPortName, &dwPortNameSize ) != ERROR_SUCCESS )
        { 
          // no more entries found
          // quit the loop
          break;
        }
        
        // entry found add it to the array
        si.strDevPath  = CString("\\\\.\\") + CString(caPortName);
        si.strPortName = CString(caPortName);
        si.bIsBusy     = FALSE;
        si.dwError     = 0;
        asi.Add(si);
      }

      RegCloseKey( hKey );
    }
  }
  catch (CString strError) 
  {
    if (hKey != NULL)
      RegCloseKey(hKey);
    throw strError;
  }
}


Perhaps you can use this for future versions of your great code.Smile | :)

Have a nice weekend.

Joerg Hoffmann
GeneralRe: Too complicated -&gt; use GetDefaultCommConfig() Pin
Mike Pulice23-Dec-03 6:26
Mike Pulice23-Dec-03 6:26 
GeneralRe: Too complicated -&gt; use GetDefaultCommConfig() Pin
Zach Gorman4-Jan-04 14:23
Zach Gorman4-Jan-04 14:23 
GeneralRe: Too complicated -&gt; use GetDefaultCommConfig() [modified] Pin
CrashTest31-Aug-09 9:22
CrashTest31-Aug-09 9:22 

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.