-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathauart.py
45 lines (33 loc) · 971 Bytes
/
auart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Test of uasyncio stream I/O using UART
# Author: Peter Hinch
# Copyright Peter Hinch 2017-2022 Released under the MIT license
# Link X1 and X2 to test.
# We run with no UART timeout: UART read never blocks.
import asyncio
from machine import UART
uart = UART(4, 9600, timeout=0)
async def sender():
swriter = asyncio.StreamWriter(uart, {})
while True:
swriter.write("Hello uart\n")
await swriter.drain()
await asyncio.sleep(2)
async def receiver():
sreader = asyncio.StreamReader(uart)
while True:
res = await sreader.readline()
print("Received", res)
async def main():
asyncio.create_task(sender())
asyncio.create_task(receiver())
while True:
await asyncio.sleep(1)
def test():
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Interrupted")
finally:
asyncio.new_event_loop()
print("as_demos.auart.test() to run again.")
test()