1

I have a script monitoring a serial port loopback.

while sleep 1; do echo "1" > /dev/ttyS0; done &

while read -t 2 < /dev/ttyS0; do xxxx; done

The above works, however if I just do an echo outside of the while loop and &, it doesnt work as it never gets a successful read, such as below.

echo "1" > /dev/ttyS0 #This is just to get the while loop started.

while read -t 2 < /dev/ttyS0; do 
sleep 1
echo "1" > /dev/ttyS0
done

I'm trying to send data down the serial port (any data idc) and verify I get a reply. Is there a better way of doing this? Why does the above not work?

Computable
  • 976
  • 2
  • 4
  • 16
  • 1
    Your title of "*serial port echo ...*" is confusing because you are actually only referring to the shell command **echo**. This has nothing to do with the practice of a UART or terminal re-transmitting its input. – sawdust Jun 27 '23 at 19:49
  • 1
    The serial terminal only receives and buffers input when/while it is *open*. In your problem case, the serial terminal is opened & closed by the initial **echo** command. The serial terminal is opened again for the `while read` loop, but this open starts with a clean buffer. The loopback data has been discarded. Try using the **strace** command to verify. – sawdust Jun 27 '23 at 19:56
  • @sawdust How can I keep it open? I see peole use stty to configure the port, but not sure if thta opens it as you say – Monstrosity Jun 28 '23 at 11:58
  • Try https://stackoverflow.com/questions/16489809/emulating-a-do-while-loop-in-bash – sawdust Jun 28 '23 at 23:57

0 Answers0