Search code examples
linuxbashserial-porttty

cu does not send TTY response to bash output


I am executing the following command, opening a serial console and sending the "LM\r" command to my remote device; for which I get "LM122.0P" as an answer:

$ sudo cu -l /dev/AR2300CTRL -s 115200 -h
Connected.
LM^MLM122.0P
~~..
Disconnected.

However, when I try to read that answer as an output from cu using the following command, I do not get anythin:

$ resp=$(
    echo "LM\r" |\
    sudo cu -l /dev/AR2300CTRL -s 115200 |\
    grep -v 'cu' | grep -v 'onnected.'
)
cu: End of file on terminal
cu: child: Got signal 9
$ echo $resp

What am I missing? Shouldn't I be getting the response to the command in the "resp" variable?


Solution

  • In that pipeline, the echo command will exit as soon as it has written text into the pipe to cu and therefore the standard input to cu will be closed as soon as cu consumes the text from the echo command. That close of standard input will cause cu to terminate -- that's why it emits the "End of file on terminal" message.

    This will all happen long before the device has had a chance to write anything back to cu, so the response message is never collected and delivered into $resp.

    To fix, do something to keep cu's stdin open for long enough to allow the device response to be sent, collected and written to cu's output. An easy way to do that is to feed the pipeline from a subprocess that does a sleep for a few seconds after the echo command. Something like this:

    resp=$(
        (echo "LM\r" ; sleep 6) |\
        sudo cu -l /dev/AR2300CTRL -s 115200 |\
        grep -v 'cu' | grep -v 'onnected.'
    )