| 1 |
# Copyright 2020 Henry Chang |
| 2 |
# |
| 3 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
import array |
| 8 |
import asyncio |
| 9 |
import concurrent.futures |
| 10 |
import sys |
| 11 |
from typing import List, Optional, Union |
| 12 |
|
| 13 |
import serial |
| 14 |
|
| 15 |
|
| 16 |
class AioSerial(serial.Serial): |
| 17 |
|
| 18 |
def __init__( |
| 19 |
self, |
| 20 |
port: Optional[str] = None, |
| 21 |
baudrate: int = 9600, |
| 22 |
bytesize: int = serial.EIGHTBITS, |
| 23 |
parity: str = serial.PARITY_NONE, |
| 24 |
stopbits: Union[float, int] = serial.STOPBITS_ONE, |
| 25 |
timeout: Optional[Union[float, int]] = None, |
| 26 |
xonxoff: bool = False, |
| 27 |
rtscts: bool = False, |
| 28 |
write_timeout: Optional[Union[float, int]] = None, |
| 29 |
dsrdtr: bool = False, |
| 30 |
inter_byte_timeout: Optional[Union[float, int]] = None, |
| 31 |
exclusive: Optional[bool] = None, |
| 32 |
loop: Optional[asyncio.AbstractEventLoop] = None, |
| 33 |
cancel_read_timeout: int = 1, |
| 34 |
cancel_write_timeout: int = 1, |
| 35 |
**kwargs): |
| 36 |
super().__init__( |
| 37 |
port=port, |
| 38 |
baudrate=baudrate, |
| 39 |
bytesize=bytesize, |
| 40 |
parity=parity, |
| 41 |
stopbits=stopbits, |
| 42 |
timeout=timeout, |
| 43 |
xonxoff=xonxoff, |
| 44 |
rtscts=rtscts, |
| 45 |
write_timeout=write_timeout, |
| 46 |
dsrdtr=dsrdtr, |
| 47 |
inter_byte_timeout=inter_byte_timeout, |
| 48 |
exclusive=exclusive, |
| 49 |
**kwargs) |
| 50 |
self._loop: Optional[asyncio.AbstractEventLoop] = loop |
| 51 |
|
| 52 |
self._cancel_read_executor: concurrent.futures.ThreadPoolExecutor = \ |
| 53 |
concurrent.futures.ThreadPoolExecutor(max_workers=1) |
| 54 |
self._cancel_read_timeout: int = cancel_read_timeout |
| 55 |
self._read_executor: concurrent.futures.ThreadPoolExecutor = \ |
| 56 |
concurrent.futures.ThreadPoolExecutor(max_workers=1) |
| 57 |
self._read_lock: asyncio.Lock = asyncio.Lock() |
| 58 |
|
| 59 |
self._cancel_write_executor: concurrent.futures.ThreadPoolExecutor = \ |
| 60 |
concurrent.futures.ThreadPoolExecutor(max_workers=1) |
| 61 |
self._cancel_write_timeout: int = cancel_write_timeout |
| 62 |
self._write_executor: concurrent.futures.ThreadPoolExecutor = \ |
| 63 |
concurrent.futures.ThreadPoolExecutor(max_workers=1) |
| 64 |
self._write_lock: asyncio.Lock = asyncio.Lock() |
| 65 |
|
| 66 |
@property |
| 67 |
def loop(self) -> Optional[asyncio.AbstractEventLoop]: |
| 68 |
return self._loop if self._loop else asyncio.get_running_loop() \ |
| 69 |
if sys.version_info >= (3, 7) else asyncio.get_event_loop() |
| 70 |
|
| 71 |
@loop.setter |
| 72 |
def loop(self, value: Optional[asyncio.AbstractEventLoop]): |
| 73 |
self._loop = value |
| 74 |
|
| 75 |
async def _cancel_read_async(self): |
| 76 |
if not hasattr(self, 'cancel_read'): |
| 77 |
return |
| 78 |
await asyncio.wait_for( |
| 79 |
self.loop.run_in_executor( |
| 80 |
self._cancel_read_executor, self.cancel_read), |
| 81 |
self._cancel_read_timeout) |
| 82 |
|
| 83 |
async def _cancel_write_async(self): |
| 84 |
if not hasattr(self, 'cancel_write'): |
| 85 |
return |
| 86 |
await asyncio.wait_for( |
| 87 |
self.loop.run_in_executor( |
| 88 |
self._cancel_write_executor, self.cancel_write), |
| 89 |
self._cancel_write_timeout) |
| 90 |
|
| 91 |
async def read_async(self, size: int = 1) -> bytes: |
| 92 |
async with self._read_lock: |
| 93 |
try: |
| 94 |
return await self.loop.run_in_executor( |
| 95 |
self._read_executor, self.read, size) |
| 96 |
except asyncio.CancelledError: |
| 97 |
await asyncio.shield(self._cancel_read_async()) |
| 98 |
raise |
| 99 |
|
| 100 |
async def read_until_async( |
| 101 |
self, |
| 102 |
expected: bytes = serial.LF, |
| 103 |
size: Optional[int] = None) -> bytes: |
| 104 |
async with self._read_lock: |
| 105 |
try: |
| 106 |
return await self.loop.run_in_executor( |
| 107 |
self._read_executor, self.read_until, expected, size) |
| 108 |
except asyncio.CancelledError: |
| 109 |
await asyncio.shield(self._cancel_read_async()) |
| 110 |
raise |
| 111 |
|
| 112 |
async def readinto_async(self, b: Union[array.array, bytearray]): |
| 113 |
async with self._read_lock: |
| 114 |
try: |
| 115 |
return await self.loop.run_in_executor( |
| 116 |
self._read_executor, self.readinto, b) |
| 117 |
except asyncio.CancelledError: |
| 118 |
await asyncio.shield(self._cancel_read_async()) |
| 119 |
raise |
| 120 |
|
| 121 |
async def readline_async(self, size: int = -1) -> bytes: |
| 122 |
async with self._read_lock: |
| 123 |
try: |
| 124 |
return await self.loop.run_in_executor( |
| 125 |
self._read_executor, self.readline, size) |
| 126 |
except asyncio.CancelledError: |
| 127 |
await asyncio.shield(self._cancel_read_async()) |
| 128 |
raise |
| 129 |
|
| 130 |
async def readlines_async(self, hint: int = -1) -> List[bytes]: |
| 131 |
async with self._read_lock: |
| 132 |
try: |
| 133 |
return await self.loop.run_in_executor( |
| 134 |
self._read_executor, self.readlines, hint) |
| 135 |
except asyncio.CancelledError: |
| 136 |
await asyncio.shield(self._cancel_read_async()) |
| 137 |
raise |
| 138 |
|
| 139 |
async def write_async( |
| 140 |
self, data: Union[bytearray, bytes, memoryview]) -> int: |
| 141 |
async with self._write_lock: |
| 142 |
try: |
| 143 |
return await self.loop.run_in_executor( |
| 144 |
self._write_executor, self.write, data) |
| 145 |
except asyncio.CancelledError: |
| 146 |
await asyncio.shield(self._cancel_write_async()) |
| 147 |
raise |
| 148 |
|
| 149 |
async def writelines_async( |
| 150 |
self, lines: List[Union[bytearray, bytes, memoryview]]): |
| 151 |
async with self._write_lock: |
| 152 |
try: |
| 153 |
return await self.loop.run_in_executor( |
| 154 |
self._write_executor, self.writelines, lines) |
| 155 |
except asyncio.CancelledError: |
| 156 |
await asyncio.shield(self._cancel_write_async()) |
| 157 |
raise |