Reputation: 43
A user from this website helped me to create the below code, see How to retrieve serialcomm from a query?
The issue is that displayed are all the COM ports where the device was connected and I only need to know on which port it is currently connected.
I might just need to change something, but I am not sure what it is.
@echo off
setlocal EnableExtensions
set "HardwareID=VID_067B&PID_2303"
set "RegistryPath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB"
set "ProductName=RS232 cable"
set "DeviceFound=0"
cls
echo.
for /F "delims=" %%I in ('%SystemRoot%\System32\reg.exe QUERY "%RegistryPath%\%HardwareID%" 2^>nul') do call :GetPort "%%I"
if "%DeviceFound%" == "0" echo WARNING: Could not find any %ProductName%.
echo.
endlocal
pause
goto :EOF
:GetPort
set "RegistryKey=%~1"
if /I not "%RegistryKey:~0,71%" == "%RegistryPath%\%HardwareID%\" goto :EOF
for /F "skip=2 tokens=1,3" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1\Device Parameters" /v PortName 2^>nul') do (
if /I "%%A" == "PortName" set "SerialPort=%%B" && goto OutputPort
)
goto :EOF
:OutputPort
set "DeviceFound=1"
set "DeviceNumber=%RegistryKey:~-1%"
echo %DeviceNumber%. %ProductName% is %SerialPort%.
goto :EOF
pause
Upvotes: 0
Views: 2044
Reputation: 49167
Here is my first idea which is not 100% safe for connection status checking as I wrote already in answer on How to retrieve serialcomm from a query? because the string values under registry key
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
are not immediately updated by Windows when a serial device is unplugged from the machine.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "HardwareID=VID_067B&PID_2303"
set "RegistryPath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB"
set "ProductName=RS-232 cable"
set "DeviceFound=0"
cls
echo(
for /F "delims=" %%I in ('%SystemRoot%\System32\reg.exe QUERY "%RegistryPath%\%HardwareID%" 2^>nul') do call :GetPort "%%I"
if "%DeviceFound%" == "0" echo WARNING: Could not find any connected %ProductName%.
echo(
endlocal
pause
exit /B
:GetPort
set "RegistryKey=%~1"
if /I not "%RegistryKey:~0,71%" == "%RegistryPath%\%HardwareID%\" goto :EOF
for /F "skip=2 tokens=1,3" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1\Device Parameters" /v PortName 2^>nul') do (
if /I "%%A" == "PortName" set "SerialPort=%%B" && goto OutputPort
)
goto :EOF
:OutputPort
%SystemRoot%\System32\reg.exe query HKLM\HARDWARE\DEVICEMAP\SERIALCOMM | %SystemRoot%\System32\findstr.exe /E /I /L /C:%SerialPort% >nul
if errorlevel 1 goto :EOF
set "DeviceFound=1"
set "DeviceNumber=%RegistryKey:~-1%"
echo %DeviceNumber%. %ProductName% is %SerialPort%.
goto :EOF
I modified the warning by inserting the word connected
which is a cosmetic improvement, but an important one because of the other modification.
The other modification is in subroutine OutputPort
where two command lines were inserted for cross-checking the found COM port name of the specific USB device with the current list of COM port names in device map.
The output of reg.exe
is searched by findstr.exe
case-insensitive for the serial port string retrieved just before which must be found at end of an output line.
The exit code assigned to errorlevel
by findstr.exe
is 1
if current COMx could not be found at the end of any line output by reg.exe
. This means the specific USB device on USB port A is currently not connected and so the subroutine is exited with goto :EOF
.
But if there is indeed a line ending with COMx in output of reg.exe
, the specific USB device on USB port B (or C or D) is (most likely) connected and this serial communication port can be used by an application.
Run in a command prompt window findstr /?
for details on the used options.
A small story about serial port management on my computer which might be interesting for readers:
I connected the single ATEN USB to Serial Bridge adapter I own once to each USB port of my computer and then set in Windows Device Manager in the Properties of the device on tab Port Settings after clicking on button Advanced the COM Port Number to COM1.
So in my Windows registry this USB device is registered four times with COM1 and the batch file above outputs therefore also four times COM1 independent on which USB port I have currently connected this adapter.
That's okay because I have overruled the Windows standard COM name assigning mechanism to give each serial device in each individual USB port the next free COM port number not used by any device ever connected to this computer.
The big advantage for me is that the ATEN USB to Serial Bridge adapter is COM1 independent on which USB port I plug the adapter. But if I would have a second ATEN USB to Serial Bridge adapter and would plug it additionally to another USB port, I would have a problem and would need to change the COM port once again in Windows Device Manager to use both at the same time.
Another information about serial port number management:
I have seen computers where the COM port assigned to a new serial device was already very high like 45. It is possible to cleanup the serial devices list and so get COM port numbers back from the "used" list for new serial devices.
The necessary steps are as follows whereby the first three steps are needed only on Windows 2000 to Windows 7.
DEVMGR_SHOW_NONPRESENT_DEVICES
and with value 1
.DEVMGR_SHOW_NONPRESENT_DEVICES
does not need to be defined on Windows 8/8.1/10/11 and later versions of Windows as the device manager shows also the non-present devices on enabling Show hidden devices.There are shown now not only the really present but by default hidden devices, but also all non-present devices with a grayed icon like the few hidden devices (because of the environment variable on Windows 2000 to Windows 7).
So the non-present serial devices can be deleted now under Ports (COM & LPT) which results in freeing the appropriate port number.
As each USB to serial adapter has also a device entry under Universal Serial Bus controllers, it is advisable before deletion of the serial device to double click on the serial device to open the Properties, switch to tab Details, select the Hardware Ids and write down vendor identifier (VID) and product identifier (PID) before closing the properties window and deleting the serial device. The USB device with same VID and PID should be deleted, too.
Extra note: An USB flash stick is registered with in total four devices under Disk drives, Portable devices, Storage Volumes and Universal Serial Bus controllers. In general it is safe to delete all grayed devices really not used anymore except those under Non-Plug and Play Drivers, Network adapters and Sound, video and game controllers because those categories contain always present hidden devices.
See also: I have phantom serial ports in Windows. How can I remove them?
Upvotes: 1