示例#1
0
    def test_write_structure_array(self):
        """Test symbol value reading with structures."""
        structure_def = (("a", pyads.PLCTYPE_INT, 1), ("b", pyads.PLCTYPE_INT,
                                                       1),
                         ("s", pyads.PLCTYPE_STRING, 1))
        values = [{"a": 1, "b": 2, "s": "foo"}, {"a": 3, "b": 4, "s": "bar"}]
        data = bytes(bytes_from_dict(values, structure_def))

        self.handler.add_variable(
            PLCVariable("TestStructure",
                        data,
                        constants.ADST_VOID,
                        symbol_type="TestStructure"))

        write_values = [{
            "a": 42,
            "b": 43,
            "s": "hello"
        }, {
            "a": 44,
            "b": 45,
            "s": "world"
        }]
        with self.plc:
            symbol = self.plc.get_symbol("TestStructure",
                                         structure_def=structure_def,
                                         array_size=2)
            symbol.write(write_values)
            read_values = symbol.read()

        self.assertEqual(write_values, read_values)
示例#2
0
    def test_read_structure(self):
        """Test symbol value reading with structures."""
        structure_def = (
            ("i", pyads.PLCTYPE_INT, 1),
            ("s", pyads.PLCTYPE_STRING, 1),
        )
        values = {"i": 1, "s": "foo"}
        data = bytes(bytes_from_dict(values, structure_def))

        self.handler.add_variable(
            PLCVariable("TestStructure",
                        data,
                        constants.ADST_VOID,
                        symbol_type="TestStructure"))

        with self.plc:
            symbol = self.plc.get_symbol("TestStructure",
                                         structure_def=structure_def)
            read_values = symbol.read()

        self.assertEqual(values, read_values)
示例#3
0
    def test_bytes_from_dict(self):
        # type: () -> None
        """Test bytes_from_dict function"""
        # tests for known values
        structure_def = (
            ("rVar", pyads.PLCTYPE_LREAL, 1),
            ("sVar", pyads.PLCTYPE_STRING, 2, 35),
            ("rVar1", pyads.PLCTYPE_REAL, 4),
            ("iVar", pyads.PLCTYPE_DINT, 5),
            ("iVar1", pyads.PLCTYPE_INT, 3),
            ("ivar2", pyads.PLCTYPE_UDINT, 6),
            ("iVar3", pyads.PLCTYPE_UINT, 7),
            ("iVar4", pyads.PLCTYPE_BYTE, 1),
            ("iVar5", pyads.PLCTYPE_SINT, 1),
            ("iVar6", pyads.PLCTYPE_USINT, 1),
            ("bVar", pyads.PLCTYPE_BOOL, 4),
            ("iVar7", pyads.PLCTYPE_WORD, 1),
            ("iVar8", pyads.PLCTYPE_DWORD, 1),
        )
        values = OrderedDict([
            ("rVar", 1.11),
            ("sVar", ["Hello", "World"]),
            ("rVar1", [2.25, 2.25, 2.5, 2.75]),
            ("iVar", [3, 4, 5, 6, 7]),
            ("iVar1", [8, 9, 10]),
            ("ivar2", [11, 12, 13, 14, 15, 16]),
            ("iVar3", [17, 18, 19, 20, 21, 22, 23]),
            ("iVar4", 24),
            ("iVar5", 25),
            ("iVar6", 26),
            ("bVar", [True, False, True, False]),
            ("iVar7", 27),
            ("iVar8", 28),
        ])
        # fmt: off
        bytes_list = [
            195, 245, 40, 92, 143, 194, 241, 63, 72, 101, 108, 108, 111, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 87, 111, 114, 108, 100, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 16, 64, 0, 0, 16, 64, 0, 0, 32, 64, 0, 0, 48, 64, 3, 0,
            0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 9, 0,
            10, 0, 11, 0, 0, 0, 12, 0, 0, 0, 13, 0, 0, 0, 14, 0, 0, 0, 15, 0,
            0, 0, 16, 0, 0, 0, 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0,
            24, 25, 26, 1, 0, 1, 0, 27, 0, 28, 0, 0, 0
        ]
        # fmt: on
        self.assertEqual(bytes_list,
                         pyads.bytes_from_dict(values, structure_def))

        values = OrderedDict([
            ("rVar", 780245.5678),
            ("sVar", ["TwinCat works", "with Python using pyads"]),
            ("rVar1", [65.5, 89.75, 999.5, 55555.0]),
            ("iVar", [24567, -5678988, 12, -393, 0]),
            ("iVar1", [-20563, 32765, -1]),
            ("ivar2", [100001, 1234567890, 76, 582, 94034536, 2167]),
            ("iVar3", [2167, 987, 63000, 5648, 678, 2734, 43768]),
            ("iVar4", 200),
            ("iVar5", 127),
            ("iVar6", 255),
            ("bVar", [True, False, True, False]),
            ("iVar7", 45367),
            ("iVar8", 256000000),
        ])
        # fmt: off
        bytes_list = [
            125, 174, 182, 34, 171, 207, 39, 65, 84, 119, 105, 110, 67, 97,
            116, 32, 119, 111, 114, 107, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 119, 105, 116, 104, 32, 80,
            121, 116, 104, 111, 110, 32, 117, 115, 105, 110, 103, 32, 112, 121,
            97, 100, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 131, 66,
            0, 128, 179, 66, 0, 224, 121, 68, 0, 3, 89, 71, 247, 95, 0, 0, 116,
            88, 169, 255, 12, 0, 0, 0, 119, 254, 255, 255, 0, 0, 0, 0, 173,
            175, 253, 127, 255, 255, 161, 134, 1, 0, 210, 2, 150, 73, 76, 0, 0,
            0, 70, 2, 0, 0, 104, 218, 154, 5, 119, 8, 0, 0, 119, 8, 219, 3, 24,
            246, 16, 22, 166, 2, 174, 10, 248, 170, 200, 127, 255, 1, 0, 1, 0,
            55, 177, 0, 64, 66, 15
        ]
        # fmt: on
        self.assertEqual(bytes_list,
                         pyads.bytes_from_dict(values, structure_def))

        # test for PLC_DEFAULT_STRING_SIZE
        structure_def = (
            ("iVar", pyads.PLCTYPE_INT, 1),
            ("bVar", pyads.PLCTYPE_BOOL, 1),
            ("sVar", pyads.PLCTYPE_STRING, 1),
            ("iVar2", pyads.PLCTYPE_DINT, 1),
        )
        values = OrderedDict([
            ("iVar", 32767),
            ("bVar", True),
            ("sVar", "Testing the default string size of 80"),
            ("iVar2", -25600000),
        ])
        # fmt: off
        bytes_list = [
            255, 127, 1, 84, 101, 115, 116, 105, 110, 103, 32, 116, 104, 101,
            32, 100, 101, 102, 97, 117, 108, 116, 32, 115, 116, 114, 105, 110,
            103, 32, 115, 105, 122, 101, 32, 111, 102, 32, 56, 48, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 121,
            254
        ]
        # fmt: on
        self.assertEqual(bytes_list,
                         pyads.bytes_from_dict(values, structure_def))

        # test another correct definition with array of structure
        values_list = [
            OrderedDict([
                ("iVar", 32767),
                ("bVar", True),
                ("sVar", "Testing the default string size of 80"),
                ("iVar2", -25600000),
            ]),
            OrderedDict([
                ("iVar", -32768),
                ("bVar", True),
                ("sVar", "Another Test using the default string size of 80"),
                ("iVar2", -25600000),
            ]),
            OrderedDict([
                ("iVar", 0),
                ("bVar", False),
                ("sVar", "Last Test String of Array"),
                ("iVar2", 1234567890),
            ]),
        ]
        # fmt: off
        bytes_list = [
            255, 127, 1, 84, 101, 115, 116, 105, 110, 103, 32, 116, 104, 101,
            32, 100, 101, 102, 97, 117, 108, 116, 32, 115, 116, 114, 105, 110,
            103, 32, 115, 105, 122, 101, 32, 111, 102, 32, 56, 48, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 121,
            254, 0, 128, 1, 65, 110, 111, 116, 104, 101, 114, 32, 84, 101, 115,
            116, 32, 117, 115, 105, 110, 103, 32, 116, 104, 101, 32, 100, 101,
            102, 97, 117, 108, 116, 32, 115, 116, 114, 105, 110, 103, 32, 115,
            105, 122, 101, 32, 111, 102, 32, 56, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 96, 121, 254, 0, 0, 0, 76, 97, 115, 116, 32, 84, 101, 115,
            116, 32, 83, 116, 114, 105, 110, 103, 32, 111, 102, 32, 65, 114,
            114, 97, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 210, 2, 150, 73
        ]
        # fmt: on
        self.assertEqual(
            bytes_list,
            pyads.bytes_from_dict(values_list, structure_def, array_size=3))

        # test for not default string and array of LREALs
        structure_def = (
            ("sVar", pyads.PLCTYPE_STRING, 1, 20),
            ("rVar", pyads.PLCTYPE_LREAL, 4),
        )
        values = OrderedDict([("sVar", "pyads"),
                              ("rVar", [1.11, 2.22, 3.33, 4.44])])
        # fmt: off
        bytes_list = [
            112, 121, 97, 100, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 195, 245, 40, 92, 143, 194, 241, 63, 195, 245, 40, 92, 143,
            194, 1, 64, 164, 112, 61, 10, 215, 163, 10, 64, 195, 245, 40, 92,
            143, 194, 17, 64
        ]
        # fmt: on
        self.assertEqual(bytes_list,
                         pyads.bytes_from_dict(values, structure_def))

        # tests for incorrect definitions
        values = OrderedDict([("sVar", "hi"), ("rVar", 12.3), ("iVar", 5)])
        structure_def = (
            ("sVar", pyads.PLCTYPE_STRING, 1),
            ("rVar", 1, 1),
            ("iVar", pyads.PLCTYPE_DINT, 1),
        )
        with self.assertRaises(RuntimeError):
            pyads.bytes_from_dict(values, structure_def)

        structure_def = (
            ("sVar", pyads.PLCTYPE_STRING, 1),
            (pyads.PLCTYPE_REAL, 1),
            ("iVar", pyads.PLCTYPE_DINT, 1),
        )
        with self.assertRaises(ValueError):
            pyads.bytes_from_dict(values, structure_def)

        structure_def = (
            ("sVar", pyads.PLCTYPE_STRING, 1),
            ("rVar", pyads.PLCTYPE_REAL, ""),
            ("iVar", pyads.PLCTYPE_DINT, 1),
            ("iVar1", pyads.PLCTYPE_INT, 3),
        )
        with self.assertRaises(TypeError):
            pyads.bytes_from_dict(values, structure_def)

        # test for incorrect dict
        with self.assertRaises(KeyError):
            pyads.bytes_from_dict(OrderedDict(), structure_def)