Sign in to follow this  
Followers 0
ViciousXUSMC

Automatic Reassignment of COMM Port

3 posts in this topic

Another project idea that I am trying to get working.

A USB GPS device needs to be assigned COMM Port 25 when it is plugged into the computer.  It obviously gets the first open port when plugged in the first time and if I manually change it to 25 it will get a new port again if somebody plugs the device into a new USB port.

So I want to make a script that can detect the device, and move/configure its port automatically.

I started off finding this UDF: https://www.autoitscript.com/forum/topic/128546-serial-port-com-port-udf/?page=32

It was useful for finding the device is plugged in and what port its on, but it does not have the ability to actually change the port.

I think that will have to be done via registry, or perhaps via Objects.

I just want to double check my facts here and get a grip on what I need to do.

Registry has the following keys that change:
HKLM\HARDWARE\SERIALCOMM - \Device\ProlificSerial0
This key just lists the COMM Port for the device, it changes based on its current port.
HKLM\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303]
Under this key is a subkey that will duplicate every time the device gets a new port, I have not figure out quite yet how to determin what that subkey will be named, but it has the "FriendlyName" key and also the Device parameters\PortName key

Both of those are unfortunately SYSTEM level permission so that makes scripting a change even harder.

Last is HKLM\SYSTEM\CurrentControlSet\Control\COM Name Arbiter
This contains the record of what comm ports are occupied or vacant via binary to hex.  I would have to find a way to read its current value and change it to show port 25 taken and if possible free up the port taken by the device before the change.  Not sure how to write something like that.

Has anybody done something like this before?  If so what was your best approach?

Share this post


Link to post
Share on other sites



Here is what I have so far working on the ComDB portion.

#Include <Array.au3>

$sComDB = RegRead("HKLM\SYSTEM\CurrentControlSet\Control\COM Name Arbiter", "ComDB")
$sComDB = StringTrimLeft($sComDB, 2)
$aComDB =  _StringChop($sComDB, 2) ;Array of ComDB
 _ArrayDisplay($aComDB) ;Debug Show Array
$sOldPort = RegRead("HKLM\HARDWARE\DEVICEMAP\SERIALCOMM", "\Device\ProlificSerial0")
$sOldPort = StringTrimLeft($sOldPort, 3) ; Port Device is on Currently
$sArraySection =Ceiling($sOldPort / 8) ;Returns the Array Row This Com Port Falls Into

MsgBox(0, "", $sOldPort)
 
MsgBox(0, "", $sArraySection)
MsgBox(0, "", Dec($aComDB[$sArraySection]))




Func _StringChop($string, $size)
$count = Ceiling(StringLen($string)/$size)
Dim $array[$count+1], $start = 1
For $i = 1 To $count
    $array[$i] = StringMid($string, $start, $size)
    $start += $size
Next
$array[0] = $count
Return $array
EndFunc

I have it so that I will known what octet I am working with in the array so that I can modify that portion of the DB and write it back to the registry.  Stuck on the fact that I need to find a way to know what binary position the Com port is using so that I can subtract it out of the decimal value and change it back to hex.

For the registry permissions issue probably will use SetACL.  I have a BAT file working so need to convert that to Autoit (Surprised I never found a "finished" ObjectCom version of SetACL for autoit)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Reporting back with all my findings.

Contains all the free/taken Com Ports via Binary to Hex

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\COM Name Arbiter]

 

Parent Key for Devices GPS is under USB

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\

 

FriendlyName: What shows up in Device Manager

DeviceParameters\PortName: The Actual Port Assignment If you Change in Device Manager

 

Record of Ports

[HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM]

 

All Ports Registered and there settings

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports

 

Seems the FriendlyName is key only effects the name you see in Device Manager, this one is system level permissions, but the Deviceparameters\PortName is the actual port asignment and its not locked to system.  I can change it as admin without any permission changes.  So of all the keys above I can change exclusively that one and it works!  However its sloppy because I am not blocking/relieving any ports for autodetect, I am not changing the display name, I found the CurrentVersion\Ports key this must be where port settings are stored.  

 

Definitely close on this project.  

What would be awesome is a script to take all ports listed in HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports and do the conversion needed to block/unblock those ports in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\COM Name Arbiter then you can just delete/add as needed and have the script/function do the update.

 

Edit:

I have a working script its sort of a "shotgun" approach.  I am taking any/all instances of this device and making them Port25 I think this is safe because you can only have one device plugged in so its not like 2 of them will be attached causing a conflict.  I do have Port25 being blocked out here in the ComDatabase but I am not relieving the old port, but that is not a major issue and ok for now until I can figure out how to make that work cleanly.

This script runs as system so I did not need to use the SetACL to give permissions to change the FriendlyName.

 

#RequireAdmin
#Include <Array.au3>

Local $sTestKey  ="HKLM\SYSTEM\CurrentControlSet\Enum\USB\VID_067B&PID_2303"
Local $sNewReg

RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports", "COM25:", "REG_SZ", "9600,n,8,1")
RegWrite("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM", "\Device\ProlificSerial0", "REG_SZ", "COM25")

For $i = 1 to 30
$sCurKey = RegEnumKey($sTestKey, $i)
If @Error Then ExitLoop
RegWrite($sTestKey & "\" & $sCurKey & "\Device Parameters", "PortName", "REG_SZ", "COM25")
RegWrite($sTestKey & "\" & $sCurKey, "FriendlyName", "REG_SZ", "Prolific USB-to-Serial Comm Port (COM25)")
Next

$sComDB = RegRead("HKLM\SYSTEM\CurrentControlSet\Control\COM Name Arbiter", "ComDB")
$sComDB = StringTrimLeft($sComDB, 2)
$aComDB =  _StringChop($sComDB, 2) ;Array of ComDB
 ;_ArrayDisplay($aComDB) ;Debug Show Array
$aComDB[4] = "01" ;Change Port 25 to Occupied

For $i = 1 to $aComDB[0]
    $sNewReg &= $aComDB[$i]
Next
;Write Port25 Taken to Registry
RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\COM Name Arbiter", "ComDB", "REG_BINARY", Binary("0x" & $sNewReg))

;Functions
Func _StringChop($string, $size)
$count = Ceiling(StringLen($string)/$size)
Dim $array[$count+1], $start = 1
For $i = 1 To $count
    $array[$i] = StringMid($string, $start, $size)
    $start += $size
Next
$array[0] = $count
Return $array
EndFunc

 

Edited by ViciousXUSMC

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

  • Similar Content

    • Simpel
      By Simpel
      Hi,
      I wondered why negative integers I wrote into registry (e.g. negative x-coordinates of a gui if using two monitors and the right one is the main one) wouldn't return right when reading. Now I know: it is saved as an unsigned integer (without algebraic sign). So here is a snippet that is changing unsigned to signed integer:
      Global Const $g_sRegKey = "HKEY_CURRENT_USER\Software\" & @ScriptName ; path to registry RegWrite($g_sRegKey, "Value", "REG_DWORD", -2147483647) ; write some negative integer into registry; -2147483647 highest possible negative integer , 2147483648 highest possible positive integer if talking of 32bit Local $sValue = RegRead($g_sRegKey, "Value") ; read out registry ConsoleWrite("Value: " & $sValue & @CRLF) ; show real value in console Local $sResult = _SignedInteger($sValue) ; change to signed value ConsoleWrite("Result: " & $sResult & @CRLF) ; and show it in console Func _SignedInteger($iUnsignedInteger) Local $iSignedInteger If $iUnsignedInteger > (2^31) Then ; then it means a negative integer $iSignedInteger = $iUnsignedInteger - (2^32) Else $iSignedInteger = $iUnsignedInteger EndIf Return $iSignedInteger EndFunc It took me some time to find out the problem and so I hope I can help somebody with this.
      Regards, Conrad
    • GreenSparks
      By GreenSparks
      Hi!
      I post here because i was wonderring if someone in this forum could help me with my script.
      What i'm trying to do, is to basically install a new keyboard layout in Windows and activate it. I want to acheive by the registry or the command line, but not with the autoit automation tools, i don't find it stable...
      Thanks for your help and have a good day!
    • Leo1906
      By Leo1906
      Hello guys,
      I'm having a problem right here ..
      I was trying to create a few Registry Values using autoit, but it seems to me that Autoit can't write those values to this specific part of the registry ..
      Here is the code I'm trying to run:
      RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test\command", "", "REG_SZ", "C:\test\convert.exe %1 geojson|convert") I'm definitly sure that the path is correct. Script is runned with Admin Rights. Everything's fine here.
      It's possible to read other values stored by Windows in this directory:
      MsgBox(0, 0, RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.burn", "Description")) But you can't edit those using Autoit. I don't know what this is. In my eyes it's an Autoit Bug or something like that ...
      Possible solution for me is to write a ".reg" file and run it afterwards, but this is an unclean method I don't really want to use ..
      Any suggestions on this?
       
      Edit: the OS is Windows 7 x64 German Language btw ..
    • ur
      By ur
      I have a registry value in which the name is having \ and when I am trying to delete the registry,it is failing because of \.
      The registry is given below.
       
      Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Active Setup\Installed Components\{2D46B6DC-2207-486B-B523-A557E6D54B47}] "Version"="9,0,8112,16421" "Locale"="*" "C:\\ProgramData\\Symantec\\Symantec Endpoint Protection\\"="C:\\ProgramData\\Symantec\\Symantec Endpoint Protection\\" Can you please suggest how to delete the last value.
    • Wicked_Caty
      By Wicked_Caty
      I wanted to have a few lines that copy the whole registry into a file. I know that there is RegRead, but I don't want to type every single key into my code...
      Any way of doing this easily? Filetype doesn't matter, as long as it isn't encrypted. A batch or exe would be okay, if it's in the same location on every PC. Doing this without admin permissions would be awesome too
      Thanks