Click here to Skip to main content
Click here to Skip to main content

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

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

WMI-USB-SERIAL-FINDER.png

Introduction

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.

Background

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

WMICodeCreator

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:

usb-serial-adapter.jpg

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
try
{
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher("root\\WMI",
        "SELECT * FROM MSSerial_PortName");

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

        Console.WriteLine("-----------------------------------");
        Console.WriteLine("MSSerial_PortName instance");
        Console.WriteLine("-----------------------------------");
        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.

History

  • 7th January, 2009: Initial post

License

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

Share

About the Author

surf uk

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionIts not a clear picture of concept Pinmemberkoool.kabeer8-May-13 23:36 
GeneralMy vote of 5 Pinmembersilvio pontes29-Aug-12 14:22 
GeneralThanks PinmemberLordHagen2121-Aug-12 15:00 
GeneralserialPort.Open() IO Exception PinmemberSri Swamy21-Jan-10 3:39 
GeneralWindows7-Problem [modified] Pinmemberaquilaii12-Jan-10 3:38 
GeneralRe: Windows7-Problem [modified] Pinmembermarek_janek6-Oct-11 2:10 
Generalvirtual serial ports Pinmemberimehlhorn13-Jan-09 23:19 
AnswerRe: virtual serial ports Pinmembersurf uk14-Jan-09 12:38 
GeneralRe: virtual serial ports Pinmemberimehlhorn14-Jan-09 20:55 
GeneralRe: virtual serial ports Pinmembermarek_janek6-Oct-11 2:09 
GeneralGood start... PinmvpDave Kreskowiak8-Jan-09 8:50 
GeneralRe: Good start... Pinmembersurf uk8-Jan-09 12:19 
GeneralWMI Errors Pinmembersam.hill7-Jan-09 16:28 
AnswerRe: WMI Errors Pinmembersurf uk7-Jan-09 22:34 
GeneralRe: WMI Errors Pinmembersam.hill8-Jan-09 4:35 
AnswerRe: WMI Errors Pinmembersurf uk8-Jan-09 11:56 
GeneralRe: WMI Errors Pinmembersam.hill8-Jan-09 16:57 
AnswerRe: WMI Errors Pinmembersurf uk9-Jan-09 0:40 
GeneralGreat Idea! Pinmembersam.hill7-Jan-09 15:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141030.1 | Last Updated 7 Jan 2009
Article Copyright 2009 by surf uk
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid