IF statement problems?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
microBoa
Posts: 13
Joined: Mon Apr 05, 2021 6:09 am

IF statement problems?

Post by microBoa » Wed Apr 07, 2021 2:30 am

I'm stuck in a maze of twisty little passages....

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)
it STILL prints 'j'.

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)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: IF statement problems?

Post by pythoncoder » Wed Apr 07, 2021 5:49 am

I would expect your code to throw an exception as you issue

Code: Select all

buf = None
then

Code: Select all

buf = buf + ch
The following is a somewhat hand-waving response rather than picking up a specific error in your code.

I would avoid converting to Unicode in this type of low-level routine and perform all the comparisons on bytes. UARTs receive data in bytes and in my experience converting to Unicode is best done at the last possible moment. You might want to accumulate bytes in a bytearray and perform comparisons with e.g. b'\n' or b'j'.
Peter Hinch
Index to my micropython libraries.

microBoa
Posts: 13
Joined: Mon Apr 05, 2021 6:09 am

Re: IF statement problems?

Post by microBoa » Wed Apr 07, 2021 6:26 am

Thanks Peter,

Just moments ago I figured out the

Code: Select all

buf = None
problem, I was about to reply to myself and saw you beat me to it.

Perhaps since the REPL was disabled I didn't see any exception. The whole REPL redirection thing has been my biggest frustration since trying Micropython several days ago, really difficult to troubleshoot.

I'll switch back to comparing bytes, it was more of a troubleshooting tactic.

Thanks for taking a look and helping out!

Post Reply