我正在尝试从串行端口读取字符串并将其保存到VAR中,如下所示:
set /p y= < %COM%
到目前为止没有成功。
我能够通过将字符串保存到 .txt 文件中并将其内容读取到变量中来做到这一点,但没有 .txt 文件我就无法做到这一点,我真的需要这样做。
::This works:
type %COM% > sample.txt
set /p y= < sample.txt
del sample.txt
请您尝试以下方法:
for /f "usebackq" %%a in (`type %COM%`) do (
set y=%%a
)
echo %y%
将命令输出捕获为变量可能是一个常见的习惯用法。(类似于bash
中的var=$(command)
(
help for
说:
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.
它有几个选项,help for
将提供信息。