Beispiel #1
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
Beispiel #2
0
    def test_init_manual(self):
        """Test symbol without lookup"""
        with self.plc:

            # Create symbol while providing everything:
            symbol = AdsSymbol(
                self.plc,
                name=self.test_var.name,
                index_group=self.test_var.index_group,
                index_offset=self.test_var.index_offset,
                symbol_type=self.test_var.symbol_type,
            )

            self.assertAdsRequestsCount(0)  # No requests yet

            self.plc.write(
                self.test_var.index_group,
                self.test_var.index_offset,
                12.3,
                self.test_var.plc_type,
            )

            self.assertEqual(12.3, symbol.read())

        self.assertAdsRequestsCount(2)  # Only a WRITE followed by a READ
Beispiel #3
0
    def test_read_write_errors(self):
        """Test read/write on invalid AdsSymbol

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

        symbol = AdsSymbol(self.plc, "MySymbol", 123, 0, "BYTE")

        with self.assertRaises(ValueError) as cm:
            symbol.read()  # Cannot read with unopened Connection
        self.assertIn("missing or closed Connection", str(cm.exception))

        self.plc.open()

        symbol.index_offset = None  # Set index to something invalid

        # with self.assertRaises(ValueError) as cm:
        #     symbol.read()  # Cannot read with invalid index
        # self.assertIn('invalid values for', str(cm.exception))

        with self.assertRaises(TypeError) as cm:
            symbol.read()  # Catch error inside pyads_ex
        self.assertIn("integer is required", str(cm.exception))

        symbol.index_group = None

        with self.assertRaises(TypeError) as cm:
            symbol.read()  # Catch error inside pyads_ex
        self.assertIn("integer is required", str(cm.exception))

        symbol.index_offset = 'A'

        with self.assertRaises(TypeError) as cm:
            symbol.read()  # Catch error inside pyads_ex
        self.assertIn("integer is required", str(cm.exception))

        symbol.index_group = 'B'

        with self.assertRaises(TypeError) as cm:
            symbol.read()  # Catch error inside pyads_ex
        self.assertIn("integer is required", str(cm.exception))
Beispiel #4
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,
Beispiel #5
0
    def test_value(self):
        """Test the buffer property"""

        with self.plc:
            symbol = AdsSymbol(self.plc, name=self.test_var.name)

            symbol.value = 420.0  # Shouldn't change anything yet

            self.assertAdsRequestsCount(1)  # Only a READWRITE for info

            symbol.write()

            self.assertAdsRequestsCount(2)  # Written from buffer

            symbol.read()

            for i in range(10):
                custom_buffer = symbol.value

            self.assertEqual(420.0, symbol.value)

            self.assertAdsRequestsCount(3)  # Read only once
Beispiel #6
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
Beispiel #7
0
    def test_read(self):
        """Test symbol value reading"""

        with self.plc:

            self.plc.write(
                self.test_var.index_group,
                self.test_var.index_offset,
                420.0,
                self.test_var.plc_type,
            )

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

            self.assertEqual(420.0, symbol.read())

        self.assertAdsRequestsCount(3)  # WRITE, READWRITE for info and