unexpected try-except drop through #19077
Unanswered
krismc
asked this question in
Using MicroPython
Replies: 2 comments 1 reply
-
|
Could it be the wraparound on https://docs.micropython.org/en/latest/library/time.html#time.ticks_diff |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
Why not use the UART's built-in timeout? from machine import UART
from time import ticks_ms, ticks_diff
def anything():
start = ticks_ms()
res = uart.read()
if res is None: # Timed out waiting for 1st or subsequent chars
print(f"Timeout at {ticks_diff(ticks_ms(), start)}ms")
else:
return res.decode()
uart = UART(0, timeout=500, timeout_char=500)
print(anything()) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Reading an rpi pico uart with
start=ticks_ms()while ticks_ms()-start<100:if uart.any(): return uart.read().decode()print('timeout', ticks_ms()-start); return uart.read().decode()it works OK, but if I put the last line inside a try except
start='ticks_ms()while ticks_ms()-start<100:if uart.any(): return uart.read().decode()try:print('timeout', ticks_ms()-start); return uart.read().decode()except Exception as e: sys.print_exception(e)it occasionally drops through into the timeout print without the while timeout loop having expired, typically after 0 to 2ms. I'm struggling to explain this. Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions