Losing bytes in RX buffer?

I have a problem with ttyO4. It looks like something is emptying the uarts buffer.

When is run two bash scripts at the same time I can send characters and receive characters at the same time. The two files running parallel look like this (simplified):

1:

for ((x=0;x<100;x++)); do echo -en '\xAA' > /dev/ttyO4 done

2:

for ((x=0;x<100;x++)); do read -n 1 -t 1 < /dev/ttyO4 done

Number 1 is sending and I receive the 0xAA at number 2.

Now I try to combine these two files:

for ((x=0;x<100;x++)); do echo -en '\xAA' > /dev/ttyO4 read -n 1 -t 1 < /dev/ttyO4 done

This does not work. So I write one byte and I am to late to receive this byte. When I use the bash script reading parallel it works again.

The 0xAA received at the RX, where does it go? Why isn’t it in the Beaglebones (hardware) buffer? Is there some linux process reading these bytes before my own read (from the bash file) is ready to read?

read </dev/ttyO4 opens the file and after the command is done it is closed.
The UART throws away characters received while the tty is closed.

What you can do is
for … do
read
done </dev/ttyO4

Thanks Nikolaus, tonight I will give it a try.

But where do I place the echo? Without an echo to write some character there is nothing to read.

for ((x=0;x<100;x++)); do
echo -en ‘\xAA’ > /dev/ttyO4
read -n 1 -t 1 < /dev/ttyO4

done < /dev/ttyO4

Something like this? Just add the “< /dev/ttyO4” at the bottom to hold the file descriptor open?

Thanks Nikolaus, tonight I will give it a try.

But where do I place the echo? Without an echo to write some character there is nothing to read.

for ((x=0;x<100;x++)); do
echo -en ‘\xAA’ > /dev/ttyO4
read -n 1 -t 1 < /dev/ttyO4

remove the < /dev/ttyO4 here or you have it twice

done < /dev/ttyO4

Something like this? Just add the “< /dev/ttyO4” at the bottom to hold the file descriptor open?

you can also move the > /dev/ttyO4 to the end of the do.

Basically this tells the shell to redirect for all commands inside the loop.

Thanks, got it working now this way.