0

I have a FTDI usb-serial and I am interested in the getting the COM port assigned to the device on USB serial converter A. Is there a way I can list this information programatically (say cmd) to extract the COM port number that corresponds to the one at that location. I can view this information in the device manager properties, so there could be a way to get this information through a script I guess? I am certain the COM port I would connect to will be on the serial converter A. I was looking up the registry entries for the usb device and the device I want to connect to goes to VCP0. to narrow down my question, whats the best way to get the COM number from query-ing HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM

which gives me:

HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM  
\Device\VCP0    REG_SZ    COM9                                                                                                                                                      
\Device\VCP1    REG_SZ    COM10

EDIT: I am trying to get the value of data for \Device\VCP0 using this command, but I think I am not escaping backslashes correctly:

REG QUERY "HKLM:\HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM" /v "\\Device\\VCP0"

1 Answer 1

2

Here's one way to do it:

@Echo Off
Set "CDev=VCP0"
Set "RKey=HKLM\HARDWARE\DEVICEMAP\SERIALCOMM"
Set "CNum="
For /F "Skip=1 Tokens=3" %%A In (
    'Reg Query "%RKey%" /V "\Device\%CDev%" 2^>Nul') Do Set CNum=%%A
If Not Defined CNum Exit /B
Rem Your commands below here
Echo %CNum%
Timeout 5 >Nul

You should only ever need to modify the device name on line 2 and place any of your own commands beneath from the Rem line.

Possible corrections for your straight query command are either:

Reg Query "HKLM\HARDWARE\DEVICEMAP\SERIALCOMM" /V "\Device\VCP0"

Or:

Reg Query "HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM" /V "\Device\VCP0"

There was no need to escape the backslashes in the device name, and a little confusion in root key syntax

3
  • Ah I see, so the problem is that I am adding the HKLM in front of the path? And I don't need to escape backslashes. The for loop is pretty nifty, care to explain what the Skip=1 Tokens=3 is doing?
    – rookie
    Mar 14, 2018 at 2:18
  • Actually the entire for loop looks foreign to me, it'd be great if you can provide some background
    – rookie
    Mar 14, 2018 at 2:29
  • I have added corrections for your straight registry query commands to the bottom of my answer.
    – Compo
    Mar 14, 2018 at 2:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.