hvl_ccb.comm.serial¶
Communication protocol for serial ports. Makes use of the pySerial library.
-
class
SerialCommunication
(configuration)[source]¶ Bases:
hvl_ccb.comm.base.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
¶ 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
-
static
-
class
SerialCommunicationBytesize
(value=<object object>, names=None, module=None, type=None, start=1, boundary=None)[source]¶ Bases:
hvl_ccb.utils.enum.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 = 'replace', wait_sec_read_text_nonempty: Union[int, float] = 0.5, default_n_attempts_read_text_nonempty: int = 10, port: Optional[str] = None, baudrate: int = 9600, parity: Union[str, hvl_ccb.comm.serial.SerialCommunicationParity] = <SerialCommunicationParity.NONE: 'N'>, stopbits: Union[int, float, hvl_ccb.comm.serial.SerialCommunicationStopbits] = <SerialCommunicationStopbits.ONE: 1>, bytesize: Union[int, hvl_ccb.comm.serial.SerialCommunicationBytesize] = <SerialCommunicationBytesize.EIGHTBITS: 8>, timeout: Union[int, float] = 2)[source]¶ Bases:
hvl_ccb.comm.base.AsyncCommunicationProtocolConfig
Configuration dataclass for
SerialCommunication
.-
Bytesize
¶
-
Parity
¶
-
Stopbits
¶
-
baudrate
: int = 9600¶ Baudrate of the serial port
-
bytesize
: Union[int, hvl_ccb.comm.serial.SerialCommunicationBytesize] = 8¶ Size of a byte, 5 to 8
-
create_serial_port
() → serial.serialposix.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
: Union[str, hvl_ccb.comm.serial.SerialCommunicationParity] = 'N'¶ Parity to be used for the connection.
-
port
: Optional[str] = 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
: Union[int, float, hvl_ccb.comm.serial.SerialCommunicationStopbits] = 1¶ Stopbits setting, can be 1, 1.5 or 2.
-
timeout
: Union[int, float] = 2¶ Timeout in seconds for the serial port
-
-
exception
SerialCommunicationIOError
[source]¶ Bases:
OSError
Serial communication related I/O errors.
-
class
SerialCommunicationParity
(value=<object object>, names=None, module=None, type=None, start=1, boundary=None)[source]¶ Bases:
hvl_ccb.utils.enum.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'¶
-