Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Shell io from and to serial device



On Wed, Feb 13, 2019 at 4:01 PM Dominik Vogt <dominik.vogt@xxxxxx> wrote:
>
>   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

You might get better results using "printf" rather than "echo",
depending on how precise you need to be about what is sent to the
device (where newlines appear, etc.).

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

I think what you want to do here is use "read -E" combined with
"zselect -t" (assuming you have a recent zsh where zselect accepts
hundredths of a second timeouts).

I have a bit of trouble following your example script (what the heck
is that "cat <<EOF" doing in there?), and you haven't said anything
about the "protocol" used on the serial device (is every write by
either side supposed to end with a newline, for example?) so it's hard
to be specific, but something like:

trap "kill 0 2>/dev/null" EXIT
stty -F "$DEV" speed 115200 cs8 -cstopb -parenb -echo > /dev/null
{ zselect -t 1 -w 1 && printf "%s\n" "$SEND" > "$DEV" } &
# $TIMEOUT has to be adjusted to be 100ths before doing this;
# adjusted $TIMEOUT better be > 1 or this will always fail
coproc { zselect -t $TIMEOUT -r 0 || kill 0 2>/dev/null }
# This assumes the "other end" always sends full lines
while read -E LINE >> "$LOGFILE"; do
  print -r -- "$LINE"
done | grep --max-lines=1 --quiet "$EXPECT"
# If not exiting here, "print -p $?" to end the coproc
exit $?



Messages sorted by: Reverse Date, Date, Thread, Author