Jump to content
Sign in to follow this  
goodmanjl531

Com port Query

Recommended Posts

goodmanjl531

I was trying to find a way to find out via AutoIt what comm port a device was plugged into.  I am using a Silicon Labs CP210x USB to UART Bridge.  This has to be done behind the scenes so i can automatically setup the correct comm port for my communications software.

Thanks in advance

 

post-77772-0-80434600-1392404213_thumb.j

Share this post


Link to post
Share on other sites
orbs

alternative, using WMI:

code adapted from: http://artisgeek.com/weblog/scripts/-getdevices-autoit/

#include <Array.au3>
$aDeviceList=_getDevices('Silicon Labs CP210x USB to UART Bridge',1)
_ArrayDisplay($aDeviceList)

;Function Name: Get (Connected) Devices
;Written By: Amin Babaeipanah
;Usage: _getDevices(1,'')
;_getDevices("search for", flag)
;"search for": can be set to empty to list everything
;flag:
;    1 = List Device Name(s)
;    2 = List Device ID(s)
;    3 = List Device Name(s) @ Device ID(s)
;Example 1:
;    Code below will list all the connected devices by name
;        _getDevices('',1)
;    Code below will list all the connected devices by ID
;        _getDevices('',2)
;    Code below will list all the connected devices by name that has the word "COM"
;        _getDevices('COM',1)

; adaptation: replace ConsoleWrite with array return
; adaptation by: orbs
; original source at: http://artisgeek.com/weblog/scripts/-getdevices-autoit/

Func _getDevices($name,$type)
    Dim $aDeviceList[1]=[0]
    Local $objWMIService = ObjGet('winmgmts:\\localhost\root\CIMV2')
    Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%"&$name&"%'", "WQL", 48)
    If IsObj($colItems) Then
        If $type = 1 Then
            For $objItem In $colItems
                $aDeviceList[0]+=1
                ReDim $aDeviceList[$aDeviceList[0]+1]
                $aDeviceList[$aDeviceList[0]]=$objItem.Name
                ;ConsoleWrite($objItem.Name&@LF)
            Next
        ElseIf $type = 2 Then
            For $objItem In $colItems
                $aDeviceList[0]+=1
                ReDim $aDeviceList[$aDeviceList[0]+1]
                $aDeviceList[$aDeviceList[0]]=$objItem.PNPDeviceID
                ;ConsoleWrite($objItem.PNPDeviceID&@LF)
            Next
        ElseIf $type = 3 Then
            For $objItem In $colItems
                $aDeviceList[0]+=1
                ReDim $aDeviceList[$aDeviceList[0]+1]
                $aDeviceList[$aDeviceList[0]]=$objItem.Name&'@'&$objItem.PNPDeviceID
                ;ConsoleWrite($objItem.Name&'@'&$objItem.PNPDeviceID&@LF)
            Next
        EndIf
    EndIf
    Return $aDeviceList
EndFunc

the function returns an array of all devices by criteria (name or type).

calling he function with your device name will return (hopefully) only one result, then use string manipulation - like _StringBetween() - to parse the port.

  • Like 2

Share this post


Link to post
Share on other sites
goodmanjl531

Thanks, That did the trick!!!

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  

×