hvl_ccb.comm.modbus

Inheritance diagram of hvl_ccb.comm.modbus

Communication protocol for Modbus communications. Makes use of the pymodbus library.

class ModbusBaseCommunication(configuration)[source]

Bases: CommunicationProtocol, ABC

Implements the Communication Protocol for modbus.

close() None[source]

Close the Modbus connection.

open() None[source]

Open the Modbus connection.

Raises:

ModbusConnectionFailedError – if the connection fails.

read_holding_registers(address: int, count: int) list[int][source]

Read specified number of register starting with given address and return the values from each register.

Parameters:
  • address – address of the first register

  • count – count of registers to read

Returns:

list of int values

read_input_registers(address: int, count: int) list[int][source]

Read specified number of register starting with given address and return the values from each register in a list.

Parameters:
  • address – address of the first register

  • count – count of registers to read

Returns:

list of int values

write_registers(address: int, values: list[int] | int) None[source]

Write values from the specified address forward.

Parameters:
  • address – address of the first register

  • values – list with all values

class ModbusBaseCommunicationConfig(unit: int = 1)[source]

Bases: object

Base modbus communication config

clean_values() None[source]
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

is_configdataclass = True
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.

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.

unit: int = 1
exception ModbusConnectionError(string='')[source]

Bases: ConnectionException, CommunicationError

Error raised when the connection failed.

class ModbusSerialCommunication(configuration)[source]

Bases: ModbusBaseCommunication

Modbus with the Serial-Version, can be RTU or ASCII

static config_cls() type[ModbusSerialCommunicationConfig][source]

Return the default configdataclass class.

Returns:

a reference to the default configdataclass class

class ModbusSerialCommunicationConfig(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, unit: int = 1, framer: str | ~pymodbus.framer.base.FramerType = FramerType.RTU)[source]

Bases: ModbusBaseCommunicationConfig, SerialCommunicationConfig

Configuration dataclass for ModbusRtuCommunication.

clean_values() None[source]
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

framer: str | FramerType = 'rtu'
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.

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.

class ModbusTcpCommunication(configuration)[source]

Bases: ModbusBaseCommunication

Modbus with the TCP-Version

static config_cls() type[ModbusTcpCommunicationConfig][source]

Return the default configdataclass class.

Returns:

a reference to the default configdataclass class

class ModbusTcpCommunicationConfig(unit: int = 1, host: str | IPv4Address | IPv6Address | None = None, port: int = 502)[source]

Bases: ModbusBaseCommunicationConfig

Configuration dataclass for ModbusTcpCommunication.

clean_values() None[source]
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

host: str | IPv4Address | IPv6Address | None = None
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.

port: int = 502
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.