| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: aioserial |
| 3 |
Version: 1.3.1 |
| 4 |
Summary: An asynchronous serial port library of Python |
| 5 |
Home-page: https://github.com/changyuheng/aioserial.py |
| 6 |
License: MPL-2.0 |
| 7 |
Author: Johann Chang |
| 8 |
Author-email: [email protected] |
| 9 |
Requires-Python: >=3.6,<4.0 |
| 10 |
Classifier: License :: OSI Approved |
| 11 |
Classifier: Programming Language :: Python :: 3 |
| 12 |
Classifier: Programming Language :: Python :: 3.10 |
| 13 |
Classifier: Programming Language :: Python :: 3.6 |
| 14 |
Classifier: Programming Language :: Python :: 3.7 |
| 15 |
Classifier: Programming Language :: Python :: 3.8 |
| 16 |
Classifier: Programming Language :: Python :: 3.9 |
| 17 |
Requires-Dist: pyserial |
| 18 |
Description-Content-Type: text/markdown |
| 19 |
|
| 20 |
# aioserial |
| 21 |
|
| 22 |
* [Quick start](#quick-start) |
| 23 |
+ [A simple serial port reader](#a-simple-serial-port-reader) |
| 24 |
+ [pyserial-asyncio example replacement](#pyserial-asyncio-example-replacement) |
| 25 |
* [API](#api) |
| 26 |
+ [AioSerial](#aioserial) |
| 27 |
- [Constructor](#constructor) |
| 28 |
- [Methods](#methods) |
| 29 |
* [read_async](#read-async) |
| 30 |
* [read_until_async](#read-until-async) |
| 31 |
* [readinto_async](#readinto-async) |
| 32 |
* [readline_async](#readline-async) |
| 33 |
* [readlines_async](#readlines-async) |
| 34 |
* [write_async](#write-async) |
| 35 |
* [writelines_async](#writelines-async) |
| 36 |
+ [Other APIs](#other-apis) |
| 37 |
* [Why aioserial?](#why-aioserial-) |
| 38 |
|
| 39 |
A Python package that combines [asyncio](https://docs.python.org/3/library/asyncio.html) and [pySerial](https://pypi.org/project/pyserial/). |
| 40 |
|
| 41 |
## Quick start |
| 42 |
|
| 43 |
### A simple serial port reader |
| 44 |
|
| 45 |
```py |
| 46 |
import asyncio |
| 47 |
|
| 48 |
import aioserial |
| 49 |
|
| 50 |
|
| 51 |
async def read_and_print(aioserial_instance: aioserial.AioSerial): |
| 52 |
while True: |
| 53 |
print((await aioserial_instance.read_async()).decode(errors='ignore'), end='', flush=True) |
| 54 |
|
| 55 |
asyncio.run(read_and_print(aioserial.AioSerial(port='COM1'))) |
| 56 |
``` |
| 57 |
|
| 58 |
### pyserial-asyncio example replacement |
| 59 |
|
| 60 |
**The example usage from pyserial-asyncio** |
| 61 |
|
| 62 |
https://pyserial-asyncio.readthedocs.io/en/latest/shortintro.html |
| 63 |
|
| 64 |
```py |
| 65 |
import asyncio |
| 66 |
import serial_asyncio |
| 67 |
|
| 68 |
class Output(asyncio.Protocol): |
| 69 |
def connection_made(self, transport): |
| 70 |
self.transport = transport |
| 71 |
print('port opened', transport) |
| 72 |
transport.serial.rts = False # You can manipulate Serial object via transport |
| 73 |
transport.write(b'Hello, World!\n') # Write serial data via transport |
| 74 |
|
| 75 |
def data_received(self, data): |
| 76 |
print('data received', repr(data)) |
| 77 |
if b'\n' in data: |
| 78 |
self.transport.close() |
| 79 |
|
| 80 |
def connection_lost(self, exc): |
| 81 |
print('port closed') |
| 82 |
self.transport.loop.stop() |
| 83 |
|
| 84 |
def pause_writing(self): |
| 85 |
print('pause writing') |
| 86 |
print(self.transport.get_write_buffer_size()) |
| 87 |
|
| 88 |
def resume_writing(self): |
| 89 |
print(self.transport.get_write_buffer_size()) |
| 90 |
print('resume writing') |
| 91 |
|
| 92 |
loop = asyncio.get_event_loop() |
| 93 |
coro = serial_asyncio.create_serial_connection(loop, Output, '/dev/ttyUSB0', baudrate=115200) |
| 94 |
loop.run_until_complete(coro) |
| 95 |
loop.run_forever() |
| 96 |
loop.close() |
| 97 |
``` |
| 98 |
|
| 99 |
**aioserial equivalence** |
| 100 |
|
| 101 |
```py |
| 102 |
import asyncio |
| 103 |
|
| 104 |
import aioserial |
| 105 |
|
| 106 |
|
| 107 |
async def read_and_print(aioserial_instance: aioserial.AioSerial): |
| 108 |
while True: |
| 109 |
data: bytes = await aioserial_instance.read_async() |
| 110 |
print(data.decode(errors='ignore'), end='', flush=True) |
| 111 |
if b'\n' in data: |
| 112 |
aioserial_instance.close() |
| 113 |
break |
| 114 |
|
| 115 |
aioserial_instance: aioserial.AioSerial = aioserial.AioSerial(port='/dev/ttyUSB0', baudrate=115200) |
| 116 |
asyncio.run(asyncio.gather(read_and_print(aioserial_instance), aioserial_instance.write_async(b'Hello, World!\n'))) |
| 117 |
``` |
| 118 |
|
| 119 |
## API |
| 120 |
|
| 121 |
### AioSerial |
| 122 |
|
| 123 |
```py |
| 124 |
>>> import aioserial |
| 125 |
>>> import serial |
| 126 |
|
| 127 |
>>> isinstance(aioserial.AioSerial(), serial.Serial) |
| 128 |
True |
| 129 |
|
| 130 |
>>> issubclass(aioserial.AioSerial, serial.Serial) |
| 131 |
True |
| 132 |
|
| 133 |
>>> aioserial.Serial is serial.Serial |
| 134 |
True |
| 135 |
``` |
| 136 |
|
| 137 |
#### Constructor |
| 138 |
|
| 139 |
```py |
| 140 |
aioserial_instance: aioserial.AioSerial = aioserial.AioSerial( |
| 141 |
# ... same with what can be passed to serial.Serial ..., |
| 142 |
loop: Optional[asyncio.AbstractEventLoop] = None, |
| 143 |
cancel_read_timeout: int = 1, |
| 144 |
cancel_write_timeout: int = 1) |
| 145 |
``` |
| 146 |
|
| 147 |
#### Methods |
| 148 |
|
| 149 |
|
| 150 |
##### read_async |
| 151 |
|
| 152 |
```py |
| 153 |
bytes_read: bytes = \ |
| 154 |
await aioserial_instance.read_async(size: int = 1) |
| 155 |
``` |
| 156 |
|
| 157 |
##### read_until_async |
| 158 |
|
| 159 |
```py |
| 160 |
at_most_certain_size_of_bytes_read: bytes = \ |
| 161 |
await aioserial_instance.read_until_async( |
| 162 |
expected: bytes = aioserial.LF, size: Optional[int] = None) |
| 163 |
``` |
| 164 |
|
| 165 |
##### readinto_async |
| 166 |
|
| 167 |
```py |
| 168 |
number_of_byte_read: int = \ |
| 169 |
await aioserial_instance.readinto_async(b: Union[array.array, bytearray]) |
| 170 |
``` |
| 171 |
|
| 172 |
##### readline_async |
| 173 |
|
| 174 |
```py |
| 175 |
a_line_of_at_most_certain_size_of_bytes_read: bytes = \ |
| 176 |
await aioserial_instance.readline_async(size: int = -1) |
| 177 |
``` |
| 178 |
|
| 179 |
##### readlines_async |
| 180 |
|
| 181 |
```py |
| 182 |
lines_of_at_most_certain_size_of_bytes_read: bytes = \ |
| 183 |
await aioserial_instance.readlines_async(hint: int = -1) |
| 184 |
``` |
| 185 |
|
| 186 |
##### write_async |
| 187 |
|
| 188 |
```py |
| 189 |
number_of_byte_like_data_written: int = \ |
| 190 |
await aioserial_instance.write_async(bytes_like_data) |
| 191 |
``` |
| 192 |
|
| 193 |
##### writelines_async |
| 194 |
|
| 195 |
```py |
| 196 |
number_of_byte_like_data_in_the_given_list_written: int = \ |
| 197 |
await aioserial_instance.writelines_async(list_of_bytes_like_data) |
| 198 |
``` |
| 199 |
|
| 200 |
### Other APIs |
| 201 |
|
| 202 |
All the other APIs in the mother package [pySerial](https://pypi.org/project/pyserial/) are supported in aioserial as-is. |
| 203 |
|
| 204 |
## Why aioserial? |
| 205 |
|
| 206 |
* Want to use an asyncio-based but not a (self-built) thread-based serial library. |
| 207 |
* [pySerial-asyncio](https://pypi.org/project/pyserial-asyncio/) does [not support Windows](https://github.com/pyserial/pyserial-asyncio/issues/3). |
| 208 |
* APIs in all the other packages ([pySerial-asyncio](https://pypi.org/project/pyserial-asyncio/), |
| 209 |
[asyncserial](https://pypi.org/project/asyncserial/)) that target the same goal are not designed in high level. |
| 210 |
|