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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • VMware vCloud® Government Service provided by Carpathia® is an enterprise-class hybrid cloud service that delivers the tried and tested VMware capabilities widely used by government organizations today, with the added security and compliance assurance of FedRAMP authorization. The hybrid cloud is becoming more and more prevalent – in fact, nearly three-fourths of large enterprises expect to have hybrid deployments by 2015, according to a recent Gartner analyst report. Learn about the benefits of …

Most Popular Programming Stories

More for Developers

RSS Feeds