I'm really not sure what's going on here. I have a polling loop to capture characters from the uart. If I don't have any IF statements and just print the characters in a While loop, then they will print with uart.write(). If I try to check for one or more characters while I construct a buffer, I get ONE character to write and it hangs. If I try
Code: Select all
if ch != 'j': uart.write(ch)
I've converted the type from uart.read() to a string also. I've written enough Python and used conditions like this before, I'm stumped.
At the moment it will echo ONE character, and if I press Enter as that character it isn't dropping out of the While loop.
What am I missing? Thanks for taking a look!
Code: Select all
import machine
import network
import uselect
import uos
from machine import UART
# ESP-8266 ... stuck in a maze of twisty little passages
def get_cmd():
buf = None
uos.dupterm(None, 1)
uart = UART(0, 115200, timeout=10, timeout_char=10)
poll = uselect.poll()
poll.register(uart, uselect.POLLIN)
uart.write("\033[2J\033[1;1f") #Clear screen
while True:
ch = str(uart.read(1), 'UTF-8') if poll.poll() else None
if ch == None:
continue
if ch != 'j':
uart.write(ch)
if ch != '\r' or ch != '\n':
buf = buf + ch
# if ch != '\010': # Anything except Backspace (tried \b)
# uart.write(ch)
# buf = buf + ch
# if ch == '\010' and len(buf) >= 1: # Prevent backspacing too far
# uart.write('\b\033[0K') # Backspace and overwrite char on screen
# buf = buf[:-1]
else: # \r or \n
poll.unregister(uart)
uos.dupterm(UART(0, 115200), 1)
break
return buf
buf = get_cmd()
uos.dupterm(UART(0, 115200), 1)
print("BUF> ", buf)