Executing multiple commands over serial port with PuTTY with a .bat file with a delay between individual commands
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
Related videos on Youtube
yong.cheng
Updated on September 18, 2022Comments
-
yong.cheng over 1 year
send.bat:
plink -load test < commands.txt
(test is a saved session in putty)
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.
I put these files in the same directory.
-
Scott - Слава Україні over 5 yearsWhat 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 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 over 5 years
-m
does not work with serial port, only with SSH. -
yong.cheng over 5 yearsdo 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 over 5 yearsYes 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 over 5 yearsI 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 over 5 yearsI 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 over 5 yearsThere's no way to pass commands to PuTTY. PuTTY is for an interactive use, not for automating command execution.
-
Martin Prikryl over 5 yearsWhat does it mean "the commands can't excute" - How does that manifest?
-
yong.cheng over 5 yearsgot 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 over 5 yearsOK, what do you need the
timeout
command for? -
yong.cheng over 5 yearsnow I didn't use the timeout command, I just send one command "set_a 65535" .
-
yong.cheng over 5 yearsuse 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 over 5 yearsOK, so if you send just one command, does it work or not? With one command, you can also try simpler
plink ... < command.txt
withcommand.txt
containing justset_a 65535
(notecho set_a 65535
). -
yong.cheng over 5 yearsyes, the format is the same with you mentioned above, but it still output invalid commands and no response with my input commands.
-
Martin Prikryl over 5 yearsOK, 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.