程序 return 状态时的 cmd 操作

cmd action when program return status

我有 USB 继电器,我可以通过 cmd 文件 ON.cmd 和 OFF.cmd 控制它。我想创建 TOOGLE.cmd 文件。 我可以通过发送 hidusb-relay-cmd id=YWM8T STATUS 及其 return State: R1=OFF R2=OFF 来检查中继状态。如何发送 hidusb-relay-cmd id=YWM8T ON ALL 如果 State: R1=OFF R2=OFF 和发送 hidusb-relay-cmd id=YWM8T OFF ALL 如果 State: R1=ON R2=ON?

试试这个:

@echo off
cd %tmp%
hidusb-relay-cmd id=YWM8T STATUS|findstr "OFF">sh.tmp
set "out="
set /P out=<sh.tmp
del /q /f sh.tmp
if defined out goto :def
hidusb-relay-cmd id=YWM8T OFF ALL
goto :exit
:def
hidusb-relay-cmd id=YWM8T ON ALL
:exit

工作原理:

  1. 它在 hideusb-relay-cmd 命令的输出中查找 OFF 并将其重定向到一个临时文件。
  2. 它重置 out 变量(如果你要在命令行上使用它,这是非常必要的)。
  3. 它将out设置为临时文件的内容。
  4. 删除临时文件。
  5. 它检查 out 是否有任何值。如果有,goto :def 并跳过 hidusb-relay-cmd id=YWM8T OFF ALL 命令。如果没有,它不会跳过它。
  6. 无论它走到哪里,它都会在之后退出。