Page 1 of 1

uasyncio - StreamWriter in two async loops.

Posted: Thu Nov 12, 2020 7:28 am
by prem111
Hi. How to use uasyncio.StreamWriter in two asynchronous loops in the same time? Sometimes when he wants to write to uart at the same time, not everything comes up. Thanks for help!

Re: uasyncio - StreamWriter in two async loops.

Posted: Thu Nov 12, 2020 8:27 am
by pythoncoder
This sounds like very bad practice. If two tasks write to the UART at once, the result will be a garbled mixture of the two messages.

The correct way to handle UART communications is to associate a single StreamWriter and StreamReader with the UART, with a task handling writes and another handling reads. If multiple tasks want to write, they should add their messages to a queue.

The write task has a while True loop which checks for a message on the queue and, if present, writes it to the StreamWriter. That way message integrity is maintained.

Re: uasyncio - StreamWriter in two async loops.

Posted: Sat Nov 14, 2020 9:51 am
by prem111
Everything ok, I did it in queue. Thanks.