@@ -2014,6 +2014,12 @@ asyncio.run(run())
2014
2014
2015
2015
## 6.3 Using the stream mechanism
2016
2016
2017
+ A stream is an abstraction of a device whose interface consists of a realtime
2018
+ source of bytes. Examples include UARTs, I2S devices and sockets. Many streams
2019
+ are continuous: an I2S microphone will source data until switched off and the
2020
+ interface is closed. Streams are supported by ` asyncio.StreamReader ` and
2021
+ ` asyncio.StreamWriter ` classes.
2022
+
2017
2023
This section applies to platforms other than the Unix build. The latter handles
2018
2024
stream I/O in a different way described
2019
2025
[ here] ( https://github.com/micropython/micropython/issues/7965#issuecomment-960259481 ) .
@@ -2054,6 +2060,7 @@ async def main():
2054
2060
2055
2061
asyncio.run(main())
2056
2062
```
2063
+ The ` .readline ` method will pause until ` \n ` is received. The ` .read `
2057
2064
Writing to a ` StreamWriter ` occurs in two stages. The synchronous ` .write `
2058
2065
method concatenates data for later transmission. The asynchronous ` .drain `
2059
2066
causes transmission. To avoid allocation call ` .drain ` after each call to
@@ -2078,6 +2085,12 @@ avoid buffer overflows and data loss. This can be ameliorated by using a larger
2078
2085
UART read buffer or a lower baudrate. Alternatively hardware flow control will
2079
2086
provide a solution if the data source supports it.
2080
2087
2088
+ The ` StreamReader ` read methods fall into two categories depending on whether
2089
+ they wait for a specific end condition. Thus ` .readline ` pauses until a newline
2090
+ byte has been received, ` .read(-1) ` waits for EOF, and ` readexactly ` waits for
2091
+ a precise number of bytes. Other methods return the number of bytes available
2092
+ at the time they are called (upto a maximum).
2093
+
2081
2094
### 6.3.1 A UART driver example
2082
2095
2083
2096
The program [ auart_hd.py] ( ../as_demos/auart_hd.py ) illustrates a method of
0 commit comments