larksp

How to Switch com1 to com 2 and com2 to com1

11 posts in this topic

sometimes after i reinstall a w7 Acer . it shows 2 com ports Most of the time it sets the right 1 to com1.
there is only  1 serial port on the back of the computer .  A DP1 & dp2 also a dvi-d (plus normal ones)

I know how to use Wmi to show there are 2 com ports on the pc
"SELECT * FROM MSSerial_PortName"
OR SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%)'

I see there are few udfs to do alot more than i would want and not sure if they can do this
just a simple check com ports and switch them
rather than going device manager checking to see if there are more than 1 port then changing it
(Port in use so have to change "1 TO 3" "2 TO 1" "3 TO 2 )

If this is harder than it seems just say and i wont bother :)

Share this post


Link to post
Share on other sites



Have you tried the following UDF, _CommListPorts, _CommSetPort functions in particular.

 

Share this post


Link to post
Share on other sites

i will have a look now hopefully but am getting bit sleepy :)

Share this post


Link to post
Share on other sites

i will have a play with the code either Tuesday or try find a pc lying around that actually has a com port as my main computers at home dont :)

Share this post


Link to post
Share on other sites

I'm not sure to understand what you want to do. You want to change the COM port assignment ? Set the COM1 to use the COM3 name ?
Maybe this article could help you : Re-Assigning COM Port Numbers Using the Windows Registry

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

yes on some of the acer computers under com ports(Device manager) has 2 Ports  "com1 and com2..." but only 1 port to plug the cable in.
some times after a reinstall of windows the real serial port  (where the cable plugs in) instead is now com2.
so i just want to change it back to com1
doing this the manually way i have to set com1 to com3 so that i can set com2 to com1.. you cant just set com2 to com1 as there is already com1 assigned

(Motherboard assigned ports)

Edited by larksp
ref to what ports

Share this post


Link to post
Share on other sites

With the Win32_SerialPort WMI class, you can  check if the property PNPDeviceID is different between the 2 ports (with a Acer computer having two COM ports). You maybe you will be able to determine in which case you have to re-assign the COM port.

 

 

Share this post


Link to post
Share on other sites
_PnPEntity()

Func _PnPEntity()
    Local $Array[0][3]
    Local $WMI = ObjGet("winmgmts:\\localhost\root\CIMV2")
    Local $Port = $WMI.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%)'", "WQL", 48)
    If IsObj($Port) Then
    For $col In $Port
        ReDim $Array[UBound($Array) + 1][3]
        $Array[$I][0] = $I + 1
        $Array[$I][1] = $col.Name
        $Array[$I][2] = $col.PNPDeviceID
        $I += 1
    Next
EndIf

untested as this pc has no comport i think Win32_PnPEntity from https://msdn.microsoft.com/en-us/library/aa394353(v=vs.85).aspx has PNPDeviceID
would ov need to add array display to see it

Share this post


Link to post
Share on other sites

still dont know how to do the re-assign part tho the doc you posted is in hex and it does say the first 2 hex numbers is com 1 and com 2 i think but i dont understand hex lol

Share this post


Link to post
Share on other sites

#10 ·  Posted

I made some tests on my computer to re-assign com1 to com2. I managed to do it though the registry.
I understand how it works now, here is my explanation (I hope it will be clear enough).

First, you have to get sufficient rigths in some registry keys (like HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\COM Name Arbiter because you don't have enough rights to write in the key, even if you use an administrator account).

In the above registry key, the value ComDB contains the list of the COM ports used by the system. On my computer its value is 0x0500000000000000000000000000000000000000000000000000000000000000
The binary value is made like this :
 - the first byte represents the state of use for COM1 To COM8  : on my computer, the value is 05 (or 0x05). In real binary it's equal to 00000101 (COM3 and COM1 used)
 - the next byte represents COM9 to COM16 and so on...
So the first thing to do is to check if the desired port is used or not using this value :

Local $ComDB = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\COM Name Arbiter", "ComDB")
Local $iLen = BinaryLen($ComDB)
Local $iByte, $iPortNum, $iDec

For $i = 1 To $iLen
    $iByte = BinaryMid($ComDB, $i, 1)
    For $j = 1 To 8
        $iPortNum = ($i - 1) * 8 + $j
        $iDec = 1 * 2 ^ ($j - 1)
        If BitAND($iByte, $iDec) Then ConsoleWrite("Port " & $iPortNum & " used " & @CRLF)
    Next
Next

Next you will have to calculate the new ComDB value : set the old port number as not used and the new port number as used. For example, if you want to set COM2 and COM3 as used and COM1 as not used, the value in real binary will be 00000110 = 0x06. So the new ComDB value to write will be 0x0600000000000000000000000000000000000000000000000000000000000000

After that, you have to remove the registry value corresponding to the old port and add the new one in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports
  - add the COM2 value and set its value to the same than COM1
  - remove the COM1 value if not used

Next you will have to apply changes in
 - HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM\   :   \Device\Serial0   -    \Device\Serial0
 - HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ACPI\PNP0501\1   :   FriendlyName (it's the PNDDeviceID found with the WMI query)
 - HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ACPI\PNP0501\1\Device Parameters   :   PortName

This is not trivial...

 

 

1 person likes this

Share this post


Link to post
Share on other sites

#11 ·  Posted

thanks for the reply.... I understand witch is good as means I have learnt more and more as i use autoit lol...

next time im on acer pc ill check the reg for it.. and maybe also open one that set ok and one that is not to see if there different as well

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