Determining What Serial Ports Are Available on a Windows Machine

原文地址::http://www.codeguru.com/cpp/w-p/system/hardwareinformation/article.php/c5721/Determining-What-Serial-Ports-Are-Available-on-a-Windows-Machine.htm



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.

Mobile Application Management Platform Evaluation Checklist
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:

 
 
  1. CString strDevPath; // Device path for use with CreateFile()
  2. CString strPortName; // Simple name (for example, COM1)
  3. CString strFriendlyName; // Full name to be displayed to a user
  4. BOOL bUsbDevice; // Provided through a USB connection?
  5. CString strPortDesc; // Friendly name without the COMx

example:

 
 
  1. CArray asi;
  2. EnumSerialPorts(asi,FALSE/*include all*/);

Downloads

Download demo project - 145 Kb





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值