Running the below code locks up my com port, and I would LOVE to know why. I have found a couple unanswered questions on SO that are similar...
I am using Bluetooth adapters (RN-41) to make a serial connection. The adapters SPP create two virtual com ports (This is common to all adapters we have tested). Windows labels them as Incoming and Outgoing, but the Incoming is the only one that actually carries serial data.
That leaves me with 'extra' virtual serial ports. I populate a dropdown with the available Com ports so the user can select the port to connect to from the list.
Prior to that, I am filtering that list to remove the 'fake' com ports. Here is my code:
public string checkBluetoothCom()
{
string bluetoothComm = "";
List<string> badComPorts = new List<string>();
ManagementClass processClass = new ManagementClass("WIN32_SerialPort");
ManagementObjectCollection Ports = processClass.GetInstances();
foreach (ManagementObject property in Ports)
{
if (property.GetPropertyValue("Name").ToString().Contains("Bluetooth"))
{
string hardwareAddress = property.GetPropertyValue("PNPDeviceID").ToString().Split('&')[4].Split('_')[0];
if (!hardwareAddress.All(c => c == '0'))
{
bluetoothComm = property.GetPropertyValue("DeviceID").ToString();
}
else
{
badComPorts.Add(property.GetPropertyValue("DeviceID").ToString());
}
}
}
Console.WriteLine("Removing {0} items from comm port list", badComPorts.Count);
ports.RemoveAll(item => badComPorts.Contains(item));
return bluetoothComm;
}
The problem is that after this runs my Com Port locks up and I cannnot make any connection until I remove my bluetooth adapter and restart the remote device (RN-41). Any thoughts?
win32_LogicalDisk
. You likely need to switch to an asynchronous WQL query. msdn.microsoft.com/en-us/library/cc143292.aspx – David-SkyMesh