Jump to content

Please help me Example on using UDF _COM_ListPortDevices()


Recommended Posts

Hi Everyone,

I try to use UDF Serial from @mLipok and @Danyfirex to get serial device name in Device Manager, but I don't know how to use or display it.

My code

#include <ComUDF.au3>
#include <Array.au3>


Local $ports = _COM_ListPorts()                 ;an array with com ports
Local $portdevices = _COM_ListPortDevices()     ;2DArray DevicesName|COMPort

_ArrayDisplay($ports, "2D display transposed")
_ArrayDisplay($portdevices, "2D display transposed")

It only display $ports Array, nothing from $portdevices.

I want to show prolific ..... COM port name like in the picture.

Please could someone help me.

Screenshot_1.png

Link to post
Share on other sites

Hello @mnkt Are you sure the port is not in use?

Could you do a Reg Search inside this Registry Key? and check if you find your prolific device.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB

 

Saludos

Link to post
Share on other sites

@mnkt I meant you need to do a search inside all those keys, or try to do  a global search to check where are those devices names.  It's possible to get same those names using native device management API but in the background it use registry anyway so I would like to update the COMUDF to read from registry as It does currently.

Saludos

 

 

Link to post
Share on other sites
Link to post
Share on other sites

Could you run this script and paste the console output here. 

;~ #AutoIt3Wrapper_UseX64=y
#include <ComUDF.au3>
#include <Array.au3>


Local $ports = _COM_ListPorts()                 ;an array with com ports
Local $portdevices = _COM_ListPortDevices_()     ;2DArray DevicesName|COMPort

_ArrayDisplay($ports, "2D display transposed")
_ArrayDisplay($portdevices, "2D display transposed")



Func _COM_ListPortDevices_($bIsAvailable = False)
    Local $aCOMPorts = _COM_ListPorts()
    If Not IsArray($aCOMPorts) Then Return SetError(1, 0, 0)

    Local Const $sKeyUSB = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB"
    Local $iIndex = 1
    Local $iSubIndex = 1
    Local $sSubKey = ""
    Local $sSubKeyChild = ""
    Local $sFriendlyName = ""
    Local $sDriver = ""
    Local $bFoundAll = False
    Local $sCOMPort = ""
    Local $hCOMPort = ""
    Local $aCOMDevices[256][3]
    Local $iIndexDevices = 0

    While True
        $sSubKey = RegEnumKey($sKeyUSB, $iIndex)
        If @error Then ExitLoop
        $iIndex += 1
        $sSubKey = $sKeyUSB & "\" & $sSubKey
        $iSubIndex = 1
        ConsoleWrite(@CRLF & @CRLF & ">" & $sSubKey & @CRLF)
        While True
            $sSubKeyChild = RegEnumKey($sSubKey, $iSubIndex)
            If @error Then ExitLoop
            $iSubIndex += 1
            ConsoleWrite(">>" & $sSubKey & "\" & $sSubKeyChild & @CRLF)
            $sFriendlyName = RegRead($sSubKey & "\" & $sSubKeyChild, "FriendlyName")
            $sDriver = RegRead($sSubKey & "\" & $sSubKeyChild, "Driver")
            ConsoleWrite(">>>FriendlyName: " & $sFriendlyName & @TAB & "Driver: " & $sDriver & @CRLF)
            If $sFriendlyName = "" Then ContinueLoop
            $sCOMPort = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" & $sDriver, "AttachedTo")
            If $sCOMPort = "" Then $sCOMPort = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" & $sDriver, "AssignedPortForQCDevice")
            ConsoleWrite(">>>>AttachedTo: " & $sCOMPort & @CRLF)
            If $sCOMPort = "" Then ContinueLoop ; not Attached
            ;check if exist in $aCOMPorts and remove it
            For $x = 0 To UBound($aCOMPorts) - 1
                If $aCOMPorts[$x] = "" Then ContinueLoop
                If $aCOMPorts[$x] = $sCOMPort Then
                    $aCOMDevices[$iIndexDevices][0] = $sFriendlyName
                    $aCOMDevices[$iIndexDevices][1] = $sCOMPort
                    $aCOMDevices[$iIndexDevices][2] = False
                    If $bIsAvailable Then
                        $hCOMPort = _COM_OpenPort($sCOMPort)
                        $aCOMDevices[$iIndexDevices][2] = $hCOMPort <> -1
                        If $hCOMPort <> -1 Then _COM_ClosePort($hCOMPort)
                    EndIf
                    $iIndexDevices += 1
                    ExitLoop
                EndIf
            Next
            If UBound($aCOMPorts) = 0 Then $bFoundAll = True
            If $bFoundAll Then ExitLoop
        WEnd
        If $bFoundAll Then ExitLoop
    WEnd

    ReDim $aCOMDevices[$iIndexDevices][3]
    If $iIndexDevices Then Return SetError(0, $iIndexDevices, $aCOMDevices)
    Return SetError(2, 0, 0)
EndFunc   ;==>_COM_ListPortDevices_

 

Saludos

Link to post
Share on other sites
On 3/2/2021 at 7:43 PM, Danyfirex said:

Could you run this script and paste the console output here. 

;~ #AutoIt3Wrapper_UseX64=y
#include <ComUDF.au3>
#include <Array.au3>


Local $ports = _COM_ListPorts()                 ;an array with com ports
Local $portdevices = _COM_ListPortDevices_()     ;2DArray DevicesName|COMPort

_ArrayDisplay($ports, "2D display transposed")
_ArrayDisplay($portdevices, "2D display transposed")



Func _COM_ListPortDevices_($bIsAvailable = False)
    Local $aCOMPorts = _COM_ListPorts()
    If Not IsArray($aCOMPorts) Then Return SetError(1, 0, 0)

    Local Const $sKeyUSB = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB"
    Local $iIndex = 1
    Local $iSubIndex = 1
    Local $sSubKey = ""
    Local $sSubKeyChild = ""
    Local $sFriendlyName = ""
    Local $sDriver = ""
    Local $bFoundAll = False
    Local $sCOMPort = ""
    Local $hCOMPort = ""
    Local $aCOMDevices[256][3]
    Local $iIndexDevices = 0

    While True
        $sSubKey = RegEnumKey($sKeyUSB, $iIndex)
        If @error Then ExitLoop
        $iIndex += 1
        $sSubKey = $sKeyUSB & "\" & $sSubKey
        $iSubIndex = 1
        ConsoleWrite(@CRLF & @CRLF & ">" & $sSubKey & @CRLF)
        While True
            $sSubKeyChild = RegEnumKey($sSubKey, $iSubIndex)
            If @error Then ExitLoop
            $iSubIndex += 1
            ConsoleWrite(">>" & $sSubKey & "\" & $sSubKeyChild & @CRLF)
            $sFriendlyName = RegRead($sSubKey & "\" & $sSubKeyChild, "FriendlyName")
            $sDriver = RegRead($sSubKey & "\" & $sSubKeyChild, "Driver")
            ConsoleWrite(">>>FriendlyName: " & $sFriendlyName & @TAB & "Driver: " & $sDriver & @CRLF)
            If $sFriendlyName = "" Then ContinueLoop
            $sCOMPort = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" & $sDriver, "AttachedTo")
            If $sCOMPort = "" Then $sCOMPort = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" & $sDriver, "AssignedPortForQCDevice")
            ConsoleWrite(">>>>AttachedTo: " & $sCOMPort & @CRLF)
            If $sCOMPort = "" Then ContinueLoop ; not Attached
            ;check if exist in $aCOMPorts and remove it
            For $x = 0 To UBound($aCOMPorts) - 1
                If $aCOMPorts[$x] = "" Then ContinueLoop
                If $aCOMPorts[$x] = $sCOMPort Then
                    $aCOMDevices[$iIndexDevices][0] = $sFriendlyName
                    $aCOMDevices[$iIndexDevices][1] = $sCOMPort
                    $aCOMDevices[$iIndexDevices][2] = False
                    If $bIsAvailable Then
                        $hCOMPort = _COM_OpenPort($sCOMPort)
                        $aCOMDevices[$iIndexDevices][2] = $hCOMPort <> -1
                        If $hCOMPort <> -1 Then _COM_ClosePort($hCOMPort)
                    EndIf
                    $iIndexDevices += 1
                    ExitLoop
                EndIf
            Next
            If UBound($aCOMPorts) = 0 Then $bFoundAll = True
            If $bFoundAll Then ExitLoop
        WEnd
        If $bFoundAll Then ExitLoop
    WEnd

    ReDim $aCOMDevices[$iIndexDevices][3]
    If $iIndexDevices Then Return SetError(0, $iIndexDevices, $aCOMDevices)
    Return SetError(2, 0, 0)
EndFunc   ;==>_COM_ListPortDevices_

 

Saludos

Hi thanks for your reply, sorry for my late.

This is the output from your code

Screenshot_1.png

Link to post
Share on other sites
14 hours ago, Danyfirex said:

Screenshot_1.thumb.png.f75718043ca3e3e3048a2ce79c3c7aca.pngHello

 


Saludos

 

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\kazih\Desktop\testmod.au3" /UserParams    
+>15:45:23 Starting AutoIt3Wrapper (19.1127.1402.0} from:SciTE.exe (4.2.0.0)  Keyboard:00000409  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\kazih\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\kazih\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.5)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\kazih\Desktop\testmod.au3
+>15:45:23 AU3Check ended.rc:0
>Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\kazih\Desktop\testmod.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\ROOT_HUB30
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\ROOT_HUB30\4&6618f79&0&0
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0001


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_0000&PID_0002
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_0000&PID_0002\5&2b1d7621&0&6
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0008


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_046D&PID_C52B
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_046D&PID_C52B\5&2b1d7621&0&1
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0006


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_046D&PID_C52B&MI_00
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_046D&PID_C52B&MI_00\6&2473d6b0&0&0000
>>>FriendlyName:    Driver: {745a17a0-74d3-11d0-b6fe-00a0c90f57da}\0005


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_046D&PID_C52B&MI_01
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_046D&PID_C52B&MI_01\6&2473d6b0&0&0001
>>>FriendlyName:    Driver: {745a17a0-74d3-11d0-b6fe-00a0c90f57da}\0006


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_046D&PID_C52B&MI_02
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_046D&PID_C52B&MI_02\6&2473d6b0&0&0002
>>>FriendlyName:    Driver: {745a17a0-74d3-11d0-b6fe-00a0c90f57da}\0007


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_058F&PID_9254
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_058F&PID_9254\5&2b1d7621&0&6
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0002
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_058F&PID_9254\6&2d5dc42f&0&4
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0005


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\5&2b1d7621&0&1
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM5)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0008
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\5&2b1d7621&0&6
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM6)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0003
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&12e3460c&0&2
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM5)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0014
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&12e3460c&0&3
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM6)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0015
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&12e3460c&0&4
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM4)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0013
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&25c59972&0&1
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM4)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0009
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&25c59972&0&2
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM6)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0011
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&25c59972&0&3
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM7)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0012
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&25c59972&0&4
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM5)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0010
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&2d5dc42f&0&1
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM6)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0005
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&2d5dc42f&0&2
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM5)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0004
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\6&2d5dc42f&0&3
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM7)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0006
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\7&1814f229&0&1
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM8)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0007
>>>>AttachedTo: 
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303\7&c15003f&0&1
>>>FriendlyName: Prolific USB-to-Serial Comm Port (COM7)    Driver: {4d36e978-e325-11ce-bfc1-08002be10318}\0016
>>>>AttachedTo: 


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_0781&PID_5591
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_0781&PID_5591\4C530000230501200220
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0015


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_0BC2&PID_A003
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_0BC2&PID_A003\2HC015KJ
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0007


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_0815
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_0815\5&2b1d7621&0&18
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0012
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_0815\6&1d21882e&0&1
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0014


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_0817
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_0817\5&2b1d7621&0&18
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0009


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_2815
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_2815\5&2b1d7621&0&6
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0011
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_2815\6&12e3460c&0&1
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0013


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_2817
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2109&PID_2817\5&2b1d7621&0&6
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0010


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_5986&PID_2118
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_5986&PID_2118\5&2b1d7621&0&8
>>>FriendlyName:    Driver: {36fc9e60-c465-11cf-8056-444553540000}\0003


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_5986&PID_2118&MI_00
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_5986&PID_2118&MI_00\6&28310c7b&0&0000
>>>FriendlyName: Integrated Camera  Driver: {ca3e7ab9-b4c3-4ae6-8251-579ef933890f}\0000
>>>>AttachedTo: 


>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_8087&PID_0A2B
>>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_8087&PID_0A2B\5&2b1d7621&0&7
>>>FriendlyName:    Driver: {e0cbf06c-cd8b-4647-bb8a-263b43f0f974}\0000
+>15:45:34 AutoIt3.exe ended.rc:0
+>15:45:34 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 12.16

Ya, this is the Console output :D @Danyfirex

Link to post
Share on other sites

Hello Sorry for de long delay for some reason I was not seeing forum's notifications.

 

 Could You open Regedit go to this key "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" and do a a search for "COM" or "COM3" or the one you're getting connected to, and show me the key value name, seems to be the the two used by the UDF does not match in your machine.

 

Saludos

Link to post
Share on other sites
8 hours ago, Danyfirex said:

Hello Sorry for de long delay for some reason I was not seeing forum's notifications.

 

 Could You open Regedit go to this key "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" and do a a search for "COM" or "COM3" or the one you're getting connected to, and show me the key value name, seems to be the the two used by the UDF does not match in your machine.

 

Saludos

Hi, I try with COM6, and do a search, nothing inside "Class" key. First and second found as in picture.

1.png

2.png

Link to post
Share on other sites

Hello. Try this version.

;~ #AutoIt3Wrapper_UseX64=y
#include <ComUDF.au3>
#include <Array.au3>


Local $ports = _COM_ListPorts()                 ;an array with com ports
Local $portdevices = _COM_ListPortDevices_Mod()     ;2DArray DevicesName|COMPort

_ArrayDisplay($ports, "2D display transposed")
_ArrayDisplay($portdevices, "2D display transposed")



Func _COM_ListPortDevices_Mod($bIsAvailable = False)
    Local $aCOMPorts = _COM_ListPorts()
    If Not IsArray($aCOMPorts) Then Return SetError(1, 0, 0)

    Local Const $sKeyUSB = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB"
    Local $iIndex = 1
    Local $iSubIndex = 1
    Local $sSubKey = ""
    Local $sSubKeyChild = ""
    Local $sFriendlyName = ""
    Local $sDriver = ""
    Local $bFoundAll = False
    Local $sCOMPort = ""
    Local $hCOMPort = ""
    Local $aCOMDevices[256][3]
    Local $iIndexDevices = 0
    Local $aCOMPort = 0

    While True
        $sSubKey = RegEnumKey($sKeyUSB, $iIndex)
        If @error Then ExitLoop
        $iIndex += 1
        $sSubKey = $sKeyUSB & "\" & $sSubKey
        $iSubIndex = 1
        ConsoleWrite(@CRLF & @CRLF & ">" & $sSubKey & @CRLF)
        While True
            $sSubKeyChild = RegEnumKey($sSubKey, $iSubIndex)
            If @error Then ExitLoop
            $iSubIndex += 1
            ConsoleWrite(">>" & $sSubKey & "\" & $sSubKeyChild & @CRLF)
            $sFriendlyName = RegRead($sSubKey & "\" & $sSubKeyChild, "FriendlyName")
            $sDriver = RegRead($sSubKey & "\" & $sSubKeyChild, "Driver")
            ConsoleWrite(">>>FriendlyName: " & $sFriendlyName & @TAB & "Driver: " & $sDriver & @CRLF)
            If $sFriendlyName = "" Then ContinueLoop
            $sCOMPort = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" & $sDriver, "AttachedTo")
            If $sCOMPort = "" Then $sCOMPort = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" & $sDriver, "AssignedPortForQCDevice")
            If $sCOMPort = "" Then
                $aCOMPort = StringRegExp($sFriendlyName, 'COM\d{1,}', 3)
                If IsArray($aCOMPort) Then $sCOMPort = $aCOMPort[0]
            EndIf
            ConsoleWrite(">>>>AttachedTo: " & $sCOMPort & @CRLF)
            If $sCOMPort = "" Then ContinueLoop ; not Attached
            ;check if exist in $aCOMPorts and remove it
            For $x = 0 To UBound($aCOMPorts) - 1
                If $aCOMPorts[$x] = "" Then ContinueLoop
                If $aCOMPorts[$x] = $sCOMPort Then
                    $aCOMDevices[$iIndexDevices][0] = $sFriendlyName
                    $aCOMDevices[$iIndexDevices][1] = $sCOMPort
                    $aCOMDevices[$iIndexDevices][2] = False
                    If $bIsAvailable Then
                        $hCOMPort = _COM_OpenPort($sCOMPort)
                        $aCOMDevices[$iIndexDevices][2] = $hCOMPort <> -1
                        If $hCOMPort <> -1 Then _COM_ClosePort($hCOMPort)
                    EndIf
                    $iIndexDevices += 1
                    ExitLoop
                EndIf
            Next
            If UBound($aCOMPorts) = 0 Then $bFoundAll = True
            If $bFoundAll Then ExitLoop
        WEnd
        If $bFoundAll Then ExitLoop
    WEnd

    ReDim $aCOMDevices[$iIndexDevices][3]
    If $iIndexDevices Then Return SetError(0, $iIndexDevices, $aCOMDevices)
    Return SetError(2, 0, 0)
EndFunc   ;==>_COM_ListPortDevices_Mod

 

 

Saludos

Link to post
Share on other sites
Posted (edited)
On 3/14/2021 at 7:13 PM, Danyfirex said:

Hello. Try this version.

;~ #AutoIt3Wrapper_UseX64=y
#include <ComUDF.au3>
#include <Array.au3>


Local $ports = _COM_ListPorts()                 ;an array with com ports
Local $portdevices = _COM_ListPortDevices_Mod()     ;2DArray DevicesName|COMPort

_ArrayDisplay($ports, "2D display transposed")
_ArrayDisplay($portdevices, "2D display transposed")



Func _COM_ListPortDevices_Mod($bIsAvailable = False)
    Local $aCOMPorts = _COM_ListPorts()
    If Not IsArray($aCOMPorts) Then Return SetError(1, 0, 0)

    Local Const $sKeyUSB = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB"
    Local $iIndex = 1
    Local $iSubIndex = 1
    Local $sSubKey = ""
    Local $sSubKeyChild = ""
    Local $sFriendlyName = ""
    Local $sDriver = ""
    Local $bFoundAll = False
    Local $sCOMPort = ""
    Local $hCOMPort = ""
    Local $aCOMDevices[256][3]
    Local $iIndexDevices = 0
    Local $aCOMPort = 0

    While True
        $sSubKey = RegEnumKey($sKeyUSB, $iIndex)
        If @error Then ExitLoop
        $iIndex += 1
        $sSubKey = $sKeyUSB & "\" & $sSubKey
        $iSubIndex = 1
        ConsoleWrite(@CRLF & @CRLF & ">" & $sSubKey & @CRLF)
        While True
            $sSubKeyChild = RegEnumKey($sSubKey, $iSubIndex)
            If @error Then ExitLoop
            $iSubIndex += 1
            ConsoleWrite(">>" & $sSubKey & "\" & $sSubKeyChild & @CRLF)
            $sFriendlyName = RegRead($sSubKey & "\" & $sSubKeyChild, "FriendlyName")
            $sDriver = RegRead($sSubKey & "\" & $sSubKeyChild, "Driver")
            ConsoleWrite(">>>FriendlyName: " & $sFriendlyName & @TAB & "Driver: " & $sDriver & @CRLF)
            If $sFriendlyName = "" Then ContinueLoop
            $sCOMPort = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" & $sDriver, "AttachedTo")
            If $sCOMPort = "" Then $sCOMPort = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\" & $sDriver, "AssignedPortForQCDevice")
            If $sCOMPort = "" Then
                $aCOMPort = StringRegExp($sFriendlyName, 'COM\d{1,}', 3)
                If IsArray($aCOMPort) Then $sCOMPort = $aCOMPort[0]
            EndIf
            ConsoleWrite(">>>>AttachedTo: " & $sCOMPort & @CRLF)
            If $sCOMPort = "" Then ContinueLoop ; not Attached
            ;check if exist in $aCOMPorts and remove it
            For $x = 0 To UBound($aCOMPorts) - 1
                If $aCOMPorts[$x] = "" Then ContinueLoop
                If $aCOMPorts[$x] = $sCOMPort Then
                    $aCOMDevices[$iIndexDevices][0] = $sFriendlyName
                    $aCOMDevices[$iIndexDevices][1] = $sCOMPort
                    $aCOMDevices[$iIndexDevices][2] = False
                    If $bIsAvailable Then
                        $hCOMPort = _COM_OpenPort($sCOMPort)
                        $aCOMDevices[$iIndexDevices][2] = $hCOMPort <> -1
                        If $hCOMPort <> -1 Then _COM_ClosePort($hCOMPort)
                    EndIf
                    $iIndexDevices += 1
                    ExitLoop
                EndIf
            Next
            If UBound($aCOMPorts) = 0 Then $bFoundAll = True
            If $bFoundAll Then ExitLoop
        WEnd
        If $bFoundAll Then ExitLoop
    WEnd

    ReDim $aCOMDevices[$iIndexDevices][3]
    If $iIndexDevices Then Return SetError(0, $iIndexDevices, $aCOMDevices)
    Return SetError(2, 0, 0)
EndFunc   ;==>_COM_ListPortDevices_Mod

Dear Danyfirex,

 

 

Saludos

 

Screenshot_2.png

Screenshot_3.png

Edited by mnkt
Link to post
Share on other sites

It seems to be there is something cached. It's really weird. I'll PM you.

 

 

Saludos

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...