'Using plink to write a file to a COM port and read back the response from the target
I want to send a list of commands to a device on my Windows COM port and receive the responses back for further processing. The target device accepts write commands and query commands. When I send a write command, nothing is returned. When I send a query command, a response is returned. My file (cmd_list.txt
) will have a long list of commands like this:
wr 0x0d 0xff
rr 0x0d
The first command writes a value of 0xff
to register 0x0d
and gets no text response. The second command queries register 0x0d
and returns the value as a string.
I have a PuTTY session named "test" that has the proper baud and other parameters. From the Windows command line I run this:
plink -load test < cmd_list.txt
The communication works and I see the correct response to the query appear, but there are two problems. First, I would like to capture the responses in a text file for further post processing in Octave. Second, the command window hangs and the command doesn't finish until I enter Ctrl-C. The bigger issue is the command window hanging and the command never finishing.
I referenced another article to kill plink and used this command:
( taskkill /f /im plink.exe > nul ) | plink -load test < cmd_list.txt
When I run this, nothing gets presented to the terminal, but the process does terminate and returns to the command prompt.
I tried redirecting to an output file with this command:
( taskkill /f /im plink.exe > nul ) | plink -load test < cmd_list.txt > output.txt
But there is nothing in the output.txt file.
I referenced these articles to get my system working to this point:
- Send commands via COM port using plink and exit
- Writing script to perform a series of commands over serial connection with PuTTY
Edit: 4/29/22 Per Martin's advice, I tried combining everything to the left of the "|", but I can't get the file that contains my commands to be delivered to plink while also using the taskkill method. Here's what I tried:
(echo cmd_list.txt taskkill /f /im plink.exe > nul ) | plink -load test
But I don't get the command prompt to return. It hangs.
Edit again: 4/29/22 I entered line by line as shown on the example and the terminal responds with "More?" after each entry. Here is what my terminal process looks like:
C:\> (
More? echo cmd_list.txt
More? taskkill /f /im plink.exe > nul
More? ) | plink -load test
| was unexpected at this time
C:\>
Solution 1:[1]
To store the response to a file, just redirect plink
output:
plink ... > output.txt
For a way to terminate the plink
, see:
Send commands via COM port using plink and exit
Note that you cannot combine <
and |
in one commandline, as both produce input to the command. You have to produce all input via |
(e.g. using the echo
command), as shown in the linked question.
As @Ben Voigt commented, if you want to feed local commands contents to plink
, you have to use type
command, not echo
.
And if you need to put everything into a single line, instead of using a batch file, you can do it like:
(type cmd_list.txt && taskkill /f /im plink.exe > nul) | plink -load test
Solution 2:[2]
Just to highlight the answer from a comment above. This is the command I was looking for:
(type cmd_list.txt && taskkill /f /im plink.exe > nul) | plink -load test > output.txt
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Sanjo |