Sign in to follow this  
Followers 0
dali4u

Get VID and PID from COM Ports

4 posts in this topic

#1 ·  Posted (edited)

Hi,

i'm not sure if it's possible. I want to get a list of all connected

devices (VendorID and ProductID). In this list, i want to search our

own VendorID and then I need to detect the COM Port (i mean the port

to which our product is connected)...

And hints for me?

Greetings

dali4u

Edited by dali4u

Share this post


Link to post
Share on other sites

I don't know if it is possible to get a list of all connected devices. You can get a list of all input devices, though.

#include <Array.au3>
#include <RawInput.au3>
#include <WinAPI.au3> ; For _WinAPI_GetLastError() and _WinAPI_GetLastErrorMessage() functions.

Dim $tRIDL, $pRIDL, $iRIDL
Dim $tBuff, $pBuff, $tszBuff, $pszBuff
Dim $iRet, $iNumDevices = 0, $iSize = 0
Dim $tagRIDL = ''
Dim $aDevices[1][2]
Dim $sKey = "HKLM\SYSTEM\CurrentControlSet\Enum"

$tRIDL = DllStructCreate($tagRAWINPUTDEVICELIST)
$iRIDL = DllStructGetSize($tRIDL)

Dim $iRet = _GetRawInputDeviceList(0, $iNumDevices, $iRIDL)

If Not @error And $iNumDevices > 0 Then
    ReDim $aDevices[$iNumDevices][2]

    For $i = 1 To $iNumDevices
        $tagRIDL &= StringRegExpReplace($tagRAWINPUTDEVICELIST, '(\w+);', '${1}' & $i & ';')
    Next

    $tBuff = DllStructCreate($tagRIDL)
    $pBuff = DllStructGetPtr($tBuff)

    $iRet = _GetRawInputDeviceList($pBuff, $iNumDevices, $iRIDL)
    For $i = 1 To $iNumDevices
        $aDevices[$i-1][0] = DllStructGetData($tBuff, 'hDevice' & $i)
        $aDevices[$i-1][1] = DllStructGetData($tBuff, 'dwType' & $i)
    Next
    _ArrayDisplay($aDevices)

    For $i = 0 To $iNumDevices-1
;~         $iRet = _GetRawInputDeviceInfo($aDevices[$i][0], $RIDI_DEVICENAME, '', $iSize)
;~         If $iSize > 0 Then
;~             $tszBuff = DllStructCreate('wchar[' & $iSize & ']')
;~             $pszBuff = DllStructGetPtr($tszBuff)
;~             $iRet = _GetRawInputDeviceInfo($aDevices[$i][0], $RIDI_DEVICENAME, $pszBuff, $iSize)
;~          $sInput = DllStructGetData($tszBuff, 1)
;~             ConsoleWrite($sInput & @LF)
;~          $sInput = StringRegExpReplace($sInput, "(?:\\\?\?\\|#)", "\\")
;~          $sInput = StringRegExpReplace($sInput, "\\[^\\]+$", "")
;~          ConsoleWrite(RegRead($sKey & $sInput, "HardwareID") & @CRLF)
;~         EndIf

        If $aDevices[$i][1] = $RIM_TYPEHID Then
            $tRIDI = DllStructCreate($tagRID_DEVICE_INFO_HID)
            $pRIDI = DllStructGetPtr($tRIDI)
            $iRIDI = DllStructGetSize($tRIDI)

            $iRet = _GetRawInputDeviceInfo($aDevices[$i][0], $RIDI_DEVICEINFO, $pRIDI, $iSize)
            ConsoleWrite("VendorId: " & DllStructGetData($tRIDI, "dwVendorId") & @TAB & _
                         "ProductId: " & DllStructGetData($tRIDI, "dwProductId") & @CRLF)
        EndIf
    Next

    $tszBuff = 0
    $tBuff = 0
EndIf

If the input device is an HID than you'll see the VID and PID of the input device. Otherwise, look for the VID and PID values (if present) in the registry. Take the commented-out code, for example.

Share this post


Link to post
Share on other sites

I don't know if it is possible to get a list of all connected devices. You can get a list of all input devices, though.

No, not all devices, only HID. Products that are connected to a

virtual COM Port are not detected. I found that script before.

It detects only at "HKLM\SYSTEM\CurrentControlSet\Enum\HID"

If the input device is an HID than you'll see the VID and PID of the input device. Otherwise, look for the VID and PID values (if present) in the registry. Take the commented-out code, for example.

Our product is not an HID, it is connected to COM7 (virtual com port driver) by USB.

I think i found a solution:

installed USB devices can be found at "HKLM\SYSTEM\CurrentControlSet\Enum\USB" and

they entrys contain also the COM Port. At "HKLM\SYSTEM\Hardware\Devicemap\Serialcom"

are all active com ports are listed. I think it is possible to compare these lists

to detect our own device.

dali4u

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You can use the following script to query devices recognized by MS-DOS:

#include <WinAPI.au3>

Local $tBuff = DllStructCreate("byte[8192]")
Local $pBuff = DllStructGetPtr($tBuff)

Local $iRet = _WinAPI_QueryDosDevice(0, $pBuff, 8192)

If $iRet = 0 Then Exit

Local $xBin = BinaryMid(DllStructGetData($tBuff, 1), 1, $iRet)
Local $aStrings = StringSplit(StringRegExpReplace($xBin, "((?:.{2})*?)00", "\1" & @LF & "0x"), @LF)

For $i = 1 To $aStrings[0]
    ConsoleWrite(BinaryToString($aStrings[$i]) & @CRLF)
Next


Func _WinAPI_QueryDosDevice($sDeviceName, $pTargetPath, $icchMax)
    Local $aResult, $sType = "str"

    If Not IsString($sDeviceName) Then $sType = "ptr"
    $aResult = DllCall("Kernel32.dll", "uint", "QueryDosDeviceA", $sType, $sDeviceName, "ptr", $pTargetPath, "uint", $icchMax)

    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[0]
EndFunc

I guess you'll need to dig in this page to get more information.

Edit: Code was incorrect.

Edited by Authenticity

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  
Followers 0