hat.drivers.serial
Asyncio serial communication driver
1"""Asyncio serial communication driver""" 2 3from hat.drivers.serial.common import (ByteSize, 4 Parity, 5 StopBits, 6 EndpointInfo, 7 Endpoint, 8 endpoint_info_to_json) 9 10from hat.drivers.serial import py_serial 11 12try: 13 from hat.drivers.serial import native_serial 14 15except ImportError: 16 native_serial = None 17 18 19__all__ = ['ByteSize', 20 'Parity', 21 'StopBits', 22 'EndpointInfo', 23 'Endpoint', 24 'endpoint_info_to_json', 25 'create', 26 'py_serial', 27 'native_serial'] 28 29 30async def create(port: str, 31 *, 32 name: str | None = None, 33 baudrate: int = 9600, 34 bytesize: ByteSize = ByteSize.EIGHTBITS, 35 parity: Parity = Parity.NONE, 36 stopbits: StopBits = StopBits.ONE, 37 xonxoff: bool = False, 38 rtscts: bool = False, 39 dsrdtr: bool = False, 40 silent_interval: float = 0 41 ) -> Endpoint: 42 """Open serial port 43 44 Args: 45 port: port name dependent of operating system 46 (e.g. `/dev/ttyUSB0`, `COM3`, ...) 47 name: endpoint name 48 baudrate: baud rate 49 bytesize: number of data bits 50 parity: parity checking 51 stopbits: number of stop bits 52 xonxoff: enable software flow control 53 rtscts: enable hardware RTS/CTS flow control 54 dsrdtr: enable hardware DSR/DTR flow control 55 silent_interval: minimum time in seconds between writing two 56 consecutive messages 57 58 """ 59 impl = native_serial or py_serial 60 return await impl.create(port=port, 61 name=name, 62 baudrate=baudrate, 63 bytesize=bytesize, 64 parity=parity, 65 stopbits=stopbits, 66 xonxoff=xonxoff, 67 rtscts=rtscts, 68 dsrdtr=dsrdtr, 69 silent_interval=silent_interval)
class
ByteSize(enum.Enum):
FIVEBITS =
<ByteSize.FIVEBITS: 5>
SIXBITS =
<ByteSize.SIXBITS: 6>
SEVENBITS =
<ByteSize.SEVENBITS: 7>
EIGHTBITS =
<ByteSize.EIGHTBITS: 8>
class
Parity(enum.Enum):
NONE =
<Parity.NONE: 'N'>
EVEN =
<Parity.EVEN: 'E'>
ODD =
<Parity.ODD: 'O'>
MARK =
<Parity.MARK: 'M'>
SPACE =
<Parity.SPACE: 'S'>
class
StopBits(enum.Enum):
ONE =
<StopBits.ONE: 1>
ONE_POINT_FIVE =
<StopBits.ONE_POINT_FIVE: 1.5>
TWO =
<StopBits.TWO: 2>
class
EndpointInfo(typing.NamedTuple):
EndpointInfo(name, port)
class
Endpoint(hat.aio.group.Resource):
42class Endpoint(aio.Resource): 43 """Serial endpoint""" 44 45 @property 46 @abc.abstractmethod 47 def info(self) -> EndpointInfo: 48 """Endpoint info""" 49 50 @abc.abstractmethod 51 async def read(self, size: int) -> util.Bytes: 52 """Read 53 54 Args: 55 size: number of bytes to read 56 57 Raises: 58 ConnectionError 59 60 """ 61 62 @abc.abstractmethod 63 async def write(self, data: util.Bytes): 64 """Write 65 66 Raises: 67 ConnectionError 68 69 """ 70 71 @abc.abstractmethod 72 async def drain(self): 73 """Drain output buffer 74 75 Raises: 76 ConnectionError 77 78 """ 79 80 @abc.abstractmethod 81 async def clear_input_buffer(self) -> int: 82 """Reset input buffer 83 84 Returns number of bytes available in buffer immediately before 85 buffer was cleared. 86 87 Raises: 88 ConnectionError 89 90 """
Serial endpoint
@abc.abstractmethod
async def
read(self, size: int) -> bytes | bytearray | memoryview:
50 @abc.abstractmethod 51 async def read(self, size: int) -> util.Bytes: 52 """Read 53 54 Args: 55 size: number of bytes to read 56 57 Raises: 58 ConnectionError 59 60 """
Read
Arguments:
- size: number of bytes to read
Raises:
- ConnectionError
@abc.abstractmethod
async def
write(self, data: bytes | bytearray | memoryview):
62 @abc.abstractmethod 63 async def write(self, data: util.Bytes): 64 """Write 65 66 Raises: 67 ConnectionError 68 69 """
Write
Raises:
- ConnectionError
@abc.abstractmethod
async def
drain(self):
71 @abc.abstractmethod 72 async def drain(self): 73 """Drain output buffer 74 75 Raises: 76 ConnectionError 77 78 """
Drain output buffer
Raises:
- ConnectionError
@abc.abstractmethod
async def
clear_input_buffer(self) -> int:
80 @abc.abstractmethod 81 async def clear_input_buffer(self) -> int: 82 """Reset input buffer 83 84 Returns number of bytes available in buffer immediately before 85 buffer was cleared. 86 87 Raises: 88 ConnectionError 89 90 """
Reset input buffer
Returns number of bytes available in buffer immediately before buffer was cleared.
Raises:
- ConnectionError
def
endpoint_info_to_json( info: EndpointInfo) -> None | bool | int | float | str | List[ForwardRef('Data')] | Dict[str, ForwardRef('Data')]:
async def
create( port: str, *, name: str | None = None, baudrate: int = 9600, bytesize: ByteSize = <ByteSize.EIGHTBITS: 8>, parity: Parity = <Parity.NONE: 'N'>, stopbits: StopBits = <StopBits.ONE: 1>, xonxoff: bool = False, rtscts: bool = False, dsrdtr: bool = False, silent_interval: float = 0) -> Endpoint:
31async def create(port: str, 32 *, 33 name: str | None = None, 34 baudrate: int = 9600, 35 bytesize: ByteSize = ByteSize.EIGHTBITS, 36 parity: Parity = Parity.NONE, 37 stopbits: StopBits = StopBits.ONE, 38 xonxoff: bool = False, 39 rtscts: bool = False, 40 dsrdtr: bool = False, 41 silent_interval: float = 0 42 ) -> Endpoint: 43 """Open serial port 44 45 Args: 46 port: port name dependent of operating system 47 (e.g. `/dev/ttyUSB0`, `COM3`, ...) 48 name: endpoint name 49 baudrate: baud rate 50 bytesize: number of data bits 51 parity: parity checking 52 stopbits: number of stop bits 53 xonxoff: enable software flow control 54 rtscts: enable hardware RTS/CTS flow control 55 dsrdtr: enable hardware DSR/DTR flow control 56 silent_interval: minimum time in seconds between writing two 57 consecutive messages 58 59 """ 60 impl = native_serial or py_serial 61 return await impl.create(port=port, 62 name=name, 63 baudrate=baudrate, 64 bytesize=bytesize, 65 parity=parity, 66 stopbits=stopbits, 67 xonxoff=xonxoff, 68 rtscts=rtscts, 69 dsrdtr=dsrdtr, 70 silent_interval=silent_interval)
Open serial port
Arguments:
- port: port name dependent of operating system
(e.g.
/dev/ttyUSB0,COM3, ...) - name: endpoint name
- baudrate: baud rate
- bytesize: number of data bits
- parity: parity checking
- stopbits: number of stop bits
- xonxoff: enable software flow control
- rtscts: enable hardware RTS/CTS flow control
- dsrdtr: enable hardware DSR/DTR flow control
- silent_interval: minimum time in seconds between writing two consecutive messages