Question

I am writing a simple code to find the USB to serial port in Windows platform, if the port is what I want (can be filt by VID/PID number), then the program will open the port.

I use MonkeyBread plugin, WinUSBDeviceMBS, with property of VendorID and ProductID, I can select specific USB port. sample code as following.

Dim devices() As WinUSBDeviceMBS = WinUSBDeviceMBS.devices
For Each d As WinUSBDeviceMBS in devices
    msgbox d.vendor+"-" + str(Hex(d.VendorID),"0000") + " " +d.product+"-" + str(Hex(d.ProductID),"0000")+d.serialnumber
Next
'this will give you a message box with "FTDI-0403 FT232R USB UART-6001 A60251HV"

Also, with help of Serial.serialport.Name, I can get the COM port name for serial device. dim i, count as Integer count = System.SerialPortCount

for i = 0 to count - 1
    Msgbox System.SerialPort( i ).Name
next 
'this will popup msgbox with "COM1" or "COM3"... all the valid port number, but no vendor info or product info

But I can't find a method to match those two together. Any ideas?

Was it helpful?

Solution

Assuming you are only using Windows you will need to use the registry to get this information.

You can get a list of all COM ports on the system here: HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM. Values will be listed according to their type, so a real serial port will show up with a name \Device\Serialn and data COMxx, other devices might be different. If you look here you can find a name you can use to filter which type of device a COM port belongs to, then simply list these in your application.

You can do this in Xojo by using the Declare statement to hook into the API of the Windows DLLs: https://docs.xojo.com/index.php/Declare

Here is a document on the Registry API functions, they will be used as described here, but using the Declare statement mentioned above: http://support.microsoft.com/kb/145679

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top