none
Enumerate serial port devices

    Question

  • Hi,

    I am trying to get a list of all available serial port devices. The only thing I could get so far is just a list of available COM ports (SerialPort class).
    Additionally I would like to get more detailed information about each device, to filter virtual COM ports and distinguish between USB and Bluetooth devices.

    I am using Visual Studio 2005, C#, .Net 2.0

    Thank you.
    Friday, February 06, 2009 11:55 PM

Answers

  • Use WMI to query Win32_SerialPort.  Good Google search terms.
    Hans Passant.
    • Marked as answer by Untit1ed Saturday, February 07, 2009 5:24 AM
    Saturday, February 07, 2009 1:12 AM
  • Whenever I see "WMI", I run quickly, in the opposite direction.

    Here's a discussion on enumerating serial ports:
    http://forum.sysinternals.com/forum_posts.asp?TID=4544

    An example class is linked there:
    http://www.naughter.com/enumser.html
    • Marked as answer by Untit1ed Tuesday, February 24, 2009 3:56 AM
    Monday, February 23, 2009 11:32 PM

All replies

  • Use WMI to query Win32_SerialPort.  Good Google search terms.
    Hans Passant.
    • Marked as answer by Untit1ed Saturday, February 07, 2009 5:24 AM
    Saturday, February 07, 2009 1:12 AM
  • nobugz said:

    Use WMI to query Win32_SerialPort.  Good Google search terms.


    Hans Passant.


    Thanks, I found it, unfortunately it doesn't show virtual serial ports.
    I have 2 devices connected via USB to my PC. And only one of them has 2 real COM ports, the other one has 2 virtual ports (I haven't realized it until now). I am able to see it under port section in Windows Device Manager. So I guess I really need to know how to list virtual ports now =)

    Thank you.
    Saturday, February 07, 2009 1:21 AM
  • Whenever I see "WMI", I run quickly, in the opposite direction.

    Here's a discussion on enumerating serial ports:
    http://forum.sysinternals.com/forum_posts.asp?TID=4544

    An example class is linked there:
    http://www.naughter.com/enumser.html
    • Marked as answer by Untit1ed Tuesday, February 24, 2009 3:56 AM
    Monday, February 23, 2009 11:32 PM
  • Thanks for the example, I'll keep it in mind for future. But I feel more comfortable with WMI now(Win32_SerialPort and Win32_PnPEntity solved my problem perfectly). The example that you provided is confusing to me and it is more than thousand lines of code long, WMI just takes about 10-15 lines.
    Is there any particular reason you avoid using WMI?
    Tuesday, February 24, 2009 3:56 AM
  • WMI is bulky and slow, very slow.  Upon first use it can take 10-30 seconds to load and get itself ready.  If performance isn't an issue, WMI is easier, that's the tradeoff.

    .NET has an easier facility for enumerating serial ports.  If you don't mind using C#, try this:
    string[] SerialPortEnums = System.IO.Ports.SerialPort.GetPortNames();

    Tuesday, February 24, 2009 10:17 PM
  • Actually it works pretty fast for me. I don't see any delays, it shows me my devices as soon as I plug them in.

    And I wrote about SerialPort class in my first comment to this thread, the class doesn't provide any information about COM ports, it just lists available non-virtual COM numbers.
    Tuesday, February 24, 2009 10:30 PM