1

I'm making program to autodetect my Arduino using C# windows form application. For this I'm browsing through port names, assign them to serial port, write letter to port and wait if some port will response with another letter.

It all works fine until I run program on the computer where some COM ports are assigned to Bluetooth (Standard Serial over Bluetooth link). Looks like when I'm trying to .Write or .WriteLine to those ports it completely stops working.

If I manually delete those ports in Win through "Bluetooth --> more options" everything is ok.

So I tried to get around using stopwatch - give just some time to perform, but it is still not working:

`Stopwatch timer = new Stopwatch();
timer.Start();
textBox1.Text = "1";
while (timer.Elapsed.TotalSeconds < 2)
{
serialPort1.Write("q" + "\n"); //ping port
}
timer.Stop();`

What I'm doing wrong and how I can fix it?

Perhaps I can firstly find those ports and exclude them from ping?

I found this method, that looks for all ports:

`System.Management.ManagementObjectSearcher Searcher = new
System.Management.ManagementObjectSearcher("Select * from WIN32_SerialPort");
foreach (System.Management.ManagementObject Port in Searcher.Get())
{
    foreach (System.Management.PropertyData Property in Port.Properties)
{
    Console.WriteLine(Property.Name + " " + (Property.Value == null ? null :     
Property.Value.ToString()));
}
}`

Determine if serial port is normal COM or SPP

but how I can get Bluetooth ports names to be excluded?

N.B. I'm biotech guy who just needed pump for my experiment. Hardware switches sucks --> arduino --> windows form --> suddenly found myself in here. No software background at all.

Many thanks to Raynoceros!

SOLUTION:

  string[] ports = SerialPort.GetPortNames(); //This is all ports names  
    string[] btports = { }; // array for only bluetooth ports  
    // search for ports in ObjectSearher  
    System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_SerialPort");  
    //check if port belongs to bluetooth  
    foreach (System.Management.ManagementObject port in searcher.Get())  
    {
        string deviceId = port["PNPDeviceID"].ToString(); //get data from searcher  
        if (deviceId.Contains("BTHENUM")) //in string PNPDeviceID check for words BTHENUM (Bluetooth)  
        {
            string portName = port["DeviceID"].ToString(); // and if it have BTHENUM than take its port name  
            List<string> list = new List<string>(btports.ToList()); // and using string to create array  
            list.Add(portName);  
            btports = list.ToArray(); // make array that will contain port names that have BTHENUM in description  
        }  
    }  
    string[] coms = ports.Except(btports).ToArray(); // then extract BTHENUM ports from all ports   
    // now coms contains only names of ports not associated with bluetooth   
Tsoll
  • 21
  • 3

1 Answers1

0

You can exclude these Bluetooth serial ports from the detection process.

Check the PNPDeviceID property of each serial port that you detect. On Windows, the PNPDeviceID property of Bluetooth serial ports will contain the string BTHENUM, which you can use to exclude them from detection process.

System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_SerialPort");
foreach (System.Management.ManagementObject port in searcher.Get())
{
    string deviceId = port["PNPDeviceID"].ToString();
    if (!deviceId.Contains("BTHENUM"))
    {
        string portName = port["DeviceID"].ToString();
        Console.WriteLine(portName);
        // add the port name to your list of serial ports to test
    }
}

This will exclude any Bluetooth serial ports from the list of ports to test, and only return the names of the remaining serial ports.

Raynoceros
  • 386
  • 2
  • 15
  • Raynoceros, thank you very much! However when I try to add it, it says that it have issues with namespaces, .ManagementObjectSearcher is underlined. I have my native namespace, so and can not add one more... How I can do that? I understand that it might be too stupid question, but I'm really novice to topic. – Tsoll Feb 22 '23 at 03:43
  • @Tsoll you did reference `System.Management` assembly to your project right? – Raynoceros Feb 22 '23 at 03:50
  • Okay, now I have no namespace issue, thanks! Still not getting any port names, but let me try more... – Tsoll Feb 22 '23 at 04:40
  • For some reason only 2 ports are listed, while I have 3 active... and one that is not listed is actually Arduino Nano (clone?) that I'm using... https://i.stack.imgur.com/jCH4D.jpg – Tsoll Feb 22 '23 at 05:13
  • So when I'm using GetPortNames I have all 3 visible; ObjectSearcher finds only two, and device of interest not listed – Tsoll Feb 22 '23 at 05:49
  • It's possible that the `ManagementObjectSearcher` is not able to detect all the serial ports on your system. This can happen if the WMI service is not running or if there are issues with the system's WMI repository. @Tsoll – Raynoceros Feb 22 '23 at 05:50
  • C:\Windows\system32>net start winmgmt The requested service has already been started. So service is started, and I don't think I'm able to deal with issues in repository :) I think I will try to get Bluetooth PortNames from Searcher and then exclude them from GetPortNames array. – Tsoll Feb 22 '23 at 06:15
  • Raynoceros, thank you very much! I think it's done. – Tsoll Feb 22 '23 at 07:17