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
Next
If strDeviceID = "" Then
MsgBox "Cannot find the USB COM Port !" & vbCrLf & vbCrLf & "Please verify the USB connection."
Else
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
- Details
-
Written by Nicolas Buache
-
Created on Thursday, 26 April 2012
-
Last Updated on Thursday, 03 May 2012
-
Hits: 1608