B4J Question Get PID and VID information from COM Port (SOLVED)

walterf25

Expert
Licensed User
Longtime User
Hi all, I've searched all over the forums but was not able to find anything definitive, I have the need to retrieve the Vendor ID and Product ID information from a COMPort, does anyone have any suggestions or ideas on how to accomplish this, I know the Jserial library does not provide this method, I've also looked at some other possibilities but I would like to stay away from creating another library if possible.

FYI, I tried the B4R Serial Connector and although it works, it doesn't return a description for the specific Serial Port I am working with.

Just a little more info for context, the ComPort I am trying to use, uses a Qualcom Driver that puts the device in EDL mode (Emergency Download Mode), essentially what I'm working is a custom Android device that I am flashing through the USB Serial Port. The device can be placed in normal flashing mode or (Fastboot mode) but when that fails the device can be recovered by placing it in EDL Mode, when placed in EDL mode the ComPort enumerates just fine on my PC and has a VID = 0x05C6 and a PID of 0x9008, please check the image attached.

The B4R Serial Connectors picks up the ComPort but it doesn't show a description, I would like to find a better solution to check for that specific VID and PID so I can find the ComPort number and be able to use it with the application I am developing.

Thanks,
Walter
 

Attachments

  • comports.PNG
    comports.PNG
    5.2 KB · Views: 55
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I guess you need to find java-code to iterate through all Hardwarecomponents (maybe filteres to only com-ports) and to get such information.
I can´t remember having seen any such library.

Maybe
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Hi everyone, after spending some time researching the best way to come up with a solution, I came up with a very quick but very efficient solution, at least for my purposes.
There are a few ways this can be done, i.e. using JNI, and wrapping other libraries but I was really trying to stay away from that.

I solved my needs by using PowerShell and using the following:
powershell script:
Get-CimInstance -Class Win32_SerialPort|Select-Object Name, Description, PNPDeviceId
This will give list all the serial ports including com port number along with the VID and PID of each device.
Capture.PNG


If you want to look for a specific VID and PID device you can also do so with the following command.
Retrieve COM Port:
(Get-WmiObject -Class Win32_PnPEntity | Where-Object {$_.PNPDeviceID -Match 'VID_05C6&PID_9008'} | Select-Object Name | Select-String -Pattern 'COM\d+').Matches.Value

I am using this using Jshell on B4J.
Find Com Ports:
Public Sub FindEDLDevices As ResumableSub
    Dim comList As List
    comList.Initialize
    Dim pwshl As Shell
    pwshl.InitializeDoNotHandleQuotes("pwrshl", "powershell.exe", Array("-Command", "(Get-WmiObject -Class Win32_PnPEntity | Where-Object {$_.PNPDeviceID -Match 'VID_05C6&PID_9008'} | Select-Object Name | Select-String -Pattern 'COM\d+').Matches.Value"))
    '''pwshl.InitializeDoNotHandleQuotes("pwrshl", "powershell.exe", Array("-Command", "(Get-WmiObject -Class Win32_PnPEntity | Where-Object {$_.PNPDeviceID -Match 'VID_0D28&PID_0204'} | Select-Object Name | Select-String -Pattern 'COM\d+').Matches.Value"))
    pwshl.Run(-1)
    wait for (pwshl) pwrshl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log("stdout: " & StdOut)
    Dim comports() As String = Regex.Split(Chr(10), StdOut.Trim)
    If comports.Length > 0 Then
        For Each com As String In comports
            comList.Add(com.Trim)
        Next
    Else
        '''Return StdOut.Trim
        comList.Add(StdOut.Trim)
    End If
    Return comList
End Sub

Hope this helps you guys if you guys ever have the need to find a com port based on VID and PID values.

Regards,
Waler
 
Last edited:
Upvote 0
Top