0

I have a pretty simple bash script that sends command to serial and reads thee value back. The problem is when I don't get a value back,, it can get stuck.

echo BC > /dev/ttyS1
read line < /dev/ttyS1
echo $line

I have used the cat command with a timeout, but cannot use the same technique with 'read', because if I send the process to the background, I never get value back on exit. 'cat' works for the most part, but i'm not sure if this is the most robust way to do this.

echo BC > /dev/ttyS1
cat /dev/ttyS1 &
    pid=$!
    sleep 0.1
    kill -9 $pid
2
  • 3
    Does the read -t flag do what you want? Commented Dec 16, 2014 at 15:50
  • oh thanks man! I was so hung up on cat and kill $pid, that I didn't even read the man page properly! Yea, 'read -t 1 line < /dev/ttyS1' works good. Commented Dec 16, 2014 at 16:26

1 Answer 1

3

From section 4.2 Bash Builtin Commands of the Bash Reference Manual:

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name …]

...

-t timeout

Cause read to time out and return failure if a complete line of input is not read within timeout seconds. timeout may be a decimal number with a fractional portion following the decimal point. This option is only effective if read is reading input from a terminal, pipe, or other special file; it has no effect when reading from regular files. If timeout is 0, read returns success if input is available on the specified file descriptor, failure otherwise. The exit status is greater than 128 if the timeout is exceeded.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.