Classes

Bus coupler

These classes are bus couplers and are used to connect the IO bus terminals to a Ethernet interface.

class pyhoff.devices.BK9000(host, port=502, bus_terminals=[], timeout=5, watchdog=0, debug=False)

Bases: BusCoupler

BK9000 ModBus TCP bus coupler

add_bus_terminals(*new_bus_terminals)

Add bus terminals to the bus coupler.

Parameters:

new_bus_terminals (Union[type[BusTerminal], Iterable[type[BusTerminal]]]) – bus terminal classes to add.

Return type:

list[BusTerminal]

Returns:

The corresponding list of bus terminal objects.

get_error()

Get the last error message.

Return type:

str

Returns:

The last error message.

class pyhoff.devices.BK9050(host, port=502, bus_terminals=[], timeout=5, watchdog=0, debug=False)

Bases: BK9000

BK9050 ModBus TCP bus coupler

add_bus_terminals(*new_bus_terminals)

Add bus terminals to the bus coupler.

Parameters:

new_bus_terminals (Union[type[BusTerminal], Iterable[type[BusTerminal]]]) – bus terminal classes to add.

Return type:

list[BusTerminal]

Returns:

The corresponding list of bus terminal objects.

get_error()

Get the last error message.

Return type:

str

Returns:

The last error message.

class pyhoff.devices.BK9100(host, port=502, bus_terminals=[], timeout=5, watchdog=0, debug=False)

Bases: BK9000

BK9100 ModBus TCP bus coupler

add_bus_terminals(*new_bus_terminals)

Add bus terminals to the bus coupler.

Parameters:

new_bus_terminals (Union[type[BusTerminal], Iterable[type[BusTerminal]]]) – bus terminal classes to add.

Return type:

list[BusTerminal]

Returns:

The corresponding list of bus terminal objects.

get_error()

Get the last error message.

Return type:

str

Returns:

The last error message.

class pyhoff.devices.WAGO_750_352(host, port=502, bus_terminals=[], timeout=5, watchdog=0, debug=False)

Bases: BusCoupler

Wago 750-352 ModBus TCP bus coupler

add_bus_terminals(*new_bus_terminals)

Add bus terminals to the bus coupler.

Parameters:

new_bus_terminals (Union[type[BusTerminal], Iterable[type[BusTerminal]]]) – bus terminal classes to add.

Return type:

list[BusTerminal]

Returns:

The corresponding list of bus terminal objects.

get_error()

Get the last error message.

Return type:

str

Returns:

The last error message.

Beckhoff bus terminals

class pyhoff.devices.KL1104(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: DigitalInputTerminal4Bit

KL1104: 4x digital input 24 V

parameters: dict[str, int] = {'input_bit_width': 4}
read_input(channel)

Read the input from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The input value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

class pyhoff.devices.KL1408(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: DigitalInputTerminal8Bit

KL1104: 8x digital input 24 V galvanic isolated

parameters: dict[str, int] = {'input_bit_width': 8}
read_input(channel)

Read the input from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The input value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

class pyhoff.devices.KL1512(bus_coupler, o_b_addr, i_b_addr, o_w_addr, i_w_addr, mixed_mapping)

Bases: AnalogInputTerminal

KL1512: 2x 16 bit counter, 24 V DC, 1 kHz

parameters: dict[str, int] = {'input_word_width': 2}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value.

Raises:

Exception – If the word offset or count is out of range.

read_counter(channel)

Read the absolut counter value of a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

int

Returns:

The counter value.

read_delta(channel)

Read the counter change since last read of a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

int

Returns:

The counter value.

read_normalized(channel)

Read a normalized value (0…1) from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The normalized value.

class pyhoff.devices.KL2404(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: DigitalOutputTerminal4Bit

KL2404: 4x digital output with 500 mA

parameters: dict[str, int] = {'output_bit_width': 4}
read_coil(channel)

Read the coil value back from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The coil value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

write_coil(channel, value)

Write a value to a specific channel.

Parameters:
  • channel (int) – The channel number (start counting from 1) to write to.

  • value (bool) – The value to write.

Return type:

bool

Returns:

True if the write operation succeeded, otherwise False.

Raises:

Exception – If the channel number is out of range.

class pyhoff.devices.KL2408(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: DigitalOutputTerminal8Bit

750-530: 8x digital output with 24 V / 500 mA

Contact order for DO1 to DO8 is: 1, 5, 2, 6, 3, 7, 4, 8.

parameters: dict[str, int] = {'output_bit_width': 8}
read_coil(channel)

Read the coil value back from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The coil value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

write_coil(channel, value)

Write a value to a specific channel.

Parameters:
  • channel (int) – The channel number (start counting from 1) to write to.

  • value (bool) – The value to write.

Return type:

bool

Returns:

True if the write operation succeeded, otherwise False.

Raises:

Exception – If the channel number is out of range.

class pyhoff.devices.KL2424(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: DigitalOutputTerminal4Bit

KL2424: 4x digital output with 2000 mA

parameters: dict[str, int] = {'output_bit_width': 4}
read_coil(channel)

Read the coil value back from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The coil value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

write_coil(channel, value)

Write a value to a specific channel.

Parameters:
  • channel (int) – The channel number (start counting from 1) to write to.

  • value (bool) – The value to write.

Return type:

bool

Returns:

True if the write operation succeeded, otherwise False.

Raises:

Exception – If the channel number is out of range.

class pyhoff.devices.KL2634(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: DigitalOutputTerminal4Bit

KL2634: 4x digital output 250 V AC, 30 V DC, 4 A

parameters: dict[str, int] = {'output_bit_width': 4}
read_coil(channel)

Read the coil value back from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The coil value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

write_coil(channel, value)

Write a value to a specific channel.

Parameters:
  • channel (int) – The channel number (start counting from 1) to write to.

  • value (bool) – The value to write.

Return type:

bool

Returns:

True if the write operation succeeded, otherwise False.

Raises:

Exception – If the channel number is out of range.

class pyhoff.devices.KL3042(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: AnalogInputTerminal

KL3042: 2x analog input 0…20 mA 12 Bit single-ended

parameters: dict[str, int] = {'input_word_width': 2}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value.

Raises:

Exception – If the word offset or count is out of range.

read_current(channel)

Read the current value from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The current value.

read_normalized(channel)

Read a normalized value (0…1) from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The normalized value.

class pyhoff.devices.KL3054(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: AnalogInputTerminal

KL3054: 4x analog input 4…20 mA 12 Bit single-ended

parameters: dict[str, int] = {'input_word_width': 4}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value.

Raises:

Exception – If the word offset or count is out of range.

read_current(channel)

Read the current value from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The current value.

read_normalized(channel)

Read a normalized value (0…1) from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The normalized value.

class pyhoff.devices.KL3202(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: AnalogInputTerminal

KL3202: 2x analog input PT100 16 Bit 3-wire

parameters: dict[str, int] = {'input_word_width': 2}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value.

Raises:

Exception – If the word offset or count is out of range.

read_normalized(channel)

Read a normalized value (0…1) from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The normalized value.

read_temperature(channel)

Read the temperature value from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The temperature value in °C.

class pyhoff.devices.KL3214(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: AnalogInputTerminal

KL3214: 4x analog input PT100 16 Bit 3-wire

parameters: dict[str, int] = {'input_word_width': 4}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value.

Raises:

Exception – If the word offset or count is out of range.

read_normalized(channel)

Read a normalized value (0…1) from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The normalized value.

read_temperature(channel)

Read the temperature value from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The temperature value.

class pyhoff.devices.KL4002(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: AnalogOutputTerminal

KL4002: 2x analog output 0…10 V 12 Bit differentiell

parameters: dict[str, int] = {'output_word_width': 2}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value or provided error_value if read failed.

Raises:

Exception – If the word offset or count is out of range.

set_normalized(channel, value)

Set a normalized value between 0 and 1 to a specific channel.

Parameters:
  • channel (int) – The channel number to set.

  • value (float) – The normalized value to set.

Return type:

bool

Returns:

True if the write operation succeeded.

set_voltage(channel, value)

Set a voltage value to a specific channel.

Parameters:
  • channel (int) – The channel number to set.

  • value (float) – The voltage value to set.

Return type:

bool

Returns:

True if the write operation succeeded.

write_channel_word(channel, value)

Write a word to the terminal.

Parameters:

channel (int) – The channel number (1 based index) to write to.

Return type:

bool

Returns:

True if the write operation succeeded.

Raises:

Exception – If the word offset or count is out of range.

class pyhoff.devices.KL4004(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: AnalogOutputTerminal

KL4004: 4x analog output 0…10 V 12 Bit differentiell

parameters: dict[str, int] = {'output_word_width': 4}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value or provided error_value if read failed.

Raises:

Exception – If the word offset or count is out of range.

set_normalized(channel, value)

Set a normalized value between 0 and 1 to a specific channel.

Parameters:
  • channel (int) – The channel number to set.

  • value (float) – The normalized value to set.

Return type:

bool

Returns:

True if the write operation succeeded.

set_voltage(channel, value)

Set a voltage value to a specific channel.

Parameters:
  • channel (int) – The channel number to set.

  • value (float) – The voltage value to set.

Return type:

bool

Returns:

True if the write operation succeeded.

write_channel_word(channel, value)

Write a word to the terminal.

Parameters:

channel (int) – The channel number (1 based index) to write to.

Return type:

bool

Returns:

True if the write operation succeeded.

Raises:

Exception – If the word offset or count is out of range.

class pyhoff.devices.KL4132(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: AnalogOutputTerminal

KL4002: 2x analog output ±10 V 16 bit differential

parameters: dict[str, int] = {'output_word_width': 2}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value or provided error_value if read failed.

Raises:

Exception – If the word offset or count is out of range.

set_normalized(channel, value)

Set a normalized value between -1 and +1 to a specific channel.

Parameters:
  • channel (int) – The channel number to set.

  • value (float) – The normalized value to set.

Return type:

bool

Returns:

True if the write operation succeeded.

set_voltage(channel, value)

Set a voltage value between -10 and +10 V to a specific channel.

Parameters:
  • channel (int) – The channel number to set.

  • value (float) – The voltage value to set.

Return type:

bool

Returns:

True if the write operation succeeded.

write_channel_word(channel, value)

Write a word to the terminal.

Parameters:

channel (int) – The channel number (1 based index) to write to.

Return type:

bool

Returns:

True if the write operation succeeded.

Raises:

Exception – If the word offset or count is out of range.

class pyhoff.devices.KL9010(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9010: End terminal (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9070(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9070: Shield terminal (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9080(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9080: Separation terminal (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9100(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9100: Potential supply terminal, 24 V DC (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9150(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9150: Potential supply terminal, 120…230 V AC (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9180(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9180: Potential distribution terminal, 2 x 24 V DC; 2 x 0 V DC, 2 x PE (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9184(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9184: potential distribution terminal, 8 x 24 V DC, 8 x 0 V DC (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9185(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9185: potential distribution terminal, 4 x 24 V DC, 4 x 0 V DC (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9186(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9186: Potential distribution terminal, 8 x 24 V DC (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9187(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9187: Potential distribution terminal, 8 x 0 V DC (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9188(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9188: Potential distribution terminal, 16 x 24 V DC (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9189(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9189: Potential distribution terminal, 16 x 0 V DC (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9190(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9190: Potential supply terminal, any voltage up to 230 V AC (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9195(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9195: Shield terminal (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9200(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9200: Potential supply terminal, 24 V DC, with fuse (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9250(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9250: Potential supply terminal, 120…230 V AC, with fuse (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9290(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9290: Potential supply terminal, any voltage up to 230 V AC, with fuse (no I/O function)

parameters: dict[str, int] = {}
class pyhoff.devices.KL9380(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

KL9380: Mains filter terminal for dimmers (no I/O function)

parameters: dict[str, int] = {}

WAGO bus terminals

class pyhoff.devices.WAGO_750_1405(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: DigitalInputTerminal16Bit

750-1405: 16x digital input 24 V

parameters: dict[str, int] = {'input_bit_width': 16}
read_input(channel)

Read the input from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The input value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

class pyhoff.devices.WAGO_750_352(host, port=502, bus_terminals=[], timeout=5, watchdog=0, debug=False)

Bases: BusCoupler

Wago 750-352 ModBus TCP bus coupler

add_bus_terminals(*new_bus_terminals)

Add bus terminals to the bus coupler.

Parameters:

new_bus_terminals (Union[type[BusTerminal], Iterable[type[BusTerminal]]]) – bus terminal classes to add.

Return type:

list[BusTerminal]

Returns:

The corresponding list of bus terminal objects.

get_error()

Get the last error message.

Return type:

str

Returns:

The last error message.

class pyhoff.devices.WAGO_750_530(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: DigitalOutputTerminal8Bit

750-530: 8x digital output with 24 V / 500 mA

Contact order for DO1 to DO8 is: 1, 5, 2, 6, 3, 7, 4, 8.

parameters: dict[str, int] = {'output_bit_width': 8}
read_coil(channel)

Read the coil value back from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The coil value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

write_coil(channel, value)

Write a value to a specific channel.

Parameters:
  • channel (int) – The channel number (start counting from 1) to write to.

  • value (bool) – The value to write.

Return type:

bool

Returns:

True if the write operation succeeded, otherwise False.

Raises:

Exception – If the channel number is out of range.

class pyhoff.devices.WAGO_750_600(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

End terminal, no I/O function

parameters: dict[str, int] = {}
class pyhoff.devices.WAGO_750_602(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

Potential supply terminal, no I/O function

parameters: dict[str, int] = {}

Base classes

These classes are base classes for devices and are typically not used directly.

class pyhoff.AnalogInputTerminal(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

Base class for analog input terminals.

parameters: dict[str, int] = {}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value.

Raises:

Exception – If the word offset or count is out of range.

read_normalized(channel)

Read a normalized value (0…1) from a specific channel.

Parameters:

channel (int) – The channel number to read from.

Return type:

float

Returns:

The normalized value.

classmethod select(bus_coupler, terminal_number=0)

Returns the n-th bus terminal instance of the parent class specified by terminal_number.

Parameters:
  • bus_coupler (BusCoupler) – The bus coupler to which the terminal is connected.

  • terminal_number (int) – The index of the bus terminal to return. Counted for all bus terminals of the same type, not all bus terminals. Started for the first terminal with 0

Return type:

TypeVar(_BT, bound= BusTerminal)

Returns:

The selected bus terminal instance.

class pyhoff.AnalogOutputTerminal(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

Base class for analog output terminals.

parameters: dict[str, int] = {}
read_channel_word(channel, error_value=-99999)

Read a single word from the terminal.

Parameters:

channel (int) – The channel number (1 based index) to read from.

Return type:

int

Returns:

The read word value or provided error_value if read failed.

Raises:

Exception – If the word offset or count is out of range.

classmethod select(bus_coupler, terminal_number=0)

Returns the n-th bus terminal instance of the parent class specified by terminal_number.

Parameters:
  • bus_coupler (BusCoupler) – The bus coupler to which the terminal is connected.

  • terminal_number (int) – The index of the bus terminal to return. Counted for all bus terminals of the same type, not all bus terminals. Started for the first terminal with 0

Return type:

TypeVar(_BT, bound= BusTerminal)

Returns:

The selected bus terminal instance.

set_normalized(channel, value)

Set a normalized value between 0 and 1 to a specific channel.

Parameters:
  • channel (int) – The channel number to set.

  • value (float) – The normalized value to set.

Return type:

bool

Returns:

True if the write operation succeeded.

write_channel_word(channel, value)

Write a word to the terminal.

Parameters:

channel (int) – The channel number (1 based index) to write to.

Return type:

bool

Returns:

True if the write operation succeeded.

Raises:

Exception – If the word offset or count is out of range.

class pyhoff.BusCoupler(host, port=502, bus_terminals=[], timeout=5, watchdog=0, debug=False)

Bases: object

Base class for ModBus TCP bus coupler

Parameters:
  • host (str) – ip or hostname of the bus coupler

  • port (int) – port of the modbus host

  • debug (bool) – outputs modbus debug information

  • timeout (float) – timeout for waiting for the device response

  • watchdog (float) – 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.

bus_terminals

A list of bus terminal classes according to the connected terminals.

modbus

The underlying modbus client used for the connection.

add_bus_terminals(*new_bus_terminals)

Add bus terminals to the bus coupler.

Parameters:

new_bus_terminals (Union[type[BusTerminal], Iterable[type[BusTerminal]]]) – bus terminal classes to add.

Return type:

list[BusTerminal]

Returns:

The corresponding list of bus terminal objects.

get_error()

Get the last error message.

Return type:

str

Returns:

The last error message.

select(bus_terminal_type, terminal_number=0)

Returns the n-th bus terminal instance of the given bus terminal type and terminal index.

Parameters:
  • bus_terminals_type – The bus terminal class to select from.

  • terminal_number (int) – The index of the bus terminal to return. Counted for all bus terminals of the same type, not all bus terminals. Started for the first terminal with 0

Return type:

TypeVar(_BT, bound= BusTerminal)

Returns:

The selected bus terminal instance.

Example

>>> from pyhoff.devices import *
>>> bk = BK9050("172.16.17.1", bus_terminals=[KL2404, KL2424])
>>> # Select the first KL2425 terminal:
>>> kl2404 = bk.select(KL2424, 0)
class pyhoff.BusTerminal(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: object

Base class for all bus terminals.

bus_coupler

The bus coupler to which this terminal is connected.

parameters

The parameters of the terminal.

parameters: dict[str, int] = {}
classmethod select(bus_coupler, terminal_number=0)

Returns the n-th bus terminal instance of the parent class specified by terminal_number.

Parameters:
  • bus_coupler (BusCoupler) – The bus coupler to which the terminal is connected.

  • terminal_number (int) – The index of the bus terminal to return. Counted for all bus terminals of the same type, not all bus terminals. Started for the first terminal with 0

Return type:

TypeVar(_BT, bound= BusTerminal)

Returns:

The selected bus terminal instance.

class pyhoff.DigitalInputTerminal(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

Base class for digital input terminals.

parameters: dict[str, int] = {}
read_input(channel)

Read the input from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The input value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

classmethod select(bus_coupler, terminal_number=0)

Returns the n-th bus terminal instance of the parent class specified by terminal_number.

Parameters:
  • bus_coupler (BusCoupler) – The bus coupler to which the terminal is connected.

  • terminal_number (int) – The index of the bus terminal to return. Counted for all bus terminals of the same type, not all bus terminals. Started for the first terminal with 0

Return type:

TypeVar(_BT, bound= BusTerminal)

Returns:

The selected bus terminal instance.

class pyhoff.DigitalOutputTerminal(bus_coupler, output_bit_addresses, input_bit_addresses, output_word_addresses, input_word_addresses, mixed_mapping)

Bases: BusTerminal

Base class for digital output terminals.

parameters: dict[str, int] = {}
read_coil(channel)

Read the coil value back from a specific channel.

Parameters:

channel (int) – The channel number (start counting from 1) to read from.

Return type:

bool | None

Returns:

The coil value of the specified channel or None if the read operation failed.

Raises:

Exception – If the channel number is out of range.

classmethod select(bus_coupler, terminal_number=0)

Returns the n-th bus terminal instance of the parent class specified by terminal_number.

Parameters:
  • bus_coupler (BusCoupler) – The bus coupler to which the terminal is connected.

  • terminal_number (int) – The index of the bus terminal to return. Counted for all bus terminals of the same type, not all bus terminals. Started for the first terminal with 0

Return type:

TypeVar(_BT, bound= BusTerminal)

Returns:

The selected bus terminal instance.

write_coil(channel, value)

Write a value to a specific channel.

Parameters:
  • channel (int) – The channel number (start counting from 1) to write to.

  • value (bool) – The value to write.

Return type:

bool

Returns:

True if the write operation succeeded, otherwise False.

Raises:

Exception – If the channel number is out of range.

Modbus

This modbus implementation is used internally.

class pyhoff.modbus.SimpleModbusClient(host, port=502, unit_id=1, timeout=5, debug=False)

Bases: object

A simple Modbus TCP client

Parameters:
  • host (str) – hostname or IP address

  • port (int) – server port

  • unit_id (int) – ModBus id

  • timeout (float) – socket timeout in seconds

  • debug (bool) – if True prints out transmitted and received bytes in hex

host

hostname or IP address

port

server port

unit_id

ModBus id

timeout

socket timeout in seconds

last_error

contains last error message or empty string if no error occurred

debug

if True prints out transmitted and received bytes in hex

close()

Close connection

Return type:

bytes

Returns:

empty bytes object

connect()

Connect manual to the configured modbus server. Usually there is no need to call this function since it is handled automatically.

Return type:

bool

read_coil(address)

Read a coil from the given register address.

Parameters:

address (int) – The register address to read from.

Return type:

bool | None

Returns:

The value of the coil or None if error

read_coils(bit_address, bit_lengths=1)

ModBus function for reading coils (0x01)

Parameters:
  • bit_address (int) – Bit address (0 to 0xffff)

  • bit_lengths (int) – Number of bits to read (1 to 2000)

Returns:

Bits list or None if error

Return type:

list of bool or None

read_discrete_input(address)

Read a discrete input from the given register address.

Parameters:

address (int) – The register address to read from.

Return type:

bool | None

Returns:

The value of the discrete input.

read_discrete_inputs(bit_address, bit_lengths=1)

ModBus function for reading discrete inputs (0x02)

Parameters:
  • bit_address (int) – Bit address (0 to 0xffff)

  • bit_lengths (int) – Number of bits to read (1 to 2000)

Returns:

Bits list or None if error

Return type:

list of bool or None

read_holding_registers(register_address, word_lengths=1)

ModBus function for reading holding registers (0x03)

Parameters:
  • register_address (int) – Register address (0 to 0xffff)

  • word_lengths (int) – Number of registers to read (1 to 125)

Returns:

Registers list or None if error

Return type:

list of int or None

read_input_registers(register_address, word_lengths=1)

ModBus function for reading input registers (0x04)

Parameters:
  • register_address (int) – Register address (0 to 0xffff)

  • word_lengths (int) – Number of registers to read (1 to 125)

Returns:

Registers list or None if error

Return type:

list of int or None

receive_modbus_data()

Receive a ModBus frame

Return type:

bytes

Returns:

bytes received or empty bytes object if an error occurred

send_modbus_data(function_code, body)

Send raw ModBus TCP frame

Parameters:
  • unction_code – ModBus function code

  • body (bytes) – data

Return type:

int

Returns:

number of transmitted bytes or 0 if transmission failed

write_multiple_coils(bit_address, values)

ModBus function for writing multiple coils (0x0F)

Parameters:
  • bit_address (int) – Bit address (0 to 0xffff)

  • values (list[bool]) – List of bit values to write

Return type:

bool

Returns:

True if write succeeded or False if failed

write_multiple_registers(register_address, values)

ModBus function for writing multiple registers (0x10)

Parameters:
  • register_address (int) – Register address (0 to 0xffff)

  • values (list[int]) – List of 16 bit register values to write

Return type:

bool

Returns:

True if write succeeded or False if failed

write_single_coil(bit_address, value)

ModBus function for writing a single coil (0x05)

Parameters:
  • bit_address (int) – Bit address (0 to 0xffff)

  • value (bool) – Value to write (single bit)

Return type:

bool

Returns:

True if write succeeded or False if failed

write_single_register(register_address, value)

ModBus function for writing a single register (0x06)

Parameters:
  • register_address (int) – Register address (0 to 0xffff)

  • value (int) – Value to write (0 to 0xffff)

Return type:

bool

Returns:

True if write succeeded or False if failed