Reputation: 91
In the following method I start a process for calling WMIC.exe in order to query the DeviceID of an attached usb device. The problem is that sometimes although the device is plugged WIMC returns no instance, as if the device is not plugged. Nevertheless at the same time the Device Manager shows the device under the "Ports(COM & LPT)" which means that WMIC's information is not accurate. I mean that if the device had crashed or in any way there was something malfunctioning with the device and it would need some kind of reset it should also not be in the device manager list.
The method:
private string DonglePortName()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Windows\\System32\\wbem\\WMIC.exe";
startInfo.Arguments = "PATH Win32_SerialPort GET /VALUE";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
processTemp.Start();
string output = processTemp.StandardOutput.ReadToEnd();
int indexOfBGEntry = output.IndexOf("Bluegiga Bluetooth Low Energy");
if(indexOfBGEntry > -1)
{
string output_sub = output.Substring(indexOfBGEntry);
string str = "DeviceID=";
int i = output_sub.IndexOf(str);
int start = i + str.Length;
string substr = output_sub.Substring(start);
int end = substr.IndexOf("\r");
return output_sub.Substring(start, end);
}
return null;
}
At that point executing on a cmd window:
C:\\Windows\\System32\\wbem\\WMIC.exe PATH Win32_SerialPort GET /VALUE
returns:
No Instance(s) Available.
when at the same time Device Manager shows the device (even after clicking scan for hardware changes).
So the C# code above is not actually needed to demonstrate the problem but I am only having it here in case someone can propose another, more robust, way of getting the device port (in C#) or could point out the reason for the inaccuracy of WMIC.
Another clue is that WMIC always returns No Instance(s) Available
after an Exception (and crash) on another part of the code that reads/writes to the SerialPort. I then have to unplug and replug the usb device and then it is "viewed" again by WMIC. Nevertheless, the Device Manager shows the device when it is plugged at all times.
Upvotes: 1
Views: 3282
Reputation: 30093
You are looking in a wrong WMIC
path, as the Win32_SerialPort
WMI
class represents a serial port on a computer system running Windows. In short, here is ProviderType
property (communications provider type) list:
FAX Device, LAT Protocol, Modem Device, Network Bridge, Parallel Port, RS232 Serial Port, RS422 Port, RS423 Port, RS449 Port, Scanner Device, TCP/IP TelNet, X.25, Unspecified
Inspect next command output rather:
wmic path CIM_LogicalDevice where "Description like 'USB%'" get /value
Resource: Computer System Hardware Classes
Upvotes: 1