diff --git a/src/pyhoff/__init__.py b/src/pyhoff/__init__.py index 4ddaf2c..3d5dba1 100644 --- a/src/pyhoff/__init__.py +++ b/src/pyhoff/__init__.py @@ -13,13 +13,6 @@ class BusTerminal(): """ Base class for all bus terminals. - Args: - bus_coupler: The bus coupler to which this terminal is connected. - output_bit_addresses: List of addresses of the output bits. - input_bit_addresses: List of addresses of input bits. - output_word_addresses: List of addresses of output words. - input_word_addresses: List of addresses of input words. - Attributes: bus_coupler: The bus coupler to which this terminal is connected. parameters: The parameters of the terminal. @@ -32,7 +25,16 @@ class BusTerminal(): output_word_addresses: list[int], input_word_addresses: list[int], mixed_mapping: bool): + """ + Instantiate a new BusTerminal base class. + Args: + bus_coupler: The bus coupler to which this terminal is connected. + output_bit_addresses: List of addresses of the output bits. + input_bit_addresses: List of addresses of input bits. + output_word_addresses: List of addresses of output words. + input_word_addresses: List of addresses of input words. + """ self.bus_coupler = bus_coupler self._output_bit_addresses = output_bit_addresses self._input_bit_addresses = input_bit_addresses @@ -221,19 +223,30 @@ class BusCoupler(): bus_terminals: A list of bus terminal classes according to the connected terminals. modbus: The underlying modbus client used for the connection. - - Examples: - >>> from pyhoff.devices import * - >>> bk = BK9000('192.168.0.23', bus_terminals=[KL3202, KL9010]) - >>> t1 = bk.terminals[0].read_temperature(1) - >>> t2 = bk.terminals[0].read_temperature(2) - >>> print(f"Temperature ch1: {t1:.1f} °C, Temperature ch2: {t2:.1f} °C") - Temperature ch1: 23.2 °C, Temperature ch2: 22.1 °C """ def __init__(self, host: str, port: int = 502, bus_terminals: Iterable[Type[BusTerminal]] = [], timeout: float = 5, watchdog: float = 0, debug: bool = False): + """ + Instantiate a new bus coupler base class. + Args: + host: ip or hostname of the bus coupler + port: port of the modbus host + debug: outputs modbus debug information + timeout: timeout for waiting for the device response + watchdog: time in seconds after the device sets all outputs to + default state. A value of 0 deactivates the watchdog. + debug: If True, debug information is printed. + + Examples: + >>> from pyhoff.devices import * + >>> bk = BK9000('192.168.0.23', bus_terminals=[KL3202, KL9010]) + >>> t1 = bk.terminals[0].read_temperature(1) + >>> t2 = bk.terminals[0].read_temperature(2) + >>> print(f"Temperature ch1: {t1:.1f} °C, Temperature ch2: {t2:.1f} °C") + Temperature ch1: 23.2 °C, Temperature ch2: 22.1 °C + """ self.bus_terminals: list[BusTerminal] = list() self._next_output_bit_offset = 0 self._next_input_bit_offset = 0 diff --git a/src/pyhoff/modbus.py b/src/pyhoff/modbus.py index 7c4d11d..fccc91a 100644 --- a/src/pyhoff/modbus.py +++ b/src/pyhoff/modbus.py @@ -63,19 +63,31 @@ class SimpleModbusClient: last_error: contains last error message or empty string if no error occurred debug: if True prints out transmitted and received bytes in hex - Example: - >>> client = SimpleModbusClient('localhost', port = 502, unit_id = 1) - >>> print(client.read_coils(0, 10)) - >>> print(client.read_discrete_inputs(0, 10)) - >>> print(client.read_holding_registers(0, 10)) - >>> print(client.read_input_registers(0, 10)) - >>> print(client.write_single_coil(0, True)) - >>> print(client.write_single_register(0, 1234)) - >>> print(client.write_multiple_coils(0, [True, False, True])) - >>> print(client.write_multiple_registers(0, [1234, 5678])) - >>> client.close() """ + def __init__(self, host: str, port: int = 502, unit_id: int = 1, timeout: float = 5, debug: bool = False): + """ + Instantiate a Modbus TCP client + + Args: + host: hostname or IP address + port: server port + unit_id: ModBus id + timeout: socket timeout in seconds + debug: if True prints out transmitted and received bytes in hex + + Example: + >>> client = SimpleModbusClient('localhost', port = 502, unit_id = 1) + >>> print(client.read_coils(0, 10)) + >>> print(client.read_discrete_inputs(0, 10)) + >>> print(client.read_holding_registers(0, 10)) + >>> print(client.read_input_registers(0, 10)) + >>> print(client.write_single_coil(0, True)) + >>> print(client.write_single_register(0, 1234)) + >>> print(client.write_multiple_coils(0, [True, False, True])) + >>> print(client.write_multiple_registers(0, [1234, 5678])) + >>> client.close() + """ assert 0 <= unit_id < 256 self.host = host @@ -88,6 +100,10 @@ class SimpleModbusClient: self.debug = debug def connect(self) -> bool: + """ + Connect manual to the configured modbus server. Usually there is + no need to call this function since it is handled automatically. + """ for af, st, pr, _, sa in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):