Click here to Skip to main content
15,894,343 members
Articles / Desktop Programming / Win32

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 2009CPOL3 min read 211.6K   7.8K   83   19
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.

C#
//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)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIts not a clear picture of concept Pin
koool.kabeer8-May-13 23:36
koool.kabeer8-May-13 23:36 
GeneralMy vote of 5 Pin
silvio pontes29-Aug-12 14:22
silvio pontes29-Aug-12 14:22 
GeneralThanks Pin
LordHagen2121-Aug-12 15:00
LordHagen2121-Aug-12 15:00 
GeneralserialPort.Open() IO Exception Pin
Sri Swamy21-Jan-10 3:39
Sri Swamy21-Jan-10 3:39 
GeneralWindows7-Problem [modified] Pin
aquilaii12-Jan-10 3:38
aquilaii12-Jan-10 3:38 
GeneralRe: Windows7-Problem [modified] Pin
marek_janek6-Oct-11 2:10
marek_janek6-Oct-11 2:10 
Generalvirtual serial ports Pin
imehlhorn13-Jan-09 23:19
imehlhorn13-Jan-09 23:19 
AnswerRe: virtual serial ports Pin
surf uk14-Jan-09 12:38
surf uk14-Jan-09 12:38 
GeneralRe: virtual serial ports Pin
imehlhorn14-Jan-09 20:55
imehlhorn14-Jan-09 20:55 
GeneralRe: virtual serial ports Pin
marek_janek6-Oct-11 2:09
marek_janek6-Oct-11 2:09 
GeneralGood start... Pin
Dave Kreskowiak8-Jan-09 8:50
mveDave Kreskowiak8-Jan-09 8:50 
GeneralRe: Good start... Pin
surf uk8-Jan-09 12:19
surf uk8-Jan-09 12:19 
GeneralWMI Errors Pin
sam.hill7-Jan-09 16:28
sam.hill7-Jan-09 16:28 
AnswerRe: WMI Errors Pin
surf uk7-Jan-09 22:34
surf uk7-Jan-09 22:34 
GeneralRe: WMI Errors Pin
sam.hill8-Jan-09 4:35
sam.hill8-Jan-09 4:35 
AnswerRe: WMI Errors Pin
surf uk8-Jan-09 11:56
surf uk8-Jan-09 11:56 
sam.hill wrote:
Sorry, I did not explain fully that I do indeed have functioning USB-to-serial converters attached when I get the "null" responses. These are FTDI based devices and have assigned Com Port numbers (via CDC). Maybe it works with some devices (Prolific) and not others (FTDI).
I also get the null responses with other devices that are present.
My conclusion (unfortunately) is that WMI is not a reliable means of polling for such devices.


Sorry I misinterpreted your question! I am a plonker, saves me right rushing an answer at work... never mind I'll try and make up for it.

Your absolutely right, I only used prolific dongles so I wouldn't have seen the error. I do however have a few FTDI dongles at work, and I will try them as soon as I get in tomorrow. When I found the code creator tool, I discovered a way of poking about the computer with a rough idea of where my answers might be. So that was my main point of the article, the serial finder is just an example, please don't let me put you off the the mighty WMI.

One of my references points out the Win32_SerialPort class which in my case didn't work with a prolific device. Here's a WMI tutorial[^]which shows this class and a good description of WMI. The name property may contain FTDI or the DeviceID may give away some clues that its a USB serial port.

AVR freaks use another WMI class called Win32_DeviceBus under the CIMV2 namespace to search for FTDI devices here.[^]

Maybe an intersection between all these lists and the .Net GetNames list would provide a more reliable method of picking out USB serial adaptors.

If you have given up on the WMI approach for serial then don't forget you can use the FTDI API to list all FTDI devices currently connected using FT_ListDevices.

Hope this helps, if I make any progress on detecting USB serial adaptors then I will update the article, of course any help is welcome Smile | :)

good luck

not suitable for idiots

GeneralRe: WMI Errors Pin
sam.hill8-Jan-09 16:57
sam.hill8-Jan-09 16:57 
AnswerRe: WMI Errors Pin
surf uk9-Jan-09 0:40
surf uk9-Jan-09 0:40 
GeneralGreat Idea! Pin
sam.hill7-Jan-09 15:43
sam.hill7-Jan-09 15: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.