Click here to Skip to main content
14,743,899 members
Articles » Platforms, Frameworks & Libraries » .NET Framework » How To
Posted 7 Jan 2009


82 bookmarked

A Useful WMI Tool & How To Find USB to Serial Adaptors

Rate me:
Please Sign up or sign in to vote.
4.37/5 (18 votes)
7 Jan 2009CPOL
An article to illustrate a useful tool for WMI (WMICodeCreator) & using WMI to find USB Serial / COM port adaptors



This article has two main purposes:

  • To illustrate and advertise a useful tool for WMI (WMICodeCreator)
  • To demonstrate some WMI code to find USB-Serial/COM port adaptors on the system

However, it will not cover any ground on WMI, neither will it show you how to use the serial port. There are many useful articles already available for this. This article is also my first post to The Code Project which is my favourite resource for learning how to create cool new widgets, so I am trying to give something back to this great community.


I have a project which controls projectors to switch them on or off via the serial port (good old RS-232). The PC controlling the projector is located in a server room a long way from the projector. They are both linked by a KVM extender which squeezes a VGA signal and a USB signal over a single CAT5 Ethernet cable up to 120m away.

So I have to use a USB to Serial adaptor to talk to the projector. The problem with these adaptors is they are ‘virtual ports’ and they get assigned a random COM port name such as COM7. Also they sometimes have a tendency to wander and change their port name, requiring constant maintenance to locate the projector. I cannot rely on the .NET serial port to get names as it will return non USB COM ports as well. As there will only be one adaptor per PC, it makes sense to search for it using WMI.

Using the Code


I have used Windows Management Instrumentation previously to find MAC addresses and disk drives. To locate something in the WMI universe sounded daunting to me. Punching in all the keywords into Google didn't get me very far, until someone mentioned WMICodeCreator on the MSDN forums “Play with WMI using this tool” it said.

It can be used to search the namespaces, classes, properties, methods, qualifiers. You can query the database, receive an event or just browse the namespace. It even gives you descriptions where possible “The MaximumBaudRate property indicates the maximum...”. When you find what you are looking for, select the code language you want (e.g. C#) and it will generate the code needed for your query.

USB to Serial Adaptors

I have used serial adaptors in the past. I have always had problems. When plugging them in a different USB port, they change their name, e.g. from COM5 to COM6.

This is a typical adaptor prolific:


I have attached the source code for a VS2005 C# project called WMITestBed. It’s called this as I imagine I will need it to test more WMI code in the future, not just boring old serial ports. It has a working demonstration of a USB Serial finder using little more than the copy-pasted code from the WMI utility. Use this to get you started. It’s very easy to mess about with your own code here.

//Below is code pasted from WMICodeCreator
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher("root\\WMI",
        "SELECT * FROM MSSerial_PortName");

    foreach (ManagementObject queryObj in searcher.Get())
        Console.WriteLine("MSSerial_PortName instance");
        Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);

        Console.WriteLine("MSSerial_PortName instance");
        Console.WriteLine("PortName: {0}", queryObj["PortName"]);

        //If the serial port's instance name contains USB 
        //it must be a USB to serial device
        if (queryObj["InstanceName"].ToString().Contains("USB"))
            Console.WriteLine(queryObj["PortName"] + " 
			is a USB to SERIAL adapter/converter");
catch (ManagementException e)
    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);

Points of Interest

To output the results of the code, I have a Class called DBug. Just as the name suggests I use it for debugging. I find the Console in the IDE becomes far too messy when debugging multiple forms, so I created this. The output can be redirected to any form which makes it more flexible and easier to understand. I have overridden Console and WriteLine so that all the output goes to the form. If you get any ambiguous compile errors, simply comment out this class and watch your output window instead.

Don't Forget

If you create your own project, don't forget to add the reference to System.Managment or it won't compile.


  • 7th January, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

surf uk
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

QuestionIts not a clear picture of concept Pin
koool.kabeer9-May-13 0:36
Memberkoool.kabeer9-May-13 0:36 
GeneralMy vote of 5 Pin
silvio pontes29-Aug-12 15:22
Membersilvio pontes29-Aug-12 15:22 
GeneralThanks Pin
LordHagen2121-Aug-12 16:00
MemberLordHagen2121-Aug-12 16:00 
GeneralserialPort.Open() IO Exception Pin
Sri Swamy21-Jan-10 4:39
MemberSri Swamy21-Jan-10 4:39 
GeneralWindows7-Problem [modified] Pin
aquilaii12-Jan-10 4:38
Memberaquilaii12-Jan-10 4:38 
GeneralRe: Windows7-Problem [modified] Pin
marek_janek6-Oct-11 3:10
Membermarek_janek6-Oct-11 3:10 
Generalvirtual serial ports Pin
imehlhorn14-Jan-09 0:19
Memberimehlhorn14-Jan-09 0:19 
AnswerRe: virtual serial ports Pin
surf uk14-Jan-09 13:38
Membersurf uk14-Jan-09 13:38 
GeneralRe: virtual serial ports Pin
imehlhorn14-Jan-09 21:55
Memberimehlhorn14-Jan-09 21:55 
GeneralRe: virtual serial ports Pin
marek_janek6-Oct-11 3:09
Membermarek_janek6-Oct-11 3:09 
GeneralGood start... Pin
Dave Kreskowiak8-Jan-09 9:50
mveDave Kreskowiak8-Jan-09 9:50 
GeneralRe: Good start... Pin
surf uk8-Jan-09 13:19
Membersurf uk8-Jan-09 13:19 
GeneralWMI Errors Pin
sam.hill7-Jan-09 17:28
Membersam.hill7-Jan-09 17:28 
AnswerRe: WMI Errors Pin
surf uk7-Jan-09 23:34
Membersurf uk7-Jan-09 23:34 
GeneralRe: WMI Errors Pin
sam.hill8-Jan-09 5:35
Membersam.hill8-Jan-09 5:35 
AnswerRe: WMI Errors Pin
surf uk8-Jan-09 12:56
Membersurf uk8-Jan-09 12:56 
GeneralRe: WMI Errors Pin
sam.hill8-Jan-09 17:57
Membersam.hill8-Jan-09 17:57 
AnswerRe: WMI Errors Pin
surf uk9-Jan-09 1:40
Membersurf uk9-Jan-09 1:40 
GeneralGreat Idea! Pin
sam.hill7-Jan-09 16:43
Membersam.hill7-Jan-09 16:43 

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.