How can I flush stdin buffer in ESP8266?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Posts: 33
Joined: Thu Jun 11, 2020 1:03 am

How can I flush stdin buffer in ESP8266?

Post by enzo » Thu Dec 03, 2020 12:03 am

please anyone can help with this?
I'm writing a non blocking timed input like this sample code:

Code: Select all

import sys, select, utime
def timed_input():
    i,_, _ = [sys.stdin], [sys.stdout], [sys.stderr], 5 )
    if (i):
        answer = sys.stdin.readline()
        answer = "*"
    for x in answer: print(x, ord(x))
    return answer.replace('\n', '')

print("Start from:")
start= timed_input()
print("End to:")
end = timed_input()  
print(start, len(start), end, len(end))
The first input (start) gets out ok but the second passes through the timed_input without allowing to insert anything from the keyboard.
I printed the details of the output from the second request because I suspected there was something left in the stdin buffer which caused the exit from the sys.stdin.readline() and indeed there's a LF (chr(10)) left which i guess is what causes the exit from the input.
I found there's sys.stdin.flush() to flush the buffer but it doesn't work in microPython (AttributeError: 'FileIO' object has no attribute 'flush').
This is confirmed by the fact that if I don't input anything, letting the timeout exit, the second call doesn't exit but waits for an input.
How can I flush the buffer or use a workaround to get a working sequence of timed inputs?
Thanks for any help.

Post Reply