def __init__(self, protocol): """Init a Protocol object. protocol fields: - name: textual identifier (eg: enip). - mode: int coding mode eg: 1 = modbus asynch TCP. - server: dict containing server settings, empty if mode = 0. extra fields: - minicps_path: full (Linux) path, including ending backslash :protocol: validated dict passed from Device obj """ # TODO: add client dictionary self._name = protocol['name'] self._mode = protocol['mode'] try: from minicps import __file__ index = __file__.rfind('minicps') self._minicps_path = __file__[:index + 7] + '/' except Exception as error: print('ERROR Protocol __init__ set _minicps_path: ', error) if self._mode > 0: # TODO: update server dict field: log self._server = protocol['server'] else: self._server = {}
def __init__(self, protocol): """Init a Protocol object. protocol fields: - name: textual identifier (eg: enip). - mode: int coding mode eg: 1 = modbus asynch TCP. - server: dict containing server settings, empty if mode = 0. extra fields: - minicps_path: full (Linux) path, including ending backslash :protocol: validated dict passed from Device obj """ # TODO: add client dictionary self._name = protocol['name'] self._mode = protocol['mode'] try: from minicps import __file__ index = __file__.rfind('minicps') self._minicps_path = __file__[:index+7] + '/' except Exception as error: print 'ERROR Protocol __init__ set _minicps_path: ', error if self._mode > 0: # TODO: update server dict field: log self._server = protocol['server'] else: self._server = {}
class TestModbusProtocol(): # NOTE: current API specifies only the number of tags TAGS = (200, 200, 200, 200) # TAGS = ( # ('CO1', 1, 'CO'), # ('CO1', 2, 'CO'), # ('DI1', 1, 'DI'), # ('DI1', 2, 'DI'), # ('HR1', 1, 'HR'), # ('HR2', 2, 'HR'), # ('IR1', 1, 'IR'), # ('IR2', 4, 'IR')) SERVER = { 'address': 'localhost:3502', 'tags': TAGS } CLIENT_SERVER_PROTOCOL = { 'name': 'modbus', 'mode': 1, 'server': SERVER, } CLIENT_PROTOCOL = { 'name': 'modbus', 'mode': 0, 'server': '', } if sys.platform.startswith('linux'): SHELL = '/bin/bash -c ' CLIENT_LOG = '--log logs/protocols_tests_modbus_client ' else: raise OSError from minicps import __file__ index = __file__.rfind('minicps') minicps_path = __file__[:index+7] + '/' SERVER_CMD_PATH = sys.executable + ' ' + minicps_path + \ 'pymodbus/servers.py ' # NOTE: ending whitespace def test_server_start_stop(self): try: server = ModbusProtocol._start_server(self.SERVER_CMD_PATH, 'localhost:3502', self.TAGS) ModbusProtocol._stop_server(server) except Exception as error: print 'ERROR test_server_start_stop: ', error assert False def test_init_client(self): try: client = ModbusProtocol( protocol=TestModbusProtocol.CLIENT_PROTOCOL) eq_(client._name, 'modbus') del client except Exception as error: print 'ERROR test_init_client: ', error assert False def test_init_server(self): try: server = ModbusProtocol( protocol=TestModbusProtocol.CLIENT_SERVER_PROTOCOL) eq_(server._name, 'modbus') server._stop_server(server._server_subprocess) del server except Exception as error: print 'ERROR test_init_server: ', error assert False def test_send(self): client = ModbusProtocol( protocol=TestModbusProtocol.CLIENT_PROTOCOL) ADDRESS = 'localhost:3502' TAGS = (20, 20, 20, 20) OFFSET = 10 try: server = ModbusProtocol._start_server(self.SERVER_CMD_PATH, ADDRESS, TAGS) time.sleep(1.0) print('TEST: Write to holding registers') for offset in range(0, OFFSET): what = ('HR', offset) client._send(what, offset, ADDRESS) print('') coil = True print('TEST: Write to coils') for offset in range(0, OFFSET): what = ('CO', offset) client._send(what, coil, ADDRESS) coil = not coil print('') ModbusProtocol._stop_server(server) except Exception as error: ModbusProtocol._stop_server(server) print 'ERROR test_send: ', error assert False def test_receive(self): client = ModbusProtocol( protocol=TestModbusProtocol.CLIENT_PROTOCOL) ADDRESS = 'localhost:3502' TAGS = (20, 20, 20, 20) OFFSET = 10 try: server = ModbusProtocol._start_server(self.SERVER_CMD_PATH, ADDRESS, TAGS) time.sleep(1.0) print('TEST: Read holding registers') for offset in range(0, OFFSET): what = ('HR', offset) eq_(client._receive(what, ADDRESS), 0) print('') print('TEST: Read input registers') for offset in range(0, OFFSET): what = ('IR', offset) eq_(client._receive(what, ADDRESS), 0) print('') print('TEST: Read discrete inputs') for offset in range(0, OFFSET): what = ('DI', offset) eq_(client._receive(what, ADDRESS), False) print('') print('TEST: Read coils inputs') for offset in range(0, OFFSET): what = ('CO', offset) eq_(client._receive(what, ADDRESS), False) print('') ModbusProtocol._stop_server(server) except Exception as error: ModbusProtocol._stop_server(server) print 'ERROR test_receive: ', error assert False @SkipTest def test_client_server(self): ADDRESS = 'localhost:3502' try: # NOTE: same instance used as server and client modbus = ModbusProtocol( protocol=TestModbusProtocol.CLIENT_SERVER_PROTOCOL) time.sleep(1.0) print('TEST: Write and read coils') what = ('CO', 0) value = True modbus._send(what, value, ADDRESS) what = ('CO', 0) eq_(modbus._receive(what, ADDRESS), True) what = ('CO', 1) value = False modbus._send(what, value, ADDRESS) what = ('CO', 1) eq_(modbus._receive(what, ADDRESS), False) print('') print('TEST: Write and read holding registers') for hr in range(10): what = ('HR', hr) modbus._send(what, hr, ADDRESS) what = ('HR', hr) eq_(modbus._receive(what, ADDRESS), hr) print('') print('TEST: Read discrete inputs (init to False)') what = ('DI', 0) eq_(modbus._receive(what, ADDRESS), False) print('') print('TEST: Read input registers (init to 0)') for ir in range(10): what = ('IR', ir) eq_(modbus._receive(what, ADDRESS), 0) print('') ModbusProtocol._stop_server(modbus._server_subprocess) except Exception as error: ModbusProtocol._stop_server(modbus._server_subprocess) print 'ERROR test_client_server: ', error assert False def test_receive_count(self): client = ModbusProtocol( protocol=TestModbusProtocol.CLIENT_PROTOCOL) ADDRESS = 'localhost:3502' TAGS = (20, 20, 20, 20) try: server = ModbusProtocol._start_server(self.SERVER_CMD_PATH, ADDRESS, TAGS) time.sleep(1.0) print('TEST: Read holding registers, count=3') what = ('HR', 0) eq_(client._receive(what, ADDRESS, count=3), [0, 0, 0]) print('') print('TEST: Read input registers, count=1') what = ('IR', 0) eq_(client._receive(what, ADDRESS, count=1), 0) print('') print('TEST: Read discrete inputs, count=2') what = ('DI', 0) eq_(client._receive(what, ADDRESS, count=2), [False] * 2) print('') print('TEST: Read coils, count=9') what = ('CO', 0) eq_(client._receive(what, ADDRESS, count=9), [False] * 9) print('') ModbusProtocol._stop_server(server) except Exception as error: ModbusProtocol._stop_server(server) print 'ERROR test_receive_count: ', error assert False def test_client_server_count(self): client = ModbusProtocol( protocol=TestModbusProtocol.CLIENT_PROTOCOL) ADDRESS = 'localhost:3502' TAGS = (50, 50, 50, 50) try: server = ModbusProtocol._start_server(self.SERVER_CMD_PATH, ADDRESS, TAGS) time.sleep(1.0) print('TEST: Write and Read holding registers, offset=4, count=3') what = ('HR', 4) hrs = [1, 2, 3] client._send(what, hrs, ADDRESS, count=3) eq_(client._receive(what, ADDRESS, count=3), hrs) print('') print('TEST: Write and Read holding registers, offset=4, count=3') what = ('CO', 7) cos = [True, False, False, True, False] client._send(what, cos, ADDRESS, count=5) eq_(client._receive(what, ADDRESS, count=5), cos) print('') ModbusProtocol._stop_server(server) except Exception as error: ModbusProtocol._stop_server(server) print 'ERROR test_client_server_count: ', error assert False