Determining What Serial Ports Are Available on a Windows Machine

Environment:

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 Windows 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 Windows 95 and NT4. (I didn't really need to support those OSs 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 (for example, 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 asi;
  EnumSerialPorts(asi,FALSE/*include all*/);

Downloads

Download demo project - 145 Kb



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds