Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Why are echo and sleep reading stdin ?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
doublehp
Guru
Guru


Joined: 11 Apr 2005
Posts: 472
Location: FRANCE

PostPosted: Thu Sep 01, 2016 8:11 am    Post subject: Why are echo and sleep reading stdin ? Reply with quote

Code:
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; echo "*" ; cat ; } < <( cat /dev/ttyAMA0 )
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# {sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < <( cat /dev/ttyAMA0 )
-bash: syntax error near unexpected token `}'
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < <( cat /dev/ttyAMA0 )
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 20 ; cat ; } < <( cat /dev/ttyAMA0 )
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; echo "*" ; cat ; } < /dev/ttyAMA0
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; echo "*" ; cat ; } < /dev/ttyAMA0
*
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 20 ; cat ; } < <( cat /dev/ttyAMA0 )
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; echo "*" ; sleep 2
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 20 ; cat ; } < <( cat /dev/ttyAMA0 )
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 2
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; echo "*" ; cat ; } < /dev/ttyAMA0
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; echo "*" ; cat ; } < /dev/ttyAMA0
*
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 2 ; cat ; } < <( cat /dev/ttyAMA0 )
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; echo "*" ; sleep 2 ; cat ; } < <( cat /dev/ttyAMA0 )
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 2 ; cat ; } < <( cat /dev/ttyAMA0 )
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; echo "*" ; cat ; } < /dev/ttyAMA0
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; echo "*" </dev/null ; sleep 2 </dev/null ; cat ; } < <( cat /dev/ttyAMA0 )
*
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; echo "*" </dev/null ; sleep 2 ; cat ; } < <( cat /dev/ttyAMA0 )
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; echo "*" </dev/null ; sleep 2 </dev/null ; cat ; } < <( cat /dev/ttyAMA0 )
*
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; echo "*" </dev/null ; sleep 2 </dev/null ; cat ; } < <( cat /dev/ttyAMA0 )
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
1234567890abcdefghijklmnopqrstuvwxyz
^C


I am running out of ideas ...

8B may be the hardware buffer; but why is a part of the string eaten in some cases ?
_________________
DEMAINE Benoît-Pierre (aka DoubleHP ) http://www.demaine.info/
>o_/ Coin coin coin \_o<
to contact me (MSN,ICQ, JABBER, Skype ... ) http://benoit.demaine.info/contact.png
Back to top
View user's profile Send private message
frostschutz
Advocate
Advocate


Joined: 22 Feb 2005
Posts: 2971
Location: Germany

PostPosted: Thu Sep 01, 2016 8:35 am    Post subject: Reply with quote

They are not reading stdin...

Code:

$ echo  "1234567890abcdefghijklmnopqrstuvwxyz" | (sleep 1 ; echo -n "Data is: "; cat)
Data is: 1234567890abcdefghijklmnopqrstuvwxyz


I don't have a /dev/ttyAMA0 to play with, sorry.
Back to top
View user's profile Send private message
doublehp
Guru
Guru


Joined: 11 Apr 2005
Posts: 472
Location: FRANCE

PostPosted: Thu Sep 01, 2016 9:59 am    Post subject: Reply with quote

Why aren't they reading stdin ?
_________________
DEMAINE Benoît-Pierre (aka DoubleHP ) http://www.demaine.info/
>o_/ Coin coin coin \_o<
to contact me (MSN,ICQ, JABBER, Skype ... ) http://benoit.demaine.info/contact.png
Back to top
View user's profile Send private message
Ant P.
Watchman
Watchman


Joined: 18 Apr 2009
Posts: 5878

PostPosted: Thu Sep 01, 2016 8:30 pm    Post subject: Reply with quote

Does this go away when you use /bin/sleep and /bin/echo instead of the bash builtins?
Back to top
View user's profile Send private message
doublehp
Guru
Guru


Joined: 11 Apr 2005
Posts: 472
Location: FRANCE

PostPosted: Fri Sep 02, 2016 7:53 am    Post subject: Reply with quote

I forgot to give a very important detail: the stty.

No, system calls does not give different results from bash internals.

Code:
root@rpi-04:~# /bin/stty -F /dev/ttyAMA0 1200 sane evenp parenb cs7 -crtscts ignbrk -brkint -icrnl -opost -onlcr -isig -echo -echoe -echok -echoctl -echoke noflsh -crtscts -hupcl -ixon -ixoff -imaxbel -icanon -xcase min 1 time 0
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; echo "*" </dev/null ; sleep 2 </dev/null ; cat ; } < <( cat /dev/ttyAMA0 )
*
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { sleep 1 ; echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; echo "*" </dev/null ; sleep 2 </dev/null ; cat ; } < <( cat /dev/ttyAMA0 )
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; sleep 0 ; cat ; } < /dev/ttyAMA0
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { /bin/sleep 1 ; /bin/echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; /bin/echo "*" </dev/null ; /bin/sleep 2 </dev/null ; cat ; } < <( cat /dev/ttyAMA0 )
*
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { /bin/sleep 1 ; /bin/echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; /bin/echo "*" </dev/null ; /bin/sleep 2 </dev/null ; cat ; } < <( cat /dev/ttyAMA0 )
*
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { /bin/echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; /bin/sleep 0 ; cat ; } < /dev/ttyAMA0
90abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~# { /bin/echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; /bin/sleep 0 ; cat ; } < /dev/ttyAMA0
1234567890abcdefghijklmnopqrstuvwxyz
^C
root@rpi-04:~#



For reference, this gives completely different behaviour:

Code:
root@rpi-04:~# /bin/stty -F /dev/ttyAMA0 1200 sane evenp parenb cs7 -crtscts


But, let's do one thing at a time.

I forgot to post the original bug I had even before this: if I send stuff in the port, let say between 20 and 256 bytes, the quantity of stuff I can read on the port will vary with how much time I wait before read. If I read immediately, within 20ms, I get almost all of it. If I wait 10s, I loose 2/3. That's why the sleep 0 is very important; sleep 0 performs a kernel scheduling, which implies a lag of about 10ms; without sleep 0, we would usually get more data; aboce sleep 10, results don't change much. Sleep 2 is a very intermediate value where things hugely change with the mood of the scheduler (depending on load).

I can not accept that data read on buffer may age, or fade with time.

Loosing data in buffer is notmal when buffer is closed; as you see, I took care to open it, and maintain it CONTINUOUSLY open between calls. Beginners usually do this, and cry fast:

Code:
/bin/echo "1234567890abcdefghijklmnopqrstuvwxyz" >/dev/ttyAMA0 ; cat /dev/ttyAMA0


The serial settings are very important; but it takes days to test each and every option; so, maybe some one knows the answer in a short glance ?

Especially, twice the same command giving two different results ALWAYS MAKES ME ANGRY !!!
_________________
DEMAINE Benoît-Pierre (aka DoubleHP ) http://www.demaine.info/
>o_/ Coin coin coin \_o<
to contact me (MSN,ICQ, JABBER, Skype ... ) http://benoit.demaine.info/contact.png
Back to top
View user's profile Send private message
mmogilvi
n00b
n00b


Joined: 13 May 2011
Posts: 40

PostPosted: Mon Oct 24, 2016 6:22 am    Post subject: Reply with quote

I'm not sure how useful these will be, but here are some thoughts:

It sounds like a problem in either the /dev/ttyAMA0 driver, the underlying hardware, stty settings [particularly hardware flow control], the serial cable, and/or whatever device is connected to the other end of the cable. Somewhere in there characters are being dropped. It is highly unlikely to be a problem with echo or sleep, so the subject line is kind of misleading. I don't have /dev/ttyAMA0 and had to use google to see that it is apparently common on Raspberry Pi.

Although you don't say so, it sounds like you are either looping the transmit pin to the receive pin, or you are connected to some external device that is configured to echo back anything you send it.

stty is a command line tool wrapping various system calls documented in "man termios". It seems to have a few more details about some of the settings than "man stty".

It might help to enable hardware flow control (crtscts). This is an extra pair of lines often used to pause the transmitter if the receiving end is not keeping up (otherwise a slow receiver is likely to loose characters).

RS-232 (or equivalent) serial lines send one bit at a time, and the highest level protocol recognized at a hardware level is a single byte "packet" consisting of 6-8 data bits, and a few configurable start/stop/parity bits, all of which need to be agreed on for both ends. Even the parity bit (if present) is insufficient to give any real level of reliability with such a small packet, so any kind of retransmission and reliability logic has to be implemented outside the core RS-232 protocol, usually in software. If you aren't already intimately familiar with RS-232, it might be good to read https://en.wikipedia.org/wiki/RS-232 and related links carefully. I'm not familiar with the UART behind /dev/ttyAMA0 , but I played with setting up a SLIP-based serial network with 16450's and 16550's twenty years ago, and know that on Linux with my hardware at the time, the 16450 occasionally dropped some bytes when running at 38400 baud, but the 16550 could go faster. But 1200 from your stty line seems slow enough to not matter. (See https://en.wikipedia.org/wiki/8250_UART )

Is there something funny about the driver, such that it doesn't buffer incoming data properly? Maybe it is only emulating a UART, and has really sever speed restrictions when the CPU is doing other things? Track down and look at the relevant kernel source and/or online hardware specs, etc.

Now that I think about it, the most likely cause may be that the driver is confused by opening the device multiple times at once. Perhaps each open() gets its own input buffer, even if open write-only? Maybe opening it once read/write (shell using "<>" [and then dup()'ing file descriptors as necessary]) would work better?
Code:
{ echo "whatever" 1>&3 ; [other commands] ;cat 0<&3 } 3<> /dev/ttyAMA0
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum