question

KenStewartSmith-7697 avatar image
0 Votes"
KenStewartSmith-7697 asked KenStewartSmith-7697 commented

VB GetObject() hands if Bluetooth printer port paired but off or out of range

Issue: VB GetObject() hangs if Bluetooth printer paired but off or out of range

Win10
Visual Studio 2005 net 2 or
Visual Studio 2019 (latest update) net 4
Visual Basic

I am using the following code to find the USB serial port for an attached device based on PID and VID.

For Each objItem In GetObject("winmgmts:\.\root\cimv2").ExecQuery("Select * from Win32_SerialPort")
If InStr(objItem.PNPDeviceID, "VID_04D8&" & pid) <> 0 Then
FoundComPort = objItem.DeviceID
Exit For
End If
Next

The code works perfectly without a Bluetooth printer installed.

When a Bluetooth printer is installed and paired, it creates two COM ports as seen in device manager.

226257-image.png

Note: If you try to remove the above, it will let you but next time you check Device Manager, they are automatically put back in. To remove a Bluetooth Printer, you must go into Win Settings, Bluetooth and remove the device from there.

1) When the Bluetooth printer is installed, paired and ON, the code works but is slow to respond.

2) When the Bluetooth printer is installed, paired but OFF or out of range, the GetObject() code hangs.

I assume the reason it hangs is due to the two visible ports showing up in the list of COM ports. For the unresponsive port (COM6) in my case, GetObject() is unable to retrieve the PID or VID.

Similarly for the other port (COM15), when the printer is OFF, no PID or VID info is returned and the GetObject() function hangs i.e. does not handle the error properly.

QUESTION

Other than forcing the client to uninstall and re-install Bluetooth printers each time they want to run the application, is there a better alternative to GetObject() or another way to finding which COM port a device with a specific PID and VID is attached to that is more Bluetooth Printer friendly?

Thanks,

Ken



dotnet-visual-basic
image.png (49.1 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

is there a better alternative to GetObject() or another way to finding which COM port a device with a specific PID and VID is attached to

If you want to get COMx from PNPDeviceID, you can use SetupDI APIs (SetupDiGetClassDevs with GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR for example, SetupDiEnumDeviceInfo, and so on...)






0 Votes 0 ·

Castorix31,

Thanks for the reply. I was not familiar with SetupDI APIs so I searched the net and found what I thought was a good example that had the following:

comPort = GetCOMPortNumberForUSBDevice("0403", "6001")

Although it does appear to work, and finds COM ports, it does not find all the COM ports that are available and does not associate the correct VID and PID. In addition, I had to set the VB advanced compile options to X86 to get it to sort of work.

Here is a code snipet:

 <DllImport("setupapi.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
     Public Shared Function SetupDiGetDeviceInterfaceDetail( _
     ByVal deviceInfoSet As IntPtr, _
     ByRef deviceInterfaceData As DeviceInterfaceData, _
     ByVal deviceInterfaceDetailData As IntPtr, _
     ByVal deviceInterfaceDetailDataSize As Integer, _
     ByRef requiredSize As Integer, _
     ByRef deviceInfoData As DevinfoData) As Boolean
     End Function

Since I have a 64-bit machine and I had to set the compile to x86 to get it to work, I am guessing that may be the reason that it is not working properly.

Can you please provide me with a VB SetupDI API code example for for a 64-bit machine that will get me the COM port for a specific USB PID VID device.

I can also post the code I am using if it would be helpful but it is fairly long.

Thanks,

Ken

0 Votes 0 ·

0 Answers