unexpected try-except drop through #19077
Replies: 3 comments 5 replies
-
|
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.
-
|
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.
-
|
Tnx for the ideas all. Went down a bit of a rabbit hole looking for the cause of the occasional unexpected timeout. To me it looks like a dodgy integer is somehow inserted in the flow of integers from ticks_ms(). The 'post' event values for now, was & d look exactly like what I would expect to see if the event never occurred. |
Beta Was this translation helpful? Give feedback.

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