hvl_ccb.comm.serial
Communication protocol for serial ports. Makes use of the pySerial library.
- class SerialCommunication(configuration)[source]
Bases:
AsyncCommunicationProtocol
Implements the Communication Protocol for serial ports.
- 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 | ~hvl_ccb.comm.serial.SerialCommunicationParity = <SerialCommunicationParity.NONE: 'N'>, stopbits: int | float | ~hvl_ccb.comm.serial.SerialCommunicationStopbits = <SerialCommunicationStopbits.ONE: 1>, bytesize: int | ~hvl_ccb.comm.serial.SerialCommunicationBytesize = <SerialCommunicationBytesize.EIGHTBITS: 8>, 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
- 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.
- 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'