get serial com port description in windows batch

一曲冷凌霜 提交于 2019-12-22 00:05:05

问题


I want to retrieve serial com port information as device manager shows. For example, "Intel(R) Active Management Technology - SOL (COM3)". I know use command "mode" to retrieve serial port list, but there is no description like "Intel(R) Active Management Technology - SOL". How do I get this information in windows batch file?


回答1:


Updated answer

... was provided by the original asker in the comments below. Apparently Win32_SerialPort doesn't include USB->Serial devices. William's solution to this is to query Win32_PnPEntity instead.

Just so I still feel useful, here's a way to scrape that list and set each line into a variable:

@echo off
setlocal

:: wmic /format:list strips trailing spaces (at least for path win32_pnpentity)
for /f "tokens=1* delims==" %%I in ('wmic path win32_pnpentity get caption /format:list ^| find "COM"') do (
    call :setCOM "%%~J"
)

:: display all _COM* variables
set _COM

:: end main batch
goto :EOF

:setCOM <WMIC_output_line>
:: sets _COM#=line
setlocal
set "str=%~1"
set "num=%str:*(COM=%"
set "num=%num:)=%"
set str=%str:(COM=&rem.%
endlocal & set "_COM%num%=%str%"
goto :EOF

Original answer

Use wmic with Win32_SerialPort, something like this:

@echo off
setlocal

for /f "delims=" %%I in ('wmic path Win32_SerialPort get DeviceID^,Caption^,Description^,Name^,ProviderType /format:list ^| find "="') do (
    set "%%I"
)

echo %DeviceID%
echo %Caption%
echo %Description%
echo %Name%
echo %ProviderType%

See the documentation for Win32_SerialPort to see what other properties you can query. I'll leave it as an exercise for you to find a way to set the variables uniquely so you aren't overwriting them with iterations over each COM port. Enjoy!




回答2:


Please use mode command to get details of available COM ports..



来源:https://stackoverflow.com/questions/27772861/get-serial-com-port-description-in-windows-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!