hvl_ccb.comm.serial

digraph inheritancec97af51923 { bgcolor=transparent; rankdir=TB; size=""; "ABC" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",tooltip="Helper class that provides a standard way to create an ABC using"]; "AsyncCommunicationProtocol" [URL="../hvl_ccb.comm.base.html#hvl_ccb.comm.base.AsyncCommunicationProtocol",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Abstract base class for asynchronous communication protocols"]; "CommunicationProtocol" -> "AsyncCommunicationProtocol" [arrowsize=0.5,style="setlinewidth(0.5)"]; "AsyncCommunicationProtocolConfig" [URL="../hvl_ccb.comm.base.html#hvl_ccb.comm.base.AsyncCommunicationProtocolConfig",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Base configuration data class for asynchronous communication protocols"]; "CCBError" [URL="../hvl_ccb.error.html#hvl_ccb.error.CCBError",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top"]; "CommunicationError" [URL="../hvl_ccb.comm.base.html#hvl_ccb.comm.base.CommunicationError",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top"]; "CCBError" -> "CommunicationError" [arrowsize=0.5,style="setlinewidth(0.5)"]; "CommunicationProtocol" [URL="../hvl_ccb.comm.base.html#hvl_ccb.comm.base.CommunicationProtocol",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Communication protocol abstract base class."]; "ConfigurationMixin" -> "CommunicationProtocol" [arrowsize=0.5,style="setlinewidth(0.5)"]; "ABC" -> "CommunicationProtocol" [arrowsize=0.5,style="setlinewidth(0.5)"]; "ConfigurationMixin" [URL="../hvl_ccb.configuration.html#hvl_ccb.configuration.ConfigurationMixin",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Mixin providing configuration to a class."]; "ABC" -> "ConfigurationMixin" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Enum" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",tooltip="An enumeration."]; "SerialCommunication" [URL="../hvl_ccb.comm.serial.html#hvl_ccb.comm.serial.SerialCommunication",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Implements the Communication Protocol for serial ports."]; "AsyncCommunicationProtocol" -> "SerialCommunication" [arrowsize=0.5,style="setlinewidth(0.5)"]; "SerialCommunicationBytesize" [URL="../hvl_ccb.comm.serial.html#hvl_ccb.comm.serial.SerialCommunicationBytesize",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Serial communication bytesize."]; "ValueEnum" -> "SerialCommunicationBytesize" [arrowsize=0.5,style="setlinewidth(0.5)"]; "SerialCommunicationConfig" [URL="../hvl_ccb.comm.serial.html#hvl_ccb.comm.serial.SerialCommunicationConfig",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Configuration dataclass for :class:`SerialCommunication`."]; "AsyncCommunicationProtocolConfig" -> "SerialCommunicationConfig" [arrowsize=0.5,style="setlinewidth(0.5)"]; "SerialCommunicationIOError" [URL="../hvl_ccb.comm.serial.html#hvl_ccb.comm.serial.SerialCommunicationIOError",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Serial communication related I/O errors."]; "CommunicationError" -> "SerialCommunicationIOError" [arrowsize=0.5,style="setlinewidth(0.5)"]; "SerialCommunicationParity" [URL="../hvl_ccb.comm.serial.html#hvl_ccb.comm.serial.SerialCommunicationParity",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Serial communication parity."]; "ValueEnum" -> "SerialCommunicationParity" [arrowsize=0.5,style="setlinewidth(0.5)"]; "SerialCommunicationStopbits" [URL="../hvl_ccb.comm.serial.html#hvl_ccb.comm.serial.SerialCommunicationStopbits",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Serial communication stopbits."]; "ValueEnum" -> "SerialCommunicationStopbits" [arrowsize=0.5,style="setlinewidth(0.5)"]; "StrEnumBase" [URL="../hvl_ccb.utils.enum.html#hvl_ccb.utils.enum.StrEnumBase",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="String representation-based equality and lookup."]; "Enum" -> "StrEnumBase" [arrowsize=0.5,style="setlinewidth(0.5)"]; "ValueEnum" [URL="../hvl_ccb.utils.enum.html#hvl_ccb.utils.enum.ValueEnum",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Enum with string representation of values used as string representation, and with"]; "StrEnumBase" -> "ValueEnum" [arrowsize=0.5,style="setlinewidth(0.5)"]; }

Communication protocol for serial ports. Makes use of the pySerial library.

class SerialCommunication(configuration)[source]

Bases: AsyncCommunicationProtocol

Implements the Communication Protocol for serial ports.

close()[source]

Close the serial connection.

static config_cls()[source]

Return the default configdataclass class.

Returns:

a reference to the default configdataclass class

property is_open: bool

Flag indicating if the serial port is open.

Returns:

True if the serial port is open, otherwise False

open()[source]

Open the serial connection.

Raises:

SerialCommunicationIOError – when communication port cannot be opened.

read_bytes() bytes[source]

Read the bytes from the serial port till the terminator is found. The input buffer may hold additional lines afterwards.

This method uses self.access_lock to ensure thread-safety.

Returns:

Bytes read from the serial port; b’’ if there was nothing to read.

Raises:

SerialCommunicationIOError – when communication port is not opened

read_single_bytes(size: int = 1) bytes[source]

Read the specified number of bytes from the serial port. The input buffer may hold additional data afterwards.

Returns:

Bytes read from the serial port; b’’ if there was nothing to read.

write_bytes(data: bytes) int[source]

Write bytes to the serial port.

This method uses self.access_lock to ensure thread-safety.

Parameters:

data – data to write to the serial port

Returns:

number of bytes written

Raises:

SerialCommunicationIOError – when communication port is not opened

class SerialCommunicationBytesize(value=<no_arg>, names=None, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: ValueEnum

Serial communication bytesize.

EIGHTBITS = 8
FIVEBITS = 5
SEVENBITS = 7
SIXBITS = 6
class SerialCommunicationConfig(terminator: bytes = b'\r\n', encoding: str = 'utf-8', encoding_error_handling: str = 'strict', wait_sec_read_text_nonempty: int | float = 0.5, default_n_attempts_read_text_nonempty: int = 10, port: str | None = None, baudrate: int = 9600, parity: str | SerialCommunicationParity = SerialCommunicationParity.NONE, stopbits: int | float | SerialCommunicationStopbits = SerialCommunicationStopbits.ONE, bytesize: int | SerialCommunicationBytesize = SerialCommunicationBytesize.EIGHTBITS, timeout: int | float = 2)[source]

Bases: AsyncCommunicationProtocolConfig

Configuration dataclass for SerialCommunication.

Bytesize

alias of SerialCommunicationBytesize

Parity

alias of SerialCommunicationParity

Stopbits

alias of SerialCommunicationStopbits

baudrate: int = 9600

Baudrate of the serial port

bytesize: int | SerialCommunicationBytesize = 8

Size of a byte, 5 to 8

clean_values()[source]
create_serial_port() Serial[source]

Create a serial port instance according to specification in this configuration

Returns:

Closed serial port instance

force_value(fieldname, value)

Forces a value to a dataclass field despite the class being frozen.

NOTE: you can define post_force_value method with same signature as this method to do extra processing after value has been forced on fieldname.

Parameters:
  • fieldname – name of the field

  • value – value to assign

classmethod keys() Sequence[str]

Returns a list of all configdataclass fields key-names.

Returns:

a list of strings containing all keys.

classmethod optional_defaults() dict[str, object]

Returns a list of all configdataclass fields, that have a default value assigned and may be optionally specified on instantiation.

Returns:

a list of strings containing all optional keys.

parity: str | SerialCommunicationParity = 'N'

Parity to be used for the connection.

port: str | None = None

Port is a string referring to a COM-port (e.g. 'COM3') or a URL. The full list of capabilities is found on the pyserial documentation.

classmethod required_keys() Sequence[str]

Returns a list of all configdataclass fields, that have no default value assigned and need to be specified on instantiation.

Returns:

a list of strings containing all required keys.

stopbits: int | float | SerialCommunicationStopbits = 1

Stopbits setting, can be 1, 1.5 or 2.

terminator_str() str[source]
timeout: int | float = 2

Timeout in seconds for the serial port

exception SerialCommunicationIOError[source]

Bases: OSError, CommunicationError

Serial communication related I/O errors.

class SerialCommunicationParity(value=<no_arg>, names=None, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: ValueEnum

Serial communication parity.

EVEN = 'E'
MARK = 'M'
NAMES = {'E': 'Even', 'M': 'Mark', 'N': 'None', 'O': 'Odd', 'S': 'Space'}
NONE = 'N'
ODD = 'O'
SPACE = 'S'
class SerialCommunicationStopbits(value=<no_arg>, names=None, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: ValueEnum

Serial communication stopbits.

ONE = 1
ONE_POINT_FIVE = 1.5
TWO = 2