pythoncoder wrote: ↑Tue Feb 20, 2018 6:07 amI don't know where you found that. The official docs have no reference to this as far as I can see.
There are various ways to use the UART. You can poll any() to see if data is available. If you don't mind methods which block until data is received you can use the character or line reading methods. The most elegant way is to use uasyncio: UARTS are stream devices and can be run as asynchronous tasks. The following code fragment performs concurrent I/O on a single UART and can be tested with a loopback between pins X1 and X2:The point here is that you can have other tasks running concurrently while the receiver task pauses pending input.CODE:
import uasyncio as asynciofrom pyb import UARTuart = UART(4, 9600)async def sender(): swriter = asyncio.StreamWriter(uart, {}) while True: await swriter.awrite('Hello uart\n') await asyncio.sleep(2)async def receiver(): sreader = asyncio.StreamReader(uart) while True: res = await sreader.readline() print('Recieved', res)loop = asyncio.get_event_loop()loop.create_task(sender())loop.create_task(receiver())loop.run_forever()
for some reason this code is not working for me. nothing happens.
esp32 s2, v1.22.1
Statistics: Posted by Alexel — Wed Jan 10, 2024 9:36 pm