Replies: 4 comments 6 replies
-
I would set the UART timeout to 0 (wait forever). If you need a timeout it should be applied to the task doing the reading rather than to the UART device driver. |
Beta Was this translation helpful? Give feedback.
-
Apologies, you are correct about async def read_serial_data(sreader):
while True:
try:
chars = await sreader.read(100) # Return as much data as is available
for c in chars:
print(c.hex(), end='')
# new line on end of message (\xDD)
if c == FRAME_TAIL: # We just printed the tail
print() # Newline
except KeyboardInterrupt:
print("Program terminated.")
break |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply. Changing the number of bytes to read works. For some reason asyncio / streamreader makes it really slow. I don't really understand why. https://github.com/micropython/micropython/blob/master/extmod/asyncio/stream.py # MicroPython asyncio module
# stream.py
class Stream:
def __init__(self, s, e={}):
self.s = s
self.e = e
self.out_buf = b""
...
# async
def read(self, n=-1):
r = b""
while True:
yield core._io_queue.queue_read(self.s)
r2 = self.s.read(n)
if r2 is not None:
if n >= 0:
return r2
if not len(r2):
return r
r += r2
...
StreamReader = Stream What is the line I don't really see the point in using I don't know what makes There is another thing that i don't understand. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/micropython/micropython/blob/master/extmod/asyncio/core.py#L75 def _enqueue(self, s, idx):
if id(s) not in self.map:
entry = [None, None, s]
entry[idx] = cur_task
self.map[id(s)] = entry
self.poller.register(s, select.POLLIN if idx == 0 else select.POLLOUT)
else:
sm = self.map[id(s)]
assert sm[idx] is None
assert sm[1 - idx] is not None
sm[idx] = cur_task
self.poller.modify(s, select.POLLIN | select.POLLOUT)
# Link task to this IOQueue so it can be removed if needed
cur_task.data = self
...
def queue_read(self, s):
self._enqueue(s, 0) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm writing a driver for a RFID reader that communicates over UART. Around every 7th message / frame is broken because of missing bytes. I already checked the pin at ESP32 with a logic analyzer and the data that reaches the pin seems flawless.
How could this happen and how can i solve this?
As i understand it this should not happen since the ESP32 has hardware buffer which is far from being filled up.
Here is the minimal code that i'm using to read and print the messages:
The read data looks like follows. With the logic analyzer the frames are the same lengths. You can see that there is sometimes parts of the message missing if bytes in the middle got lost. Sometimes the message is longer if the end or the beginning of the new message got lost.
MicroPython v1.22.0-preview.73.g7736ab832.dirty on 2023-11-24; Generic ESP32 module with ESP32
Also failed with older micropython version 1.20.
Beta Was this translation helpful? Give feedback.
All reactions