zsh-users
 help / color / mirror / code / Atom feed
From: Dominik Vogt <dominik.vogt@gmx.de>
To: Zsh Users <zsh-users@zsh.org>
Subject: Shell io from and to serial device
Date: Thu, 14 Feb 2019 01:00:42 +0100	[thread overview]
Message-ID: <20190214000042.ed73yom5s26pst5p@gmx.de> (raw)

Hi Folks,

I'm really stuck with this problem.  We need a fast, and reliable
way to send a string to a serial device and grep for a certain
reply, and failed to make that work with "expect" - because its
documentation is awful.  Anyway, it should be not too difficult to
do with zsh.  The task is:

  1. configure serial line, e.g. with

    $ stty -F /dev/ttyUSB0 speed 115200 cs8 -cstopb -parenb -echo

  2. Send some string to the serial line

    $ echo foo > /dev/ttyUSB0

  3. Grep for a certain reply with configurable, sub second timeout.

    $ ??????

  4. In case of a timeout, all input from the serial line is stored
     in a file or shell variable.

There should be no noticeable delay if the expected reply shows
up immediately.

Something like this should do it:

-- snip --
#!/usr/bin/zsh

set -u
set -C

SEND="$1"
EXPECT="$2"
TIMEOUT="$3"
DEV="$4"
LOGFILE="$5"
shift 5

trap "exit 1" TERM

# serial setup
stty -F "$DEV" speed 115200 cs8 -cstopb -parenb -echo > /dev/null
# watchdog process
{ sleep "$TIMEOUT"; echo "timeout" 1>&2; kill 0 2> /dev/null; } &
# child sends string with delay
{ sleep 0.01; echo "$SEND" > "$DEV" } &
rm -f "$LOGFILE"
# parent waits for reply
tee "$LOGFILE" < "$DEV" |
while read LINE; do
	if { cat <<EOF
$LINE
EOF
} | grep -q "$EXPECT"
	then
		trap : TERM
		kill 0 2> /dev/null
		exit 0
	fi
done
exit 1
-- snip --

But this looks really complicated and the "sleep 0.01" is
annoying.  There must be an easier way.

Also, I'm not convinced that "tee" won't buffer serial input and
cause a timeout, although on the test machine it doesn't.

Ciao

Dominik ^_^  ^_^

-- 

Dominik Vogt

             reply	other threads:[~2019-02-14 13:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-14  0:00 Dominik Vogt [this message]
2019-02-14  0:56 ` Ben Klein
2019-02-15  1:02 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190214000042.ed73yom5s26pst5p@gmx.de \
    --to=dominik.vogt@gmx.de \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).