Executing multiple commands over serial port with PuTTY with a .bat file with a delay between individual commands

6,926

Your commands.txt file is just a mess.

I assume you want to send set_zero command, not echo set_zero command, so put set_zero to the file, not echo set_zero.

Though if you need to pause between the commands, a simple input redirection won't help, as you cannot use timeout command then. You want to execute timeout command locally, while you are sending it to the serial port. Your code is actually correct, had you used it as a script that generates an input for the plink. But for that, you need to use | not <.

This might do what you want:

(
    echo set_zero
    timeout /t 3 /nobreak >nul 2>&1
    pause >nul 2>&1
    echo set_a 65535
    pause >nul 2>&1
    timeout /t 3 /nobreak >nul 2>&1
    echo set_a 0
    timeout /t 3 /nobreak >nul 2>&1
    pause >nul 2>&1
) | plink -load test

Though, you might want to move pause only after plink:

(
    ...
    echo set_a 0
    timeout /t 3 /nobreak >nul 2>&1
) | plink -load test

pause >nul 2>&1

Alternativelly, as @Appleodity already suggested, rename commands.txt to commands.bat and use it like:

commands.bat | plink -load test

Again, maybe like this (after moving pause from commands.bat):

commands.bat | plink -load test

pause >nul 2>&1
Share:
6,926

Related videos on Youtube

yong.cheng
Author by

yong.cheng

Updated on September 18, 2022

Comments

  • yong.cheng
    yong.cheng over 1 year

    send.bat:

    plink -load test < commands.txt
    

    (test is a saved session in putty)

    enter image description here

    commands.txt

    echo set_zero
    timeout /t 3 /nobreak >nul 2>&1
    pause >nul 2>&1
    echo set_a 65535
    pause >nul 2>&1
    timeout /t 3 /nobreak >nul 2>&1
    echo set_a 0
    timeout /t 3 /nobreak >nul 2>&1
    pause >nul 2>&1
    

    When I run the send.bat, it can open putty seccessfully, but it can't input the commands which I write in the commands.txt.

    enter image description here

    I put these files in the same directory. enter image description here

    • Scott - Слава Україні
      Scott - Слава Україні over 5 years
      What does the serial port connect to?  What are you trying to accomplish?  Please do not respond in comments; edit your question to make it clearer and more complete.
  • Appleoddity
    Appleoddity over 5 years
    @yong.cheng then the best thing to do is try one command at a time. Simplify things and break it down until you can get one basic command to run.
  • Martin Prikryl
    Martin Prikryl over 5 years
    -m does not work with serial port, only with SSH.
  • yong.cheng
    yong.cheng over 5 years
    do you think I put these files in the same diretory is right?putty.exe is compliant with plink? you can see the picture in the question line.
  • Martin Prikryl
    Martin Prikryl over 5 years
    Yes same directory will do (and that directory should be the working directory). - I do not know what you mean by "putty.exe is compliant with plink?" - I see the picture, but I do not understand what you ask for.
  • yong.cheng
    yong.cheng over 5 years
    I mean I don't know why use plink but not putty in the send.bat ,I can send the comands successfully with bat now and I can see the received commands in another serial port,but the commands can't excute and there are no response with my commands, it is unormal.
  • yong.cheng
    yong.cheng over 5 years
    I mean I don't know why use plink but not putty in the send.bat ,I can send the comands successfully with bat now and I can see the received commands in another serial port,but the commands can't excute and there are no response with my commands, it is unormal.
  • Martin Prikryl
    Martin Prikryl over 5 years
    There's no way to pass commands to PuTTY. PuTTY is for an interactive use, not for automating command execution.
  • Martin Prikryl
    Martin Prikryl over 5 years
    What does it mean "the commands can't excute" - How does that manifest?
  • yong.cheng
    yong.cheng over 5 years
    got it! Normally,enter the conmands will output some information with the voltage,and the correspoding LED will light on or off, but after runing the bat, there are no response.
  • Martin Prikryl
    Martin Prikryl over 5 years
    OK, what do you need the timeout command for?
  • yong.cheng
    yong.cheng over 5 years
    now I didn't use the timeout command, I just send one command "set_a 65535" .
  • yong.cheng
    yong.cheng over 5 years
    use the timeout is just ensure the last command is fully completed, and then start the next one command, in order to find the failed cause, I just send one command at one time.
  • Martin Prikryl
    Martin Prikryl over 5 years
    OK, so if you send just one command, does it work or not? With one command, you can also try simpler plink ... < command.txt with command.txt containing just set_a 65535 (not echo set_a 65535).
  • yong.cheng
    yong.cheng over 5 years
    yes, the format is the same with you mentioned above, but it still output invalid commands and no response with my input commands.
  • Martin Prikryl
    Martin Prikryl over 5 years
    OK, so you are not able to automate execution of even one command. So maybe you should ask a separate question for that. And once you resolve execution a single command, we can come back here to resolve execution of multiple commands.