npat-efault / picocom

Minimal dumb-terminal emulation program
GNU General Public License v2.0
645 stars 126 forks source link

How to write automated scripts for picocom #76

Closed alexzaitsev closed 6 years ago

alexzaitsev commented 6 years ago

Hello, @npat-efault I'd like to execute a list of commands after entering into picocom terminal. This list is stored as an usual script on my host computer. Is it possible to write an automated script which will do the following:

  1. enter to picocom terminal (that's easy one)
  2. execute a list of commands there Ideally, step 2 should be able to be repeated any number of times.

Why I need that? I want to write files to my esp8266 using micropython. So when I connect to my esp using picocom, i can open a file in python command line and write my file line-by-line there. But I have to paste every command (every line) by hand. I want to do that automatically.

Hope you have grasped my idea. Is it possible?

npat-efault commented 6 years ago

You have to think of exactly what you want to achieve, and see if it is possible or not (and how) using picocom and/or other commands. There are tools for scripting complex interactions with host systems, maybe better suited than picocom, or that could be used together with picocom...

What does it mean to send commands "line by line"? Wait a certain amount of time between lines? Wait for a response after each line? Check the response? What about remote echo? You can certainly blast the contents of the file to the port using picocom. You could maybe do it with multiple invocations of picocom, one per line. Maybe this is what you want, but maybe not. Depends on exactly what you want to achieve. There is detailed doc about how picocom behaves, so you should figure it out.

Keep in mind that picocom is primarily for interactive operations, and only very trivial non-interactive stuff. It is not a tool for scripting complex interactions (like "chat", or "expect", or others), though it could be used together with them (e.g. configure the port, possibly allow some manual interaction, and then hand over the scripted interaction to them)

npat-efault commented 6 years ago

Also, picocom does not "execute commands". Picocom configures serial ports, sends stuff to them, and receives stuff from them...

npat-efault commented 6 years ago

As a hypothetical example (and only as such): A simple thing you could do would be smth like:

cat cmd.txt | picocom -qrx 1000 /dev/ttyS0 > foo.txt

This will open and configure the port, write the contents of cmd.txt to the port, write anything comming from the port to foo.txt, until the line becomes idle for 1 sec... It will then close the port, without resetting it, and exit. You would of-course have to also think about line terminations (CR vs LF vs CRLF) and decide if some mapping is required, and so on...

Is this what you want? I have no idea...

npat-efault commented 6 years ago

Another, slightly more elaborate, example could be something like this:

#!/bin/bash

# barf on error
set -e
# configure port, exit immediately, don't reset
picocom -qrX -b 115200 ... other settings ... /dev/ttyS0
# send contents of cmd.txt, line by line, 
# waiting 1 sec in-between, after host sends reply
while read ln; do
     echo "$ln" | picocom -qrix 1000 /dev/ttyS0
done < cmd.txt

Caveats mentioned above still apply here, of-course. So it may, or may-not, be what you want. Again, as things get more complicated, you may consider other, better suited, tools (possibly together with picocom)...

npat-efault commented 6 years ago

I hope I answered your question...

alexzaitsev commented 6 years ago

@npat-efault yep, thank you so much, that's exactly I what needed. I've tried to run your script but got an error:

Unrecognized option(s)
Run with '--help'.

Could you help me a bit more?

npat-efault commented 6 years ago

Did you copy the script verbatim? There is a figurative ... other settings ... in it, which you should obviously remove. If this is not the problem then make sure you are using the latest release of picocom (or the tip from git). Anything before 3.0 will not do.

alexzaitsev commented 6 years ago

I use this one:

#!/bin/bash

# barf on error
set -e
# configure port, exit immediately, don't reset
picocom -qrX -b115200 /dev/$(ls /dev | grep cu.wchusbserial)
# send contents of cmd.txt, line by line, 
# waiting 1 sec in-between, after host sends reply
while read ln; do
     echo "$ln" | picocom -qrix 1000 /dev/$(ls /dev | grep cu.wchusbserial)
done < cmd.txt

and picocom v2.2

npat-efault commented 6 years ago

Ah, picocom 2.2 will not do! Get 3.0, or the tip from git. The non-interactive options (like -x and -q and -X) were added after 2.2.

alexzaitsev commented 6 years ago

@npat-efault got it! Thanks again

alexzaitsev commented 6 years ago

@npat-efault yeah, it works! Could I grab terminal output also in some way?

alexzaitsev commented 6 years ago

and one more question: what 'q' option means? cannot find it in the help

npat-efault commented 6 years ago

Sure, replace:

 echo "$ln" | picocom -qrix 1000 /dev/ttyS0

with something like:

 echo "$ln" | picocom -qrix 1000 /dev/ttyS0 >> output.txt

or (if you want to capture to a shell variable):

output=$(echo "$ln" | picocom -qrix 1000 /dev/ttyS0)

Keep in mind, though, that this will capture all output (incl. the remote echo)

npat-efault commented 6 years ago

Ooops, your right! Option -q is missing from the help message. It is, though, in the manual. It means --quiet (see man for details).

alexzaitsev commented 6 years ago

Thanks! Eventually i have

#!/bin/bash

# barf on error
set -e
# configure port, exit immediately, don't reset
picocom -qrX -b115200 /dev/$(ls /dev | grep cu.wchusbserial)
# send contents of cmd.txt, line by line, 
# waiting 1 sec in-between, after host sends reply
while read ln; do
     echo "$ln" | picocom -qrix 1000 /dev/$(ls /dev | grep cu.wchusbserial) >> output.txt
done < cmd.txt

cmd.txt:

print("hello")

It is executed smoothly but output.txt is still empty..

npat-efault commented 6 years ago

It works fine with me, as it should. Even without the redirection to output.txt you should see: the print("hello") command (remote echo), the hello output, and the next prompt. Are you sure the command is executed?

alexzaitsev commented 6 years ago

Finally I managed to find a 'right' way of copying a file. It's ampy. Anyway thanks for a quick response, I wish such support to all open-sourced products!

npat-efault commented 6 years ago

I do this:

The contents of output.txt are, exactly:

print("hello")
hello
>>>

without a newline at the end, as it should. I don't know what goes wrong in your case, but it is certainly not picocom-related. It probably has something to do with the specific python REPL running on your board and how it interprets input (e.g. CR vs LF issues, or something like that? I don't know...)

bbatten1 commented 5 years ago

npat-efault: Thanks for the script! It solves a problem I've been working on for most of this week. Just goes to show that four days experimenting is as good a a half hour in the library any day.

Thanks again,

bakhretdino commented 4 years ago

Hello!

Thanks for the solution. I faced one problem, not sure if it's related with this topic. echo into picocom terminal doesn't work properly. For example, I run: echo "hello" | picocom -qrix 100 $PATH_TO_DEV, and picocom terminal receives "hlo". So basically it swallows every other character. Any idea how to fix that? Thank you!

omkumar1979 commented 4 years ago

Ah, picocom 2.2 will not do! Get 3.0, or the tip from git. The non-interactive options (like -x and -q and -X) were added after 2.2.

unable to upgrade picocom to v3.0. pl. let me know the alternate way (tip from git.) as mentioned above.

ElectricRCAircraftGuy commented 2 years ago

@omkumar1979 , try my personal picocom installation instructions here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_apps/serial_terminals_README.md#installation

Let me know if they work for you.

armtir commented 1 year ago

For AT commands use:

echo -ne "$ln\r"