Ryan
Ryan

Reputation: 1068

Writing script to perform a series of commands over serial connection with PuTTY

I have an RFID reader that I need to download data from periodically via a serial port connection (serial to USB). I have been using PuTTY to connect to the reader. I saved a session in PuTTY with all of the correct parameters for the reader and named it "test". I also used "All session output" under Logging so that the output will be stored in a file. I access the session via the Windows 10 command line like this:

putty.exe -load "test"

and the terminal appears just like it would if I went though the GUI. I then have a series of commands that I would like to run in the PuTTY terminal which provides the output data I need. For example lets say the commands I need to run are:

DT
UH
SS
TF

I would like to automate this process somehow, so that every time I connect to the reader the same commands are run. Can a script be written to do this in PuTTY or can these commands be sent to PuTTY from the command line? Could someone demonstrate how to do this?

Upvotes: 2

Views: 5456

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202118

PuTTY is not the right tool for automation, use Plink (included in PuTTY package):
Automating command/script execution using PuTTY

You just need to know that specifying the commands on Plink command-line or with the -m switch, commonly used with Plink (the -m work even with PuTTY), works with the SSH protocol only.

With other protocols, notably with the serial connections, you can use the input redirection only (also covered in my answer to the question linked above), like this:

plink.exe -load "my serial connection" < c:\local\path\commands.txt

or

plink.exe -serial COMx -sercfg 19200,8,n,1,X < c:\local\path\commands.txt

See also Execute a command on device over serial connection with Plink.

(And this is something that you cannot do with GUI PuTTY, so for this approach, Plink is the way to go).


For reading the response, see:
Using plink to write a file to a COM port and read back the response from the target

Upvotes: 2

Related Questions