Jump to content

Automatic Reassignment of COMM Port


Recommended Posts

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?

Link to comment
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)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 1 year later...

hi there,

I have a PC with one physical serial port and the connected device use com2, however, under device manager, there are 3 ports available com1, com2, com3. so we need each time to go into device manager to ensure com 2 is the default one, as per below

1. if it's on com1  we will need to default on com2

2. if it's on com3  we will need to default on com 2

3. if it's on com2  we do nothing

I know the thread is 2 years old please no flame, perhaps there is a solution out there, thanks ahead for anyone that can help

#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", "COM2:", "REG_SZ", "9600,n,8,1")
RegWrite("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM", "\Device\ProlificSerial0", "REG_SZ", "COM2")

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


;*****************************************************explain this part not sure if need this*********************************
$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
 ;*****************************************************explain this part not sure if need this*********************************

 

 

 

Link to comment
Share on other sites

#RequireAdmin
#Include <Array.au3>
#include <CommMG.au3>



local $sportSetError


_commswitch(1) ;go  to COM1
_CommSetPort("2",$sportSetError,9600, 8,  0,  1,  0, 0,  0);channel 1 is COM2

_commswitch(3);go  to COM3
_CommSetPort("2",$sportSetError,9600, 8,  0,  1,  0, 0,  0);channel 3 is COM2




 $chan = IsConnected("COM2")
if $chan <> 0 then MsgBox(0,'COM2', "Is connected on channel " & $chan)

Func IsComConnected($someCOM)
    Local $n
    For $n = 1 To 3
        _CommSwitch ($n)
        If _CommPortConnection () = $someCOM Then Return $n  ; connected
    Next
    Return 0;not connected
    
    ;~ If _CommPortConnection() = "COM2" Then MsgBox(0,"","connected")

using the udf CommMG.AU3

Edited by antonioj84
Link to comment
Share on other sites

#RequireAdmin
#Include <Array.au3>
#include <CommMG.au3>



local $sportSetError

$aListPortNames = _ComGetPortNames(0)
 _ArrayDisplay($aListPortNames) ; list available port



_commswitch(1) ;go  to COM1
_CommSetPort("2",$sportSetError,9600, 8,  0,  1,  0, 0,  0);channel 1 is COM2

_commswitch(3);go  to COM3
_CommSetPort("2",$sportSetError,9600, 8,  0,  1,  0, 0,  0);channel 3 is COM2




 $chan = IscomConnected("COM2")
if $chan <> 0 then MsgBox(0,'COM2', "Is connected on channel " & $chan)
_CommCloseport("2")



Func IsComConnected($someCOM)
    Local $n
    For $n = 1 To 3
        _CommSwitch ($n)
        If _CommPortConnection () = $someCOM Then Return $n  ; connected
    Next
    Return 0;not connected

EndFunc  ;==>IsComConnected

HI there thank you for your interest,  you need to add 2 files commg.dll and the commMg.au3 is the UDF

CommMG.au3

commg.dll

Edited by antonioj84
error
Link to comment
Share on other sites

Hi
i did ask a Question like this few months back and only got bit help with Reg way and did not like that and ended up going with controlclick commands and using
Run('rundll32.exe devmgr.dll,DeviceProperties_RunDLL /DeviceID acpi\pnp0501\1')
Run('rundll32.exe devmgr.dll,DeviceProperties_RunDLL /DeviceID acpi\pnp0501\2')

some times after a reinstall of windows the ports reassignment is wrong... the port i want as 1 is now port 2 and what i want as port 2 is port 1

Port1 should be rs232 and i think com2 might be a dvi or something else on the acer pc either way i wanted the rs232 on 1
So i have to Switch
port 1 To port 3  (Cant switch To 2 as there is already a 2)
port 2 To port 1
port 3 To port 1

 

#RequireAdmin
#Include <Array.au3>
#include <CommMG.au3>

ocal $sportSetError
$aListPortNames = _ComGetPortNames(0)
 _ArrayDisplay($aListPortNames) ; list available port

_commswitch(1) ;go  to COM1
_CommSetPort("2",$sportSetError,9600, 8,  0,  1,  0, 0,  0);channel 1 is COM2
 $chan = IscomConnected("COM2")
if $chan <> 0 then MsgBox(0,'COM2', "Is connected on channel " & $chan)
_CommCloseport("2")

Run('rundll32.exe devmgr.dll,DeviceProperties_RunDLL /DeviceID acpi\pnp0501\1')

The msgbox Says COM2 is on channel 1

but the ports have not switched

Link to comment
Share on other sites

here is the info below,  can perhaps add some clarification

;===============================================================================
;
; Function Name:  _CommSwitch($channel)
;switches functions to operate on channel 1, 2, 3 to 50
;returns  on succes the channel switched to ie 1 or 2
;         on failure -1
;Remarks  on start up of script channel 1 is selected, so if you only need one COM port
;         you don't need to use _CommSwitch
;         each channel needs to be set up with _CommSetPort
;         The same COM port cannot be used on more than one channel.
;When switch is used the first time on a channel number that port will be inactive
; and the port name will be '' (an empty string) until it is set with _CommSetport.
;The exception is that on creation channel 1 is always created and used as the
;port so switch is not needed unless more than one port is used.
;         The channel number is not related to the COM port number, so channel 1 can
;         be set to use COM2 and channel 4 can be set to use COM1 or any available port.
;Any channel number in the range 1 - 50 can be used, so it is possible to use
; the same channel number as the port number, ie switch(21) switches to COM21

 

Link to comment
Share on other sites

you should do

#RequireAdmin



RegWrite("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM", "\Device\Serial1", "REG_SZ", "COM2")
RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\ACPI\PNP0501\1\Device Parameters", "\PortName", "REG_SZ", "COM2")
RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\ACPI\PNP0501\1\", "FriendlyName", "REG_SZ","Communications Port(COM2)")

You can do it that way instead, this show how to port assignment serial1 (com1) to com2, . The only problem unless you have full admin rights on regedit you can not modify the friendlyName .  however you can use tool like regini.exe

Edited by antonioj84
ok
Link to comment
Share on other sites

  • 2 weeks later...

thank you for the reply... sorry i only just logged in to notice

no issue with admin rights..... i run from a USB plugged into the computer..
i will have to wait until Tuesday + Wed to test that.. no com ports at home to test :)

i guess that also needs a restart as its registry to work... where the control clicking horrible as it is does not.. but i will still play test and see only a restart lol :)

Edited by larksp
Link to comment
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
 Share

×
×
  • Create New...