Exemple #1
0
    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
Exemple #2
0
    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
Exemple #3
0
    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
Exemple #4
0
    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
Exemple #5
0
    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
Exemple #6
0
    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
Exemple #7
0
    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