hvl_ccb.comm.modbus_tcp

Inheritance diagram of hvl_ccb.comm.modbus_tcp

Communication protocol for modbus TCP ports. Makes use of the pymodbus library.

class ModbusTcpCommunication(configuration)[source]

Bases: CommunicationProtocol

Implements the Communication Protocol for modbus TCP.

close()[source]

Close the Modbus TCP connection.

static config_cls()[source]

Return the default configdataclass class.

Returns:

a reference to the default configdataclass class

open() None[source]

Open the Modbus TCP connection.

Raises:

ModbusTcpConnectionFailedError – 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)[source]

Write values from the specified address forward.

Parameters:
  • address – address of the first register

  • values – list with all values

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

Bases: object

Configuration dataclass for ModbusTcpCommunication.

clean_values()[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
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.

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.

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

Bases: ConnectionException, CommunicationError

Error raised when the connection failed.