maniacbug / StandardCplusplus

Standard C++ for Arduino (port of uClibc++)
588 stars 182 forks source link

std::cin doesn't wait for characters. #16

Closed mike-matera closed 2 years ago

mike-matera commented 9 years ago

The implementation of basic_serialbuf treats having no characters available as an EOF condition. It expects that read() and peek() are blocking as they would be in an implementation on a OS. In Arduino they are non-blocking and return -1 when there's no characters available. The following patch fixes the behavior:


---
 serstream | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/serstream b/serstream
index 268053b..24d12e4 100644
--- a/serstream
+++ b/serstream
@@ -130,10 +130,11 @@ namespace std
         */

                virtual int_type underflow(){
-                       if(_serial.available())
-                               return _serial.peek();
-                       else 
-                               return traits::eof();
+                       // There is no EOF condition on a serial stream.
+                       // underflow() and uflow() should block, reproducing the 
+                       // OS behavior when there are no charaters to read.
+                       while (! _serial.available()) { /* wait */ }
+                       return _serial.peek();
                }

        /*
@@ -141,10 +142,9 @@ namespace std
         */

                virtual int_type uflow(){
-                       if(_serial.available())
-                               return _serial.read();
-                       else 
-                               return traits::eof();
+                       // See underflow() above
+                       while (! _serial.available()) { /* wait */ }
+                       return _serial.read();
                }

        /*
JacobJBeck commented 8 years ago

This change gave me the expected behavior.