Main Menu
Photo Gallery
House Construction
Online Profiles

How to force COM port number

Go back to Knowledge Base

In some cases, you could need to force the virtual COM port for an USB device. I needed to do this in order to connect an USB interface to interconnect measurement tools with a computer, and use it from an application that is published on Citrix. On the Citrix application, we need to know on which COM port the tools are mounted. For this reason, we needed to force the COM port on the computer.

I made some tests in order to find how to force it, and found that a registry key was modified, but the path is quite difficult to find as you have an ID designing the kind of hardware you have connected, but you also have a unique ID that is linked to the USB port where you connected it.

I then tried to find another way to implement it, and found a script to display the COM port mounted by an USB device... and some information about how to write a registry key... to finally modify and compile everything to obtain the script below.

In RED, you have all the parameters that you would probably change, depending on which COM port you want to force your hardware, and your hardware information (coming from Device Manager).

To use this script, simply paste it in a VBS file, and execute it. This script is using WMI, and will tell you if the device is not connecter, or if the COM port has been changed.

I hope you will find this script useful.


' Please specify the COM Port and Configuration at the begining of the script
' Please specify which device we should look for => Description

Dim strDeviceID
Dim strPortNumber

' Please specify the COM Port and Configuration there
strPortNumber = "COM9"
strComConfig = "9600,n,8,1"

strComputer = "."
Set objWMIService = GetObject( _
    "winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( _
    "Select * from Win32_PnPEntity")
'Search USB Virtual COM port
For Each objItem in colItems
    ' Please specify which device we should look for => Description
    If ("FTSER2K" = objItem.Service) And ("FTDI" = objItem.Manufacturer) AND ("STEINWALD USB Interface" = objItem.description) Then
        strDeviceID = objItem.DeviceID
    End if

If strDeviceID = "" Then
    MsgBox "Cannot find the USB COM Port !" & vbCrLf & vbCrLf & "Please verify the USB connection."
    const HKEY_LOCAL_MACHINE = &H80000002
    Set StdOut = WScript.StdOut

    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
    strComputer & "\root\default:StdRegProv")

    'Force COM port
    strKeyPath = "SYSTEM\CurrentControlSet\Enum\" & strDeviceID & "\Device Parameters"
    strValueName = "PortName"
    strValue = strPortNumber
    oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

    'Force port configuration, like speed
    strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports"
    strValueName = strPortNumber & ":"
    strValue = strComConfig
    oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

    MsgBox "COM Port has been forced to " & strPortNumber

End if

* * * * * Did you like this page ? - - - Post it on your Facebook ! * * * * *
* * * * * Google Custom Search * * * * *
Last Updates
Translate by Google
English French German Italian Portuguese Russian Spanish Swedish
Login Form

* * Current URL * *
Mobile Barcode
Visitors Stats
20.2%United States United States
9%United Kingdom United Kingdom
6.9%Brazil Brazil
6%Germany Germany
5.4%Australia Australia
4.2%Canada Canada
3.6%Switzerland Switzerland
2.7%Denmark Denmark
2.7%Italy Italy
2.7%France France

Stats by ExtraWatch