Quantcast
Viewing all articles
Browse latest Browse all 40

MicroPython pyboard • Re: UART IRQ/Callbacks and Class Example

pythoncoder wrote:
Tue Feb 20, 2018 6:07 am
donikuy wrote:
Mon Feb 19, 2018 1:58 pm
...Documentation indicates that irq is available...
I 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:

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()
The point here is that you can have other tasks running concurrently while the receiver task pauses pending input.

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



Viewing all articles
Browse latest Browse all 40

Trending Articles