def test_code_ascii(self):

        # _estimate_length_request

        tests = [
            {
                'src': b':01010001000A66\r\n',
                'exp': 6
            },
        ]

        obj = ModbusTransaction()

        for test in tests:
            # loop through all tests

            if test['exp'] == ValueError:
                with self.assertRaises(ValueError):
                    obj._parse_rtu(test['src'])

                pass
            else:
                obj._parse_rtu(test['src'])
                self.assertEqual(result, test['exp'])
                self.assertEqual(obj['cooked_protocol'], obj.IA_PROTOCOL_MBRTU)

        return
Ejemplo n.º 2
0
    def test_request(self):

        tests = [
            {
                'src': b'\x01\x03\x00\x00\x00\x0A\xC5\xCD',
                'raw': b'\x01\x03\x00\x00\x00\x0A',
                'exp': b'\x01\x03\x00\x00\x00\x0A\xC5\xCD'
            },
        ]

        obj = ModbusTransaction()

        for test in tests:
            # loop through all tests

            # print("test:{}".format(test))

            # this won't have the UID in first place - moved to [KEY_SRC_ID]
            msg = test['raw'][1:]

            result = obj.set_request(test['src'], ia_trans.IA_PROTOCOL_MBRTU)
            # print("obj pst:{}".format(obj.attrib))
            self.assertTrue(result)
            self.assertEqual(obj[obj.KEY_REQ_RAW], test['src'])
            self.assertEqual(obj[obj.KEY_REQ_PROTOCOL],
                             ia_trans.IA_PROTOCOL_MBRTU)
            self.assertNotIn(obj.KEY_SRC_ID, obj.attrib)
            self.assertEqual(obj[obj.KEY_REQ], msg)

            result = obj.get_request()
            self.assertEqual(result, test['exp'])

        return
Ejemplo n.º 3
0
    def __init__(self):

        # thread-safe event to halt execution
        self.running = Event()
        self.running.set()

        # various server settings
        self.host_ip = self.DEF_HOST_IP
        self.host_port = self.DEF_HOST_PORT
        self.host_protocol = self.DEF_HOST_PROTOCOL

        # used to abort an idle client connection
        self.idle_timeout = self.DEF_IDLE_TIMEOUT
        self.last_activity = time.time()

        self.serial_name = self.DEF_SERIAL_PORT
        self.serial_baud = self.DEF_SERIAL_BAUD
        self.serial_parity = self.DEF_SERIAL_PARITY
        self.serial_protocol = self.DEF_SERIAL_PROTOCOL
        self.ser = None

        self.logger = None

        # hold our server socket
        self.server = None
        self._rd_ready = None

        # this is our object to hold, parse, and transmute packets
        self.modbus = ModbusTransaction()

        return
Ejemplo n.º 4
0
    def test_all_request(self):

        tests = [
            {
                'src': b'\xAB\xCD\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A',
                'raw': b'\x01\x03\x00\x00\x00\x0A',
                'asc': b':01030000000AF2\r\n',
                'rtu': b'\x01\x03\x00\x00\x00\x0A\xC5\xCD',
                'tcp': b'\xAB\xCD\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
            },
        ]

        obj = ModbusTransaction()

        for test in tests:
            # loop through all tests

            # print("test:{}".format(test))

            result = obj.set_request(test['src'], ia_trans.IA_PROTOCOL_MBTCP)
            self.assertEqual(obj[obj.KEY_REQ_PROTOCOL],
                             ia_trans.IA_PROTOCOL_MBTCP)

            # default will be TCP
            result = obj.get_request()
            self.assertEqual(result, test['tcp'])

            # confirm we can pull back as any of the three
            result = obj.get_request(ia_trans.IA_PROTOCOL_MBASC)
            self.assertEqual(result, test['asc'])

            result = obj.get_request(ia_trans.IA_PROTOCOL_MBRTU)
            self.assertEqual(result, test['rtu'])

            result = obj.get_request(ia_trans.IA_PROTOCOL_MBTCP)
            self.assertEqual(result, test['tcp'])

        return
    def test_estimate_length_request(self):

        # _estimate_length_request

        tests = [
            {
                'src': b'\x01\x01\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x01\x00\x01\x00\x0A\x44\x2A',
                'exp': 6
            },
            {
                'src': b'\x01\x02\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x03\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x04\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x05\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x06\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x0F\x02\x00\x01',
                'exp': 5
            },
            {
                'src': b'\x01\x10\x02\x00\x01',
                'exp': 5
            },
            {
                'src': '\x01\x01\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x00\x02\x00\x01',
                'exp': ValueError
            },
        ]

        obj = ModbusTransaction()

        for test in tests:
            # loop through all tests

            if test['exp'] == ValueError:
                with self.assertRaises(ValueError):
                    obj._estimate_length_request(test['src'])

                pass
            else:
                result = obj._estimate_length_request(test['src'])
                self.assertEqual(result, test['exp'])

        return
    def test_parse_rtu(self):

        # _estimate_length_request

        tests = [
            {
                'src': b'\x01\x01\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x01\x00\x01\x00\x0A\x44\x2A',
                'exp': 6
            },
            {
                'src': b'\x01\x02\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x03\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x04\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x05\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x06\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x0F\x02\x00\x01',
                'exp': 5
            },
            {
                'src': b'\x01\x10\x02\x00\x01',
                'exp': 5
            },
            {
                'src': '\x01\x01\x00\x01\x00\x0A',
                'exp': 6
            },
            {
                'src': b'\x01\x00\x02\x00\x01',
                'exp': ValueError
            },
        ]

        obj = ModbusTransaction()

        for test in tests:
            # loop through all tests

            if test['exp'] == ValueError:
                with self.assertRaises(ValueError):
                    obj._parse_rtu(test['src'])

                pass
            else:
                obj._parse_rtu(test['src'])
                # self.assertEqual(result, test['exp'])
                self.assertEqual(obj['cooked_protocol'], obj.IA_PROTOCOL_MBRTU)

        return
Ejemplo n.º 7
0
    def test_sequence_number(self):

        tests = [
            {
                'src': b'\xAB\xCD\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A',
                'seq': b'\xAB\xCD',
                'raw': b'\x01\x03\x00\x00\x00\x0A'
            },
        ]

        obj = ModbusTransaction()

        source = b'\xAB\xCD\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
        expect = source
        obj.set_request(source, ia_trans.IA_PROTOCOL_MBTCP)
        result = obj.get_request()
        self.assertEqual(result, expect)

        # swap in a new 2-byte sequence
        obj[obj.KEY_SRC_SEQ] = b'\xFF\xEE'
        expect = b'\xFF\xEE\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
        result = obj.get_request()
        self.assertEqual(result, expect)

        # swap in a new 1-byte sequence - expect padding with 0x01
        obj[obj.KEY_SRC_SEQ] = b'\x44'
        expect = b'\x44\x01\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
        result = obj.get_request()
        self.assertEqual(result, expect)

        # swap in a new n byte sequence - expect padding with 0x01
        obj[obj.KEY_SRC_SEQ] = b''
        expect = b'\x01\x01\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
        result = obj.get_request()
        self.assertEqual(result, expect)

        # swap in a new 3 byte sequence - expect truncate to 2 bytes
        obj[obj.KEY_SRC_SEQ] = b'\x05\x06\x07'
        expect = b'\x05\x06\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
        result = obj.get_request()
        self.assertEqual(result, expect)

        # swap in a new int sequence
        obj[obj.KEY_SRC_SEQ] = 0
        expect = b'\x00\x00\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
        result = obj.get_request()
        self.assertEqual(result, expect)

        obj[obj.KEY_SRC_SEQ] = 0x01
        expect = b'\x00\x01\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
        result = obj.get_request()
        self.assertEqual(result, expect)

        # swap in a new int sequence - saved as big-endian
        obj[obj.KEY_SRC_SEQ] = 0x0306
        expect = b'\x03\x06\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
        result = obj.get_request()
        self.assertEqual(result, expect)

        # swap in a new int sequence - excess is truncated
        obj[obj.KEY_SRC_SEQ] = 0xFF0306
        expect = b'\x03\x06\x00\x00\x00\x06\x01\x03\x00\x00\x00\x0A'
        result = obj.get_request()
        self.assertEqual(result, expect)

        return