Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mike-matera opened this issue Aug 6, 2015 · 1 comment
Closed

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

mike-matera opened this issue Aug 6, 2015 · 1 comment

Comments

@mike-matera
Copy link
Contributor

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
Copy link

This change gave me the expected behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants