admin管理员组文章数量:1277380
I have an old batch file that start an exe passing some hardcoded parameters, and I need to make it more flexible.
One of those parameters is the serial port number to use, and what I want now is to prompt the used to pick the right one.
I've found this great answer to find and show the ports descriptions. On my PC, it shows this list:
_COM1=Porta di comunicazione
_COM2=Porta di comunicazione
_COM5=USB Serial Port
_COMRealtek RealManage COM1=Realtek RealManage COM1
_COMRealtek RealManage COM2=Realtek RealManage COM2
What i would like is:
- Assign the right number to each port (in my case: 1,2 and 5)
- Allow the user to choose one of them and store the number in a variable (checking if the answer is correct, or ask again)
- Optionally, get rid of the last, duplicate ports listed
So, in my case, i would like to show to the user something like this:
Please select what COM port to use:
1 - COM1=Porta di comunicazione
2 - COM2=Porta di comunicazione
5 - COM5=USB Serial Port
or press ESC to exit.
How could it be done?
I have an old batch file that start an exe passing some hardcoded parameters, and I need to make it more flexible.
One of those parameters is the serial port number to use, and what I want now is to prompt the used to pick the right one.
I've found this great answer to find and show the ports descriptions. On my PC, it shows this list:
_COM1=Porta di comunicazione
_COM2=Porta di comunicazione
_COM5=USB Serial Port
_COMRealtek RealManage COM1=Realtek RealManage COM1
_COMRealtek RealManage COM2=Realtek RealManage COM2
What i would like is:
- Assign the right number to each port (in my case: 1,2 and 5)
- Allow the user to choose one of them and store the number in a variable (checking if the answer is correct, or ask again)
- Optionally, get rid of the last, duplicate ports listed
So, in my case, i would like to show to the user something like this:
Please select what COM port to use:
1 - COM1=Porta di comunicazione
2 - COM2=Porta di comunicazione
5 - COM5=USB Serial Port
or press ESC to exit.
How could it be done?
Share Improve this question asked Feb 25 at 12:10 ParduzParduz 6025 silver badges26 bronze badges 18 | Show 13 more comments2 Answers
Reset to default 0@ECHO OFF
SETLOCAL
cls
SET /a menu=0
SET "choices="
for /f "tokens=1* delims==" %%I in ('wmic path win32_pnpentity get caption /format:list ^| findstr /e /R "(COM.)"') do SET "name="&CALL :process %%J
choice /c q%choices% /M "Choose a number or Q to quit"
SET /a choicemade=%errorlevel%-1
IF %choicemade%==0 GOTO :eof
for /f "tokens=1* delims==" %%I in ('SET menuline') DO IF /i "%%I"=="menuline%choicemade%" ECHO choice made was (%choicemade%) %%J
GOTO :EOF
:process
SET "name=%name% %~1"
IF "%~2" neq "" shift&GOTO process
SET "comport=%~1"
SET "comport=%comport:~1,-1%"
SET /a menu+=1
SET "choices=%choices%%menu%"
ECHO %menu%: %comport%%name%
SET menuline%menu%=%comport%%name%
GOTO :eof
No code, so no explains.
I would suggest the following solution:
@echo off
:request-port
echo Please select what COM port to use:
echo 1: COM1=Porta di comunicazione
echo 2: COM2=Porta di comunicazione
echo 5: COM5=USB Serial Port
echo or enter "exit" to exit
set /p port=Port (1, 2, or 5)
if "%port%"=="1" ( goto :continue )
if "%port%"=="2" ( goto :continue )
if "%port%"=="5" ( goto :continue )
if "%port%"=="exit" ( goto :eof )
goto :request-port
:continue
echo Selected port: %port% & :: Start your application here instead
In place of the last line, you can start your application and pass %port%
to its command line.
Note that & ::
starts an end-of-line comment.
I tested it, everything works correctly.
Unfortunately, I don't think there is a simple way to detect Escape. Instead, I suggested entering exit
. Well, the features of the batch language are badly limited.
However, there is a more flexible solution based on subroutines and call :label
, a newer feature of the batch language. Note that goto :eof
works like return
.
@echo off
call :request-port
if "%port%"=="exit" ( goto :eof )
echo Selected port: %port% & :: Start your application here instead
goto :eof
:request-port
echo Please select what COM port to use:
echo 1: COM1=Porta di comunicazione
echo 2: COM2=Porta di comunicazione
echo 5: COM5=USB Serial Port
echo or enter "exit" to exit
set /p port=Port (1, 2, or 5):
if "%port%"=="1" ( goto :eof )
if "%port%"=="2" ( goto :eof )
if "%port%"=="5" ( goto :eof )
if "%port%"=="exit" ( goto :eof )
goto :request-port
To overcome the limitations of the batch language, you may think of using some command-line scripting alternatives, such as PowerShell, node.js, Python, and the like. Unfortunately, only .cmd files work out-of-box, everything else requires installation and setting up your environment.
本文标签: Windows Batch to select a COM port numberStack Overflow
版权声明:本文标题:Windows Batch to select a COM port number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741203460a2357685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
search
facility with the edit box and the magnifying glass on the very top line ? :D – Magoo Commented Feb 25 at 14:40