From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 6db92b33 for ; Thu, 14 Feb 2019 12:03:43 +0000 (UTC) Received: (qmail 25120 invoked by alias); 14 Feb 2019 00:56:57 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 23868 Received: (qmail 1603 invoked by uid 1010); 14 Feb 2019 00:56:57 -0000 X-Qmail-Scanner-Diagnostics: from mail-wr1-f50.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.100.2/25112. spamassassin: 3.4.2. Clear:RC:0(209.85.221.50):SA:0(-2.0/5.0):. Processed in 2.57349 secs); 14 Feb 2019 00:56:57 -0000 X-Envelope-From: robobenklein@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=fGSrCexoMoNxXHsrpJsbNgnEU6GoKYFbpJ14RDqask4=; b=BQi71UxUtKbj8X1mRX2zVvsVzkbc40KCNwe/VJMfoYMbvFHu26wZ7cYm//nafZK3jY b0K6xLPURbC579AHeVlBimoI31tgAmNEyMzXD0XJVn32wYdBWxA9EHp1/9ZcTvy54c/+ AJm85wI7wnSj42cK3KuKEN+p9MWe8dD+9ou3Bgyjxa0CSXol0uZ97QioptHOK0+PbBl4 u80oRhjiphfbt5w5w6+fm+jCWsOjlKLqmGv+eggTrcDMI79CrZ6WN+BCtvl40fOkWrUU Qzo6/eoMK67hAeUWwGKsFiJGY8ITbSg8CiBci3qmoebFEkiky4uXvbbCDRKd2HUDgAUO i8wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=fGSrCexoMoNxXHsrpJsbNgnEU6GoKYFbpJ14RDqask4=; b=Q30ayeL1gerksRUYkGs5tbsWUdWghBItM4/4d877h8z56OslD6BeTAQPHssPJIMN6r b53mvFuFMYfqkbTSin3akb2VgK9oWFCHaNArJ5ONNrt6OacP8Dhrb++3WIPaujd10rLv Y3h9w9dn2OZYibB5WeRSOuh+wZ20GPZj3Jxn73rxu5aH1qips+LS3porM/UObFIIcVp+ Kp3+2tPsUJXzNUS0C0F2ZTQLsRN3tJ3J+Naayt3lrI8ymcU+l9jwheDC4dIGcC5C42Fq Qs8k7TxUZqVU8o2RIkMOYyLxGXC8qAq1zlFu6y2wrDHR3J7eL/0EzKYbfjJj8Q3Jdyhf Yqgw== X-Gm-Message-State: AHQUAuZR4b7AQwSBDjFhDlM6qR2FdLfI6qBO+L2/9w0qHM1mHVkAPmcP ZJq4HG4S377kOA6wPDKTs7TtMAWmkSmZftLtEe8= X-Google-Smtp-Source: AHgI3IYRiYBX0CfghO1JCC5288yiNbz0G40zNo2EClH24R12kkZ/Z7Ezlg2buDD+3F7m/K+vPbwafVz16DNA3Y61MGY= X-Received: by 2002:adf:b741:: with SMTP id n1mr527243wre.287.1550105810234; Wed, 13 Feb 2019 16:56:50 -0800 (PST) MIME-Version: 1.0 References: <20190214000042.ed73yom5s26pst5p@gmx.de> In-Reply-To: <20190214000042.ed73yom5s26pst5p@gmx.de> From: Ben Klein Date: Wed, 13 Feb 2019 19:56:38 -0500 Message-ID: Subject: Re: Shell io from and to serial device To: dominik.vogt@gmx.de, Zsh Users Content-Type: multipart/alternative; boundary="0000000000002ef4520581d02289" --0000000000002ef4520581d02289 Content-Type: text/plain; charset="UTF-8" I don't know if you have this available, but `timeout` is a common utility program: https://manpages.ubuntu.com/manpages/cosmic/en/man1/timeout.1.html (It's GNU) So you could start the whole grep pipeline with that command in a new shell, or if you wanted you could do a callback to a parent ZSH process like the zsh-async does with SIGWINCH, catch that and kill a job. (You could enable job control then kill %0 or similar when you get the interrupting signal back.) On Wed, Feb 13, 2019 at 7:01 PM Dominik Vogt wrote: > 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 < $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 > -- *\Ben Klein* Founder and Owner of Robosane, robobenklein@robosane.net You can find me elsewhere online as 'robobenklein'. If you need to contact me securely, I am also reachable via GPG, or on Keybase. --0000000000002ef4520581d02289--