Jump to content
Sign in to follow this  

Finding ComPorts by VID and PID

Recommended Posts

I have been using this function for years. I want to loop through all the ComPorts on a computer and return the ComPort of any port having VID 401 and PID 6003. This has worked forever but today I found a serious problem. I guess there is a "PortName" and a "FriendlyName" for each port. I have been incorrectly using the "FriendlyName" and need to start using the "PortName" because I guess there are certain instances where the "FriendlyName" doesn't really reflect the ""PortName". Here is the code. Can anyone tell me what I have to change to get the "PortName" and not the "FriendlyName"?

; Will scan all FTDI COMPorts in the WMI and return either the comport FTDI on or ""

Func FindOBD()
    Local $NumericPort, $Leftprens, $Rightprens, $TestDevice
    Local $oWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
    If @error Then Return SetError(@error, 0, "")
    Local $oItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%)'", "WQL", 48)
    $CommPort = ""
    For $oItem In $oItems
        $TestDevice = StringInStr($oItem.DeviceID, "FTDIBUS\VID_0403+PID_6001")
        If $TestDevice = 0 Then
        $Leftprens = StringInStr($oItem.Name, "(")
        $Rightprens = StringInStr($oItem.Name, ")")
        $CommPort = StringMid($oItem.Name, $Leftprens + 1, $Rightprens - $Leftprens - 1)
        $NumericPort = Int(StringRight($CommPort, StringLen($CommPort) - 3))
        If ($NumericPort > 9) Then
            $CommPort = "\\.\" & $CommPort
    Return $CommPort

Share this post

Link to post
Share on other sites

Maybe this will help


uint16   Availability;
  string   Caption;
  string   ClassGuid;
  string   CompatibleID[];
  uint32   ConfigManagerErrorCode;
  boolean  ConfigManagerUserConfig;
  string   CreationClassName;
  string   Description;
  string   DeviceID;
  boolean  ErrorCleared;
  string   ErrorDescription;
  string   HardwareID[];
  datetime InstallDate;
  uint32   LastErrorCode;
  string   Manufacturer;
  string   Name;
  string   PNPClass;
  string   PNPDeviceID;
  uint16   PowerManagementCapabilities[];
  boolean  PowerManagementSupported;
  boolean  Present;
  string   Service;
  string   Status;
  uint16   StatusInfo;
  string   SystemCreationClassName;
  string   SystemName;

Share this post

Link to post
Share on other sites

Maybe you can try this :

ConsoleWrite(   FindOBD("FTDIBUS\VID_0403+PID_60011")   )

Func FindOBD($sSearch)
    Local $aResult[1]
    Local $oWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
    If NOT IsObj($oWMIService) Then Return SetError(1, 0, 0)
    Local $oPnPDevices = $oWMIService.ExecQuery("select * from Win32_PnPDevice")
    For $oDevice In $oPnPDevices
        If StringInStr($oDevice.SystemElement, 'DeviceID="' & $sSearch & '"') Then
            $sPort = StringRegExpReplace( $oDevice.SameElement, '.+"([^"]+)"', "$1")
            Return $sPort
    Return ""

Share this post

Link to post
Share on other sites

Thank you both. @JohnOne I found that field list shortly after posting the original question and did a console write on every field. The actual Comport Number just isn't there. Only the "FriendlyName". Granted the Port Number and FiriendlyName are usually one in the same but in this case it isn't. Maybe I should start a new question and ask how to find all comports with a given pid and vid but not using the WMI services?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...