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:
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.
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 (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