windowspowershellserial-portfindbaud-rate

Windows CMD - Find current baud rate of COM port


I am trying to parse the out put of MODE command in command prompt and assign it to a variable. The out put of mode is as shown below,

PS C:\Users\test> mode %COMPORT%

Status for device COM5:
-----------------------
    Baud:            9600
    Parity:          None
    Data Bits:       8
    Stop Bits:       1
    Timeout:         OFF
    XON/XOFF:        OFF
    CTS handshaking: OFF
    DSR handshaking: OFF
    DSR sensitivity: OFF
    DTR circuit:     OFF
    RTS circuit:     OFF

I'm trying to get the first line using FIND as shown below,

mode COM5 | find /I "Baud"

it says FIND: Parameter format not correct

btw, this is how the whole code looks like,

@echo off
for /f "tokens=3" %%a in (
    'REG QUERY HKLM\HARDWARE\DEVICEMAP\SERIALCOMM'
) do set "COMPORT=%%a" 
echo %COMPORT%

for /f "tokens=2" %%a in (
    'MODE %COMPORT% | FIND /I "Baud"'
) do set "SPEED=%%a" 
echo %SPEED%

But this is not working, what am i doing wrong?


Solution

  • Eventhough its still unknown to me why the same command works in a normal console but not in a powershell console, the problem with the script was entirely at a different place. Inside the for loop you need to escape the | character. It was not the FIND command's Parameter format not correct error the real problem, (a false alarm), but the escaping needed for the |

    Corrected script is as shown below.

    @echo off
    for /f "tokens=3" %%a in (
        'REG QUERY HKLM\HARDWARE\DEVICEMAP\SERIALCOMM'
    ) do set "COMPORT=%%a" 
    echo %COMPORT%
    
    for /f "tokens=2" %%a in (
        'MODE %COMPORT% ^| FIND /I "Baud"'
    ) do set "SPEED=%%a" 
    echo %SPEED%