NiVZ Posted February 3, 2010 Share Posted February 3, 2010 (edited) Hello,I'm writing software to interact with Diabetic Blood Sugar Meters. So far I've got 2 working and am trying a third.Most meters use a USB to Serial cable and I need to identify the COM port it is using. For the two I have working this was easy using the following script:expandcollapse popup; Set return varialbe to -1 for error $return = -1 ; Flags for WMI $wbemFlagReturnImmediately = 0x10 ;DO NOT CHANGE $wbemFlagForwardOnly = 0x20 ;DO NOT CHANGE ; Try to connect to WMI $wbemService = ObjGet("winmgmts:\\localhost\root\WMI") ; Check we got a WMI object if IsObj($wbemService) then ; Query WMI for Serial Devices which are on USB $wbemObjectSet = $wbemService.ExecQuery("Select * from MSSerial_PortName where InstanceName LIKE 'USB%'", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly) ; Check we get a results object If IsObj($wbemObjectSet) Then ; Loop through each result For $wbemObject In $wbemObjectSet ; Get the Port no of this device $PortName = $wbemObject.PortName ; Get the device Instance name without the last 2 characters as these are not used in registry $InstanceName = StringLeft($wbemObject.InstanceName, StringLen($wbemObject.InstanceName)-2) ;MsgBox(0,"", $InstanceName & @CRLF & $PortName) ; Read the registry device name to check it is Prolific USB-to-Serial $DeviceName = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $InstanceName, "FriendlyName") ; If we've found the correct device then set the return variable to the Port number If StringLeft($DeviceName, 29) = "Prolific USB-to-Serial Comm Port" Then $return = $PortName Next EndIf EndIf ConsoleWrite($return & @CRLF & @CRLF)This cable also shows up in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\Vid_067b&Pid_2303\5&18d8be1c&0&1.The new meter I'm trying to access uses a different cable and is not detected by my script. I've found it in the registry and it's under the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\umpport\com\6&154241fc&0&0000I've never heard of a UMP Port. Does anyone know what CLASS in the WMI I should use to access these?Thanks,NiVZ Edited February 3, 2010 by NiVZ Link to comment Share on other sites More sharing options...
water Posted February 3, 2010 Share Posted February 3, 2010 UMP seems to stand for "Universal Management Port". I'm still searching and will post my findings. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
NiVZ Posted February 3, 2010 Author Share Posted February 3, 2010 Appreciate your help. I did a quick Google and searched MSDN but got nowhere fast. In theory I could just loop through the registry searching for the cable name and get the PortName out of the Parameters key, but it would be nice to have the option to use WMI. Does anyone have any comments on whether it's better to use WMI or registry for this type of thing? Thanks, NiVZ Link to comment Share on other sites More sharing options...
water Posted February 3, 2010 Share Posted February 3, 2010 Sorry, didn't find anything meaningful. Maybe you could ask the vendor how to access their product. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
martin Posted February 3, 2010 Share Posted February 3, 2010 Appreciate your help. I did a quick Google and searched MSDN but got nowhere fast.In theory I could just loop through the registry searching for the cable name and get the PortName out of the Parameters key, but it would be nice to have the option to use WMI.Does anyone have any comments on whether it's better to use WMI or registry for this type of thing?Thanks,NiVZThis post might give some help. Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
NiVZ Posted February 8, 2010 Author Share Posted February 8, 2010 This post might give some help.Thanks Martin.I had found this key as I use it to list the available COM ports. Think I might have to use a combination of looping through the HKLM\SYSTEM\CurrentControlSet\Enum\UMPPORT to find the device I'm looking for (it can appear multiple times if you've plugged it into different USB ports, and each one gets a different COM port in the 'Device Parameters' sub-key).Then when I've found one, I can check it's 'Device Parameters' COM port against the HKLM\Hardware\SERIALCOMM key to see if it's currently available/active and if it is, then chances are it's the correct COM port.Not the tidiest method, but at least it will work, and won't rely on WMI either ;o)Thanks,NiVZ Link to comment Share on other sites More sharing options...
NiVZ Posted February 8, 2010 Author Share Posted February 8, 2010 Hello, Here's the code I've put together to do this. The function accepts two parameters. 1. The name of the device you are looking for 2. The class under HKLM\System\CurrentControlSet\Enum you want to search (since I need to search either 'USB' or 'UMPPORT') The function loops through all the device GUID's (and instance sub-keys) doing a string match of the first parameter with the 'FriendlyName' (minus the (COM??) that registry adds. When it finds a match it then grabs the 'PortName' from the 'Device Parameters' sub key and checks this against the currently known Active COM ports. When both a string match and a COM port match is found it returns the name of the COM port, otherwise it returns -1. NiVZ expandcollapse popup; Seacrh for Abbott USB cable in UMPPORT devices $Abbott = _FindCOM("USB - Abbott Strip Port Cable", "umpport") ; Search for Lifescan Onetouch cable in USB devices $Lifescan = _FindCOM("Prolific USB-to-Serial Comm Port", "USB") #cs ############################################################################################################## # Function: _FindCOM($inDeviceName, $inClassKey) # # $inDeviceName - is the registry 'FriendlyName' of the device you want to find # $inClassKey - is the Bass class under HKLM\System\Enum where you want to search, eg USB, UMPPORT # # Function searches through all the device GUID's looking for a match and then compares the 'PortName' from # 'Device Parameters' with the open COM ports in HKLM\System\Hardware\SerialCOMM # # Function returns the COM port name if an active COM port is found for the device, eg 'COM1' # or -1 if an active COM port is NOT found # ############################################################################################################## #ce Func _FindCOM($inDeviceName, $inClassKey) ConsoleWrite("*** Getting COM Port for: " & $inDeviceName & @CRLF) Local $i = 1 $ClassKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $inClassKey ; DEVICE LOOP - Loop through all DEVICE GUID's While 1 ; Get the Device GUID for this loop $DeviceKey = RegEnumKey($ClassKey, $i) If @error Then ExitLoop Local $j = 1 ; INSTANCE LOOP - Loop through all instances of the device (if it has been connected to different ports) While 1 ; Get the Device Instance for this loop $InstanceKey = RegEnumKey($ClassKey & "\" & $DeviceKey, $j) If @error Then ExitLoop ; Read the friendly name $FriendlyName = RegRead($ClassKey & "\" & $DeviceKey & "\" & $InstanceKey, "FriendlyName") If @error Then ExitLoop ; The registry FriendlyName has the COM Port in brackets so need to remove this before trying the match If StringLeft($FriendlyName, StringInStr($FriendlyName, " (COM")-1) = $inDeviceName Then ; We have a match so get the COM port for this instance $COMtoCheck = RegRead($ClassKey & "\" & $DeviceKey & "\" & $InstanceKey & "\Device Parameters", "PortName") ConsoleWrite(@TAB & "Device Found at: " & $ClassKey & "\" & $DeviceKey & "\" & $InstanceKey & " using port " & $COMtoCheck & @CRLF) ; Check if this is an Active COM Port Local $k = 1 ; COM PORT LOOP - Loop through all the open COM ports While 1 ; Get the Active COM port for this loop $ActiveCOMPort = RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM\", RegEnumVal("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM\", $k)) If @Error then ExitLoop If $ActiveComPort = $COMtoCheck Then ; If the Active COM port for this loop equals our Device COM port then we have found the correct port $Active = True ExitLoop Else ; Otherwise keep checking the other Active COM ports $Active = False EndIf $k += 1 WEND If $Active Then ; If we set active variable to true then return the COM port ConsoleWrite(@TAB & "Port " & $COMtoCheck & " is ACTIVE" & @CRLF) ConsoleWrite("*** RETURN: " & $COMtoCheck & @CRLF) Return $COMtoCheck Else ; The COM port on this instance is not active ConsoleWrite(@TAB & "Port " & $COMtoCheck & " is NOT ACTIVE" & @CRLF) EndIf EndIf $j += 1 WEnd $i += 1 WEnd ; Could Not Find Active COM Port for matching device ConsoleWrite("*** RETURN: -1" & @CRLF) Return -1 EndFunc NiVZ Link to comment Share on other sites More sharing options...
frmkla Posted November 26, 2011 Share Posted November 26, 2011 this can be done with the help of wmi services, Here is an example program http://collectns.blogspot.com/2011/11/vbscript-for-detecting-usb-serial-com.html Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now