I've been trying to use uasyncio for some "parallel" tasks (uart communication and sensor polling).
My problem is that "data = await sreader.readline()" behaves very buggy in my program:
When I send a bytestring like b'test12345\n' sometimes I receive nothing after the first try.
Then after a second attempt I receive something like b'test12345test12345\n'.
I think the problem lies in the delays (time.sleep_us(10)) of the sensor library (HC-SR094) which locks the readline() command.
Does anybody know a good practice to deal with modules that uses such delays?
Here is a minimal example:
Code: Select all
from machine import Pin, UART
import uasyncio as asyncio
uart = UART(1, baudrate=115200, tx=Pin(4), rx=Pin(5))
async def receiver():
sreader = asyncio.StreamReader(uart)
while True:
data = await sreader.readline()
print(data)
async def read_sensor():
# read data from sensor
await asyncio.sleep_ms(100)
async def main():
asyncio.create_task(receiver())
asyncio.create_task(read_sensor())
while True:
await asyncio.sleep_ms(1000)
if __name__ == '__main__':
try:
asyncio.run(main())
finally:
asyncio.new_event_loop()
Regards Marc