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

1

2

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

yong.cheng

Posted 2018-10-15T02:49:55.060

Reputation: 11

2What 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. – Scott – 2018-10-15T03:51:16.520

Answers

1

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

Martin Prikryl

Posted 2018-10-15T02:49:55.060

Reputation: 13 764

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. – yong.cheng – 2018-10-17T06:44:53.237

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. – Martin Prikryl – 2018-10-17T06:46:49.530

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 – 2018-10-17T06:52:23.783

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 – 2018-10-17T06:54:30.500

There's no way to pass commands to PuTTY. PuTTY is for an interactive use, not for automating command execution. – Martin Prikryl – 2018-10-17T06:56:05.290

What does it mean "the commands can't excute" - How does that manifest? – Martin Prikryl – 2018-10-17T06:58:08.640

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. – yong.cheng – 2018-10-17T07:46:34.713

OK, what do you need the timeout command for? – Martin Prikryl – 2018-10-17T07:51:35.033

now I didn't use the timeout command, I just send one command "set_a 65535" . – yong.cheng – 2018-10-17T07:55:01.057

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. – yong.cheng – 2018-10-17T07:58:54.807

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). – Martin Prikryl – 2018-10-17T08:10:49.323

yes, the format is the same with you mentioned above, but it still output invalid commands and no response with my input commands. – yong.cheng – 2018-10-17T08:23:37.110

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. – Martin Prikryl – 2018-10-17T08:25:38.047

0

According to documentation the correct usage is:

plink -load test -m commands.txt

-m = Read remote commands from a file.

https://www.ssh.com/ssh/putty/putty-manuals/0.68/Chapter7.html#plink-usage

Although, I’m questioning what these commands are being used for. It almost looks like the contents of the commands.txt file is another batch file and that you possibly expect that file to execute and the output to be fed to the serial port.

That is not how it works. The literal contents of commands.txt is being sent to the serial port, possibly resulting in all the invalid command errors.

If you want to execute the contents of commands.txt and send the output to the serial port you should rename commands.txt to commands.bat and pipe the output to plink using the following command: commands.bat | plink -load test

Appleoddity

Posted 2018-10-15T02:49:55.060

Reputation: 9 360

@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. – Appleoddity – 2018-10-15T05:06:13.177

1-m does not work with serial port, only with SSH. – Martin Prikryl – 2018-10-15T07:33:21.557