Serial

Python implementation

Asyncio wrapper for serial communication

Warning

implementation is based on read with timeout for periodical checking if connection is closed by user - better way of canceling active read operation is needed

hat.drivers.serial.mlog

module logger

Type

logging.Logger

class hat.drivers.serial.ByteSize(value)

Bases: enum.Enum

An enumeration.

  • FIVEBITS

  • SIXBITS

  • SEVENBITS

  • EIGHTBITS

FIVEBITS = 'FIVEBITS'
SIXBITS = 'SIXBITS'
SEVENBITS = 'SEVENBITS'
EIGHTBITS = 'EIGHTBITS'
class hat.drivers.serial.Parity(value)

Bases: enum.Enum

An enumeration.

  • NONE

  • EVEN

  • ODD

  • MARK

  • SPACE

NONE = 'PARITY_NONE'
EVEN = 'PARITY_EVEN'
ODD = 'PARITY_ODD'
MARK = 'PARITI_MARK'
SPACE = 'PARITY_SPACE'
class hat.drivers.serial.StopBits(value)

Bases: enum.Enum

An enumeration.

  • ONE

  • ONE_POINT_FIVE

  • TWO

ONE = 'STOPBITS_ONE'
ONE_POINT_FIVE = 'STOPBITS_ONE_POINT_FIVE'
TWO = 'STOPBITS_TWO'
async hat.drivers.serial.open(port, *, baudrate=9600, bytesize=<ByteSize.EIGHTBITS: 'EIGHTBITS'>, parity=<Parity.NONE: 'PARITY_NONE'>, stopbits=<StopBits.ONE: 'STOPBITS_ONE'>, rtscts=False, dsrdtr=False, silent_interval=0)

Open serial port

Parameters
  • port (str) – port name dependent of operating system (e.g. /dev/ttyUSB0, COM3, …)

  • baudrate (int) – baud rate

  • bytesize (hat.drivers.serial.ByteSize) – number of data bits

  • parity (hat.drivers.serial.Parity) – parity checking

  • stopbits (hat.drivers.serial.StopBits) – number of stop bits

  • rtscts (bool) – enable hardware RTS/CTS flow control

  • dsrdtr (bool) – enable hardware DSR/DTR flow control

  • silent_interval (float) – minimum time in seconds between writing two consecutive messages

Return type

hat.drivers.serial.Connection

class hat.drivers.serial.Connection

Bases: object

Serial connection

For creating new instances see hat.drivers.serial.open()

property closed

closed future

Type

asyncio.Future

async async_close()

Async close

async read(size)

Read

Parameters

size (int) – number of bytes to read

Returns

bytes

async write(data)

Write

Parameters

data (bytes) – data