Ejemplo n.º 1
0
    def test_type_resolve(self):
        """Test if PLCTYPE is resolved correctly"""
        with self.plc:

            symbol_str = AdsSymbol(self.plc, "NonExistentVar", 123, 0, "UDINT")
            self.assertEqual(constants.PLCTYPE_UDINT, symbol_str.plc_type)
            self.assertEqual("UDINT", symbol_str.symbol_type)

            symbol_missing = AdsSymbol(self.plc, "NonExistentVar", 123, 0,
                                       "INCORRECT_TYPE")
            self.assertIsNone(symbol_missing.plc_type)

        self.assertAdsRequestsCount(0)  # No requests
Ejemplo n.º 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
Ejemplo n.º 3
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
Ejemplo n.º 4
0
 def test_repr(self):
     """Test debug string"""
     with self.plc:
         symbol = AdsSymbol(self.plc, name=self.test_var.name)
         text = str(symbol)
         self.assertIn(self.test_var.name, text)
         self.assertIn(self.test_var.symbol_type, text)  # Make sure name
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
0
    def test_init_invalid(self):
        """Test symbol creation with missing info"""
        with self.plc:

            with self.assertRaises(ValueError):
                AdsSymbol(
                    self.plc,
                    index_group=self.test_var.index_group,
                    index_offset=self.test_var.index_offset,
                )
Ejemplo n.º 8
0
    def test_type_resolve(self):
        """Test if PLCTYPE is resolved correctly"""
        with self.plc:

            symbol_const = AdsSymbol(self.plc, "NonExistentVar", 123, 0,
                                     pyads.PLCTYPE_UDINT)
            self.assertEqual(constants.PLCTYPE_UDINT, symbol_const.plc_type)
            self.assertNotIsInstance(symbol_const.symbol_type,
                                     str)  # symbol_type
            # can't a neat human-readable string now

            symbol_str = AdsSymbol(self.plc, "NonExistentVar", 123, 0, "UDINT")
            self.assertEqual(constants.PLCTYPE_UDINT, symbol_str.plc_type)
            self.assertEqual("UDINT", symbol_str.symbol_type)

            symbol_missing = AdsSymbol(self.plc, "NonExistentVar", 123, 0,
                                       "INCORRECT_TYPE")
            self.assertIsNone(symbol_missing.plc_type)

        self.assertAdsRequestsCount(0)  # No requests
Ejemplo n.º 9
0
    def test_init_by_name(self):
        """Test symbol creation by name"""
        with self.plc:
            symbol = AdsSymbol(self.plc, name=self.test_var.name)

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

        self.assertAdsRequestsCount(1)  # Only a single READWRITE must have
Ejemplo n.º 10
0
    def test_write(self):
        """Test symbol value writing"""
        with self.plc:

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

            symbol.write(3.14)  # Write

            r_value = self.plc.read(
                self.test_var.index_group,
                self.test_var.index_offset,
                self.test_var.plc_type,
            )

            self.assertEqual(3.14, r_value)

        self.assertAdsRequestsCount(3)  # READWRITE for info, WRITE and
Ejemplo n.º 11
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
Ejemplo n.º 12
0
    def test_init_missing_datatype(self):
        """Test symbol creation when integer datatype is missing"""

        # Modify variable type
        self.test_var.ads_type = 0
        self.test_var.plc_type = constants.PLCTYPE_SINT
        self.test_var.symbol_type = "SINT"
        # Variable is reference to database entry, so no saving required

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

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

        self.assertAdsRequestsCount(1)  # Only a single READWRITE must have
Ejemplo n.º 13
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))
Ejemplo n.º 14
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,
Ejemplo n.º 15
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
Ejemplo n.º 16
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