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