Nathan Almeida
Nathan Almeida

Reputation: 199

Read COM with a batch script

I'm trying to read a a string from a serial port and save it into a VAR like this:

set /p y= < %COM%

No success so far.

I was able to do it by saving the string into a .txt file and read its content into a variable, but I can't do it without the .txt file, and I really need to.

::This works:
type %COM% > sample.txt 
set /p y= < sample.txt
del sample.txt 

Upvotes: 1

Views: 487

Answers (1)

tshiono
tshiono

Reputation: 22012

Would you please try the following:

for /f "usebackq" %%a in (`type %COM%`) do (
    set y=%%a
)
echo %y%

It may be a common idiom to capture command output as a variable. (similar to var=$(command) in bash)

help for says:

FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]
(snip)
   usebackq        - specifies that the new semantics are in force,
                     where a back quoted string is executed as a
                     command and a single quoted string is a
                     literal string command and allows the use of
                     double quotes to quote file names in
                     file-set.

It has several options and help for will be informative.

Upvotes: 3

Related Questions