示例#1
0
    def test_init_by_name_matrix_style(self):
        """Test symbol creation when it's an array denoted as matrix

        This is how an array originating from Simulink could look like.
        """

        var = PLCVariable(
            "ArrayVar",
            ads_type=0,
            symbol_type="matrix_21_int8_T",  # Simulink array looks like this
        )
        var.plc_type = constants.PLCTYPE_ARR_SINT(21)  # Have to do this
        # manually
        var.index_group = 123
        var.index_offset = 100
        self.handler.add_variable(var)

        self.plc.open()

        symbol = AdsSymbol(
            self.plc,
            name=var.name,
            index_group=var.index_group,
            index_offset=var.index_offset,
            symbol_type=var.symbol_type,
        )  # No lookup

        # Verify looked up info
        self.assertEqual(var.plc_type, symbol.plc_type)
        self.assertEqual(var.symbol_type, symbol.symbol_type)

        self.assertAdsRequestsCount(0)  # No requests
示例#2
0
    def test_init_invalid_type(self):
        """Test symbol lookup when type cannot be found

        There was a read/write check that verifies the plc_typ was not None,
        but this was removed.
        """

        var = PLCVariable(name="UnknownType",
                          ads_type=0,
                          symbol_type="non_existent_type")
        var.index_group = 123
        var.index_offset = 100
        var.plc_type = constants.PLCTYPE_BYTE  # Set to something real

        self.handler.add_variable(var)

        with self.plc:

            # Create symbol while providing everything:
            symbol = AdsSymbol(self.plc, name=var.name)

            self.assertEqual(var.symbol_type, symbol.symbol_type)
            self.assertIsNone(symbol.plc_type)

            # with self.assertRaises(ValueError) as cm:
            #     # Without type specified, it cannot read
            #     symbol.read()
            # self.assertIn('Cannot read or write', str(cm.exception))

            with self.assertRaises(TypeError) as cm:
                # Error is thrown inside pyads_ex
                symbol.read()
            self.assertIn("NoneType", str(cm.exception))

        self.assertAdsRequestsCount(1)  # Only a WRITE followed by a READ
示例#3
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)
示例#4
0
    def test_init_by_name_matrix_style(self):
        """Test symbol creation when it's an array denoted as matrix

        This is how an array originating from Simulink could look like.
        """

        var = PLCVariable(
            "ArrayVar",
            struct.pack("<21b", *range(21)),
            ads_type=constants.ADST_VOID,
            symbol_type="matrix_21_int8_T",  # Simulink array looks like this
            index_group=123,
            index_offset=100,
        )
        self.handler.add_variable(var)
        self.plc.open()
        symbol = AdsSymbol(
            self.plc,
            name=var.name,
            index_group=var.index_group,
            index_offset=var.index_offset,
            symbol_type=var.symbol_type,  # String initialization
        )  # No lookup

        # Verify looked up info
        self.assertEqual(constants.PLCTYPE_ARR_SINT(21), symbol.plc_type)
        self.assertEqual(var.symbol_type, symbol.symbol_type)

        self.assertAdsRequestsCount(0)  # No requests
示例#5
0
    def test_init_by_name_array(self):
        """Test symbol creation when it's an array"""

        var = PLCVariable(
            "ArrayVar",
            ads_type=constants.ADST_INT16,  # dataType does not represent
            # array unfortunately
            symbol_type="ARRAY [1..5] OF INT",  # Array looks like this in PLC
        )
        var.plc_type = constants.PLCTYPE_ARR_INT(5)  # Have to do this
        # manually
        self.handler.add_variable(var)

        self.plc.open()

        symbol = AdsSymbol(self.plc, name=var.name)

        # Verify looked up info
        self.assertEqual(var.name, symbol.name)
        self.assertEqual(var.index_group, symbol.index_group)
        self.assertEqual(var.index_offset, symbol.index_offset)
        self.assertEqual(var.plc_type, symbol.plc_type)
        self.assertEqual(var.symbol_type, symbol.symbol_type)
        self.assertIsNone(symbol.comment)

        my_list = symbol.read()

        self.assertIsInstance(my_list, list)
        self.assertEqual(5, len(my_list))

        my_list[4] = 420

        symbol.write(my_list)  # Modify array

        my_list2 = symbol.read()  # Read again

        self.assertEqual(my_list, my_list2)

        self.assertAdsRequestsCount(4)  # A READWRITE (for info), READ,
示例#6
0
    def test_string(self):
        """Test symbol with a string value"""
        variable = PLCVariable("my_text",
                               bytes(50),
                               ads_type=constants.ADST_STRING,
                               symbol_type="STRING(50)")
        self.handler.add_variable(variable)

        with self.plc:
            symbol = self.plc.get_symbol("my_text")
            symbol.write("I am a string!")
            value = symbol.read()
            self.assertEqual(value, "I am a string!")
示例#7
0
    def setUp(self):
        # type: () -> None
        """Establish connection to the test server."""

        # Clear test server and handler
        self.test_server.request_history = []
        self.handler.reset()

        # Create PLC variable that is added by default
        self.test_var = PLCVariable("TestDouble",
                                    ads_type=constants.ADST_REAL64,
                                    symbol_type="LREAL")
        self.test_var.comment = "Some variable of type double"
        self.handler.add_variable(self.test_var)

        self.plc = pyads.Connection(TEST_SERVER_AMS_NET_ID,
                                    TEST_SERVER_AMS_PORT,
                                    TEST_SERVER_IP_ADDRESS)
示例#8
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)
示例#9
0
    def test_init_invalid_type(self):
        """Test symbol lookup when type cannot be found

        There was a read/write check that verifies the plc_typ was not None,
        but this was removed.
        """

        var = PLCVariable("UnknownType",
                          b"\x00",
                          ads_type=constants.ADST_VOID,
                          symbol_type="non_existent_type",
                          index_group=123,
                          index_offset=100)
        self.handler.add_variable(var)

        with self.plc:
            # Create symbol while providing everything:
            symbol = AdsSymbol(self.plc, name=var.name)
            self.assertEqual(var.symbol_type, symbol.symbol_type)
            with self.assertRaises(TypeError) as cm:
                # Error is thrown inside pyads_ex
                symbol.read()
            self.assertIn("NoneType", str(cm.exception))
        self.assertAdsRequestsCount(1)  # Only a WRITE followed by a READ