2025-02-16 22:49:16 +00:00
|
|
|
import inspect
|
2025-02-17 13:57:03 +00:00
|
|
|
import pyhoff as pyhoff
|
|
|
|
import pyhoff.devices as devices
|
|
|
|
from pyhoff.devices import DigitalInputTerminal, DigitalOutputTerminal, AnalogInputTerminal, AnalogOutputTerminal
|
2025-02-16 22:49:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_terminal_plausib():
|
2025-02-17 08:31:28 +00:00
|
|
|
"""
|
|
|
|
Test if all implemented BusTerminal classes in devices
|
|
|
|
have the plausible parameters
|
|
|
|
"""
|
2025-02-16 22:49:16 +00:00
|
|
|
|
|
|
|
for n, o in inspect.getmembers(devices):
|
|
|
|
if inspect.isclass(o) and o not in [DigitalInputTerminal,
|
|
|
|
DigitalOutputTerminal,
|
|
|
|
AnalogInputTerminal,
|
|
|
|
AnalogOutputTerminal]:
|
2025-02-20 16:14:23 +00:00
|
|
|
|
2025-02-16 22:49:16 +00:00
|
|
|
print('Terminal: ' + n)
|
|
|
|
if issubclass(o, DigitalInputTerminal):
|
|
|
|
assert o.parameters.get('input_bit_width', 0) > 0
|
2025-02-20 16:14:23 +00:00
|
|
|
# assert o.parameters.get('output_bit_width', 0) == 0
|
2025-02-16 22:49:16 +00:00
|
|
|
assert o.parameters.get('input_word_width', 0) == 0
|
|
|
|
assert o.parameters.get('output_word_width', 0) == 0
|
|
|
|
|
|
|
|
if issubclass(o, DigitalOutputTerminal):
|
2025-02-20 16:14:23 +00:00
|
|
|
# assert o.parameters.get('input_bit_width', 0) == 0
|
2025-02-16 22:49:16 +00:00
|
|
|
assert o.parameters.get('output_bit_width', 0) > 0
|
|
|
|
assert o.parameters.get('input_word_width', 0) == 0
|
|
|
|
assert o.parameters.get('output_word_width', 0) == 0
|
|
|
|
|
|
|
|
if issubclass(o, AnalogInputTerminal):
|
|
|
|
assert o.parameters.get('input_bit_width', 0) == 0
|
|
|
|
assert o.parameters.get('output_bit_width', 0) == 0
|
|
|
|
assert o.parameters.get('input_word_width', 0) > 0
|
|
|
|
|
|
|
|
if issubclass(o, AnalogOutputTerminal):
|
|
|
|
assert o.parameters.get('input_bit_width', 0) == 0
|
|
|
|
assert o.parameters.get('output_bit_width', 0) == 0
|
|
|
|
assert o.parameters.get('output_word_width', 0) > 0
|
2025-02-17 08:31:28 +00:00
|
|
|
|
|
|
|
|
2025-02-19 16:21:23 +00:00
|
|
|
def rw_all_bus_terminals(bus_cupler: pyhoff.BusCoupler):
|
|
|
|
for bt in bus_cupler.bus_terminals:
|
|
|
|
if isinstance(bt, AnalogOutputTerminal):
|
|
|
|
for channel in range(1, bt.parameters.get('output_word_width', 0) + 1):
|
|
|
|
bt.set_normalized(channel, 0)
|
|
|
|
bt.set_normalized(channel, 1)
|
|
|
|
bt.set_normalized(channel, 2)
|
|
|
|
|
|
|
|
if isinstance(bt, AnalogInputTerminal):
|
|
|
|
for channel in range(1, bt.parameters.get('input_word_width', 0) + 1):
|
|
|
|
assert bt.read_channel_word(channel, 1337) == 1337
|
|
|
|
assert bt.read_channel_word(channel, 1337) == 1337
|
|
|
|
assert bt.read_channel_word(channel, 1337) == 1337
|
|
|
|
|
|
|
|
if isinstance(bt, DigitalOutputTerminal):
|
|
|
|
for channel in range(1, bt.parameters.get('output_bit_width', 0) + 1):
|
|
|
|
assert not bt.write_coil(channel, True)
|
|
|
|
assert not bt.write_coil(channel, False)
|
|
|
|
|
|
|
|
if isinstance(bt, DigitalInputTerminal):
|
|
|
|
for channel in range(1, bt.parameters.get('input_bit_width', 0) + 1):
|
|
|
|
assert bt.read_input(channel) is None
|
|
|
|
|
|
|
|
|
2025-02-17 08:31:28 +00:00
|
|
|
def test_terminal_setup():
|
|
|
|
"""
|
|
|
|
Test if all implemented BusTerminal classes in devices can
|
|
|
|
be instantiated and connected to a bus coupler
|
|
|
|
"""
|
|
|
|
|
|
|
|
terminal_classes: list[type[pyhoff.BusTerminal]] = []
|
|
|
|
for n, o in inspect.getmembers(devices):
|
|
|
|
if inspect.isclass(o) and o not in [DigitalInputTerminal,
|
|
|
|
DigitalOutputTerminal,
|
|
|
|
AnalogInputTerminal,
|
|
|
|
AnalogOutputTerminal]:
|
|
|
|
if issubclass(o, pyhoff.BusTerminal):
|
|
|
|
print(n)
|
|
|
|
terminal_classes.append(o)
|
|
|
|
|
2025-02-19 16:21:23 +00:00
|
|
|
# Beckhoff
|
2025-02-17 08:31:28 +00:00
|
|
|
bus_cupler = devices.BK9050('localhost', 11255, terminal_classes, timeout=0.001)
|
|
|
|
|
|
|
|
assert len(terminal_classes) == len(bus_cupler.bus_terminals)
|
|
|
|
assert bus_cupler.get_error() == 'connection failed', bus_cupler.get_error()
|
2025-02-19 16:21:23 +00:00
|
|
|
rw_all_bus_terminals(bus_cupler)
|
|
|
|
|
|
|
|
# Wago
|
|
|
|
bus_cupler = devices.WAGO_750_352('localhost', 11255, terminal_classes, timeout=0.001)
|
|
|
|
|
|
|
|
assert len(terminal_classes) == len(bus_cupler.bus_terminals)
|
|
|
|
assert bus_cupler.get_error() == 'connection failed', bus_cupler.get_error()
|
|
|
|
rw_all_bus_terminals(bus_cupler)
|