Share via


VB adds two COM ports for a bluetooth device. Which one do I use?

Question

Thursday, August 20, 2020 8:20 PM

VB 2015

I am using My.Computer.Ports.SerialPortNames to show the serial ports in my application.

After pairing a Bluetooth device, I see two new COM ports.  On a laptop they are: COM5 and COM6.  On a desktop they are COM11 and COM12.

On the laptop, when selected COM5 works and COM6 hangs the app.  On the desktop COM11 hangs the app and COM12 works.

I tried putting a Try/Catch around the port.open but the app still hangs.

1) Is there any way to detect which of the two Bluetooth port is the correct one to use and if so, please give me an example.

2) If not, how do I prevent my app from hanging if a user tries to use the wrong port?

Thanks,

Ken

All replies (6)

Friday, August 21, 2020 4:46 PM ✅Answered

I gave you the solution, as painful as it is.  When you pick the correct device does it transmit data without user input?  Can you give specifics about the device you are trying to communicate with over BlueTooth serial please?

Search Documentation

SerialPort Info

Multics - An OS ahead of its time.

 "Those who use Application.DoEvents have no idea what it does

    and those who know what it does never use it."    former MSDN User JohnWein


Friday, August 21, 2020 7:35 AM

Hi bwanaken,

Thank you for posting here.

You can consider using following code to find a list with the port name and port description.

        Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity")
        For Each queryObj As ManagementObject In searcher.Get()
            If InStr(queryObj("Caption"), "(COM") > 0 Then
                Console.WriteLine("serial port : {0}", queryObj("Caption"))
            End If
        Next

Then see if any of them are for your device.

Besides, hope following reference could be helpful.

Change COM port via registry, command line or software?

Best Regards,

Xingyu Zhao

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Friday, August 21, 2020 1:08 PM

I have seen Bluetooth devices that have two ports.  What I've done in the past is to open one and send command if needed and wait for response,  OR just wait for data if no command is required.  If that doesn't work after a specified amount of time, try the other.

Search Documentation

SerialPort Info

Multics - An OS ahead of its time.

 "Those who use Application.DoEvents have no idea what it does

    and those who know what it does never use it."    former MSDN User JohnWein


Friday, August 21, 2020 2:33 PM

See my answer at Widcomm bluetooth : how to open the virtual COM for my understanding of the licence: using the binary version is free for commercial use. And, also that I'm maintainer of the library.

So a brief slight digression. I'm not a big fan of virtual COM ports. It always seems much easier to use a direct 'sockets' connection, rather than attempt to setup a COM port, and try to find what name it was created as (see below!), and then have to open a SerialPort to use it, and then if the connection is lost one doesn't know and have simply to keep retrying... With the library its so much easier to just to create and use that direct Bluetooth connection!

However you may want a solution to your current task at the moment. :-) So, use WMI to find the current COM ports in place and see if any of them are for your device. For example in PowerShell:

C:\ Get-WmiObject -query "select DeviceID,PNPDeviceID from Win32_SerialPort"
...
...
DeviceID         : COM66
PNPDeviceID      : BTHENUM\00001101-0000-1000-8000-00805F9B34FB}\7&1D80ECD3&0&00803A686519_C00000003
In that big long string one sees the address of the target device: 00803A686519. One can use WMI from .NET, run that query, filter the ones with "BTHENUM", and then parse out the address.

If you the do need to create a new Bluetooth virtual COM port, use 32feet.NET's BluetoothDeviceInfo.SetServiceState(BluetoothService.SerialPort) API. See the "Bluetooth Serial Ports" section in the User Guide e.g. at http://www.alanjmcf.me.uk/comms/bluetooth/32feet.NET%20--%20User%20Guide.html, and the class documentation in the release.

Unfortunately the native Win32 API we call does not tell what name of COM port it created! :-( So run the WMI query before and after the call to see what new name appeared (or use System.IO.Ports.SerialPort.GetPortNames as its simpler).

That's all specific to the Microsoft Bluetooth stack. I haven't investigated how other stacks behave in this regard. After a brief check Widcomm's serial ports appear in SerialPort.GetPortNames but not in the WMI query...


Friday, August 21, 2020 3:38 PM

Thanks for those who took the time to respond.

A little more clarification.

On my laptop, using VB, I find two Bluetooth serial ports COM5 and COM6.  I can communicate with the Bluetooth device on COM5 but when I open COM6, even though the open is surrounded by a try/catch, opening the wrong com port hangs the app. Only ctrl_alt_delete closes the app.

On my desktop, using VB, I find two Bluetooth serial ports COM11 and COM12.  I can communicate with the Bluetooth device on COM12 but when I open COM11, even though the open is surrounded by a try/catch, opening the wrong com port hangs the app. Only ctrl_alt_delete closes the app.

In summary:  Using VB, I can find two COM ports relating to the same Bluetooth device.  On one computer the working Bluetooth serial port is the first one found.  On another computer, the working Bluetooth is the second one found so I can't just pick the first one found.  Despite the following code, opening the wrong port hangs the app.

Try

If SerialPort1.IsOpen = False Then SerialPort1.Open()

Catch ex As Exception

MsgBox("Can't open " & SerialPort1.PortName)

       Exit Sub

End Try

Does anyone have a solution to determine which of the two Bluetooth serial ports found is the correct one to use without hanging the app?

Thanks, Ken


Friday, August 21, 2020 5:58 PM

Thanks for the nudge!

After debugging the code a little further, it turns out that I can open either port but attempting to write to the wrong port hangs.  The solution was to add SerialPort1.WriteTimeout=500 to allow for a graceful exit without hanging.

Thanks for everyone's help.

Ken