如何从查询中检索Serialcomm? [英] How to retrieve serialcomm from a query?

查看:387
本文介绍了如何从查询中检索Serialcomm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习如何创建批处理脚本,但是到目前为止,我只是在做查询.我可以在以下注册表路径中获取当前硬件,设备映射和串行com的列表

I'm currently learning how to create batch scripts, but so far I am doing just queries. I am able to get a list of the current hardware, device map and serial com on the following registry path

HKLM\hardware\devicemap\serialcomm

问题是每台计算机在同一设备上显示不同的serialcomm.因此,我试图根据设备名称捕获serialcomm.

The problem is that every computer is displaying a different serialcomm on the same device. So I am trying to capture the serialcomm based on the device name.

我当前的代码如下:

reg query HKLM\hardware\devicemap\serialcomm
pause

以下是我尝试根据硬件名称获取serialcomm的更新代码,但不起作用:(

Below is an updated code I have tried to get the serialcomm based on the hardware name, but is not working :(

set "Comm="
reg query HKLM\hardware\devicemap\serialcomm
if %%hardware%% = ProlificSerial0 set Comm=%%serialcomm%%
pause

以上代码是根据我在多个网站上找到的所有信息创建的,但是正如我所说,我仍在学习查询,添加更多代码有点复杂.

The above code was created based on all the information I have found on multiple websites, but as I said, I am still learning queries and it is a little bit complicated for me adding more code.

如果有人能告诉我更新后的代码出了什么问题,我将不胜感激.

I will appreciate if anybody can tell me what is wrong with the updated code.

推荐答案

这里是一个批处理代码,上面有一些解释性注释,我几周前写了这些代码,以便使用mySmartUSB MK2通过虚拟文件安装通过批处理文件对Atmel AVR控制器进行编程.安装完设备驱动程序后的通讯端口.用于编程AVR控制器的保险丝位,闪存和eeprom的代码部分在此处替换为一条简单的信息消息.

Here is a batch code with some explaining comments which I wrote a few weeks ago to program Atmel AVR controllers via a batch file using mySmartUSB MK2 being mounted as virtual serial communication port after having once the device driver installed. The code part for programming the fuse bits, flash and eeprom of the AVR controllers is replaced here by a simple information message.

批处理文件确定COM端口号

The batch file determines the COM port number

  1. 来自几个可选命令行参数(特殊选项)之一,或者
  2. 直接从Windows注册表中自动查询(默认方法),或者
  3. 向批处理文件的用户询问端口号(故障保护方法).

对于这个问题,从Windows注册表中进行识别是最有趣的部分.

For the question the identification from Windows registry is the most interesting part.

DeviceInstanceHardwareIDProductName必须适用于Prolific USB到串行转换器.

DeviceInstance, HardwareID and ProductName must be adapted for the Prolific USB-to-Serial converter.

在注册表项上使用Windows注册表编辑器查看

Look with Windows registry editor on registry key

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB

用于Prolific USB到串行转换器的设备实例标识符和硬件标识符(供应商和产品标识符).

for the device instance identifier and hardware identifier (vendor and product identifier) for the Prolific USB-to-Serial converter.

@echo off
setlocal EnableExtensions
set "ComPort="
set "DeviceInstance=mySmartUSB2-0001"
set "HardwareID=VID_10C4&PID_EA60"
set "ProductName=mySmartUSB MK2"

cls
echo.
call :ProcessArguments %*
call :CheckPort

echo Serial port of %ProductName% is: COM%ComPort%
echo.
endlocal
pause
goto :EOF


rem The subroutine ProcessArguments processes the optional parameters of
rem the batch file. For this demo everything except COMx with x being a
rem number in range 1 to 255 is interpreted as unknown parameter.

:ProcessArguments
set "EmptyLine=0"
:NextArgument
if "%~1" == "" (
    if "%EmptyLine%" == "1" echo.
    goto :EOF
)
set "SerialPort=%~1"
if /I "%SerialPort:~0,3%" == "com" (
    set "ComPort=%SerialPort:~3%"
) else (
    echo ERROR: Unknown parameter: %1
    set "EmptyLine=1"
)
shift
goto NextArgument


rem The subroutine CheckPort checks the port number specified as parameter
rem on calling the batch file.

rem In case of no COM port number was specified as parameter, it attempts
rem to determine the COM port number for the device as defined at top of
rem the batch file directly from Windows registry. The registry query is
rem written to work for Windows XP and any later Windows.

rem Last in case of a valid COM port was whether passed to the batch file
rem via COMx parameter nor could the port number be determined from Windows
rem registry automatically, the user is prompted in a loop to enter a valid
rem port number.

:CheckPort
set "EmptyLine=0"
if not "%ComPort%" == "" goto VerifyPort

for /F "skip=2 tokens=1,3" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\SYSTEM\CurrentControlSet\Enum\USB\%HardwareID%\%DeviceInstance%\Device Parameters" /v PortName 2^>nul') do (
    if /I "%%A" == "PortName" set "SerialPort=%%B" && goto GetPortNumber
)
goto EnterPort

:GetPortNumber
if /I "%SerialPort:~0,3%" == "com" (
    set "ComPort=%SerialPort:~3%"
    goto VerifyPort
)

:EnterPort
set "EmptyLine=1"
set "ComPort=4"
set /P "ComPort=Enter number of COM port (default: %ComPort%): "

:VerifyPort
for /F "delims=0123456789" %%I in ("%ComPort%") do set "ComPort="
if "%ComPort%" == "" goto EnterPort
if %ComPort% EQU   0 goto EnterPort
if %ComPort% GTR 255 goto EnterPort
if "%EmptyLine%" == "1" echo.
goto :EOF

我认为不建议处理HKLM\HARDWARE\DEVICEMAP\SERIALCOMM,因为它列出了所有串行设备,例如包括调制解调器,而不仅仅是串行端口.同样,借助设备已知的硬件标识符,可以如上所述直接更快速地找到COM端口号.

I think, processing HKLM\HARDWARE\DEVICEMAP\SERIALCOMM is not advisable as it lists all serial devices including for example also modems and not only serial ports. Also with hardware identifier known for the device, the COM port number can be found out more quickly directly as demonstrated above.

DeviceInstance标识符通常以递增号结尾,如果多个具有相同供应商和产品标识符的设备同时连接在不同的USB端口上.

The DeviceInstance identifier often ends with an incrementing number in case of several devices with same vendor and product identifier are connected at the same time on different USB ports.

因此,要支持连接在同一台计算机上的多个Prolific USB转串口转换器,最好先查询硬件标识符以获取所有设备实例标识符.

So for supporting multiple Prolific USB-to-Serial converters connected on same computer it would be better to query first just the hardware identifier to get all device instance identifiers.

这是ATEN USB到串行桥适配器的一个非常简单的示例:

Here is a very simple example for ATEN USB to Serial Bridge adapter:

@echo off
setlocal EnableExtensions
set "HardwareID=VID_0557&PID_2008"
set "RegistryPath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB"
set "ProductName=ATEN USB to Serial Bridge"
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

注意:这两个批处理代码示例均不检查每个找到的设备当前是否确实已连接到计算机.这将需要额外的代码,从而即使使用当前在HKLM\HARDWARE\DEVICEMAP\SERIALCOMM下注册的值进行交叉检查也是不够的,因为在拔出(虚拟)串行COM端口设备时该注册表项不会自动更新.仅当插入串行COM端口设备时,它才会始终更新.

Note: Both batch code examples do not check if each found device is really currently connected to the computer. That would require additional code whereby even cross-checking with values registered currently under HKLM\HARDWARE\DEVICEMAP\SERIALCOMM is not enough as this registry key is not automatically updated when a (virtual) serial COM port device is unplugged. It is only always updated when a serial COM port device is plugged in.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • cls /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • reg /?
  • reg query /?
  • rem /?
  • set /?
  • setlocal /?
  • shift /?
  • call /?
  • cls /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • reg /?
  • reg query /?
  • rem /?
  • set /?
  • setlocal /?
  • shift /?

这篇关于如何从查询中检索Serialcomm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆