Beispiel #1
0
    def test_read_structure_by_name(self):
        # type: () -> None
        """Test read by structure method"""
        # TODO may need testserver.py changes to increase test usefulness

        handle_name = "TestHandle"

        structure_def = (("xVar", pyads.PLCTYPE_BYTE, 1),)

        # test with no structure size passed in
        with self.plc:
            read_value = self.plc.read_structure_by_name(handle_name, structure_def)

        # Retrieve list of received requests from server
        requests = self.test_server.request_history

        # Assert that the server received 3 requests
        self.assertEqual(len(requests), 3)

        # Assert that Read/Write command was used to get the handle by name
        self.assert_command_id(requests[0], constants.ADSCOMMAND_READWRITE)
        # Assert that the server received the handle by name
        received_value = requests[0].ams_header.data[16:]
        sent_value = (handle_name + "\x00").encode("utf-8")
        self.assertEqual(sent_value, received_value)

        # Assert that next, the Read command was used to get the value
        self.assert_command_id(requests[1], constants.ADSCOMMAND_READ)

        # Assert that Write was used to release the handle
        self.assert_command_id(requests[2], constants.ADSCOMMAND_WRITE)

        # Check read value returned by server:
        # Test server just returns repeated bytes of 0x0F terminated with 0x00
        # But because the read value is only 1-byte long, we just get 0x00
        expected_result = OrderedDict([("xVar", 0)])
        self.assertEqual(read_value, expected_result)

        # Test with structure size passed in
        structure_size = pyads.size_of_structure(structure_def)
        with self.plc:
            read_value = self.plc.read_structure_by_name(
                handle_name, structure_def, structure_size=structure_size
            )
        self.assertEqual(read_value, expected_result)

        # Test with handle passed in
        with self.plc:
            handle = self.plc.get_handle(handle_name)
            read_value = self.plc.read_structure_by_name(
                "", structure_def, handle=handle
            )
        self.assertEqual(read_value, expected_result)
        with self.plc:
            self.plc.release_handle(handle)
Beispiel #2
0
    def test_write_structure_by_name(self):
        # type: () -> None
        """Test write by structure method"""

        handle_name = "TestHandle"
        struct_to_write = OrderedDict([("sVar", "Test Value")])
        value = "Test Value"

        structure_def = (("sVar", pyads.PLCTYPE_STRING, 1),)

        # test with no structure size passed in
        with self.plc:
            self.plc.write_structure_by_name(handle_name, struct_to_write, structure_def)

        # Retrieve list of received requests from server
        requests = self.test_server.request_history

        # Assert that the server received 3 requests
        self.assertEqual(len(requests), 3)

        # Assert that Read/Write command was used to get the handle by name
        self.assert_command_id(requests[0], constants.ADSCOMMAND_READWRITE)

        # Assert that Write command was used to write the value
        self.assert_command_id(requests[1], constants.ADSCOMMAND_WRITE)
        # Check the value written matches our value
        received_value = requests[1].ams_header.data[12:].decode("utf-8").rstrip("\x00")
        self.assertEqual(value, received_value)

        # Assert that Write was used to release the handle
        self.assert_command_id(requests[2], constants.ADSCOMMAND_WRITE)

        # Test with structure size passed in
        structure_size = pyads.size_of_structure(structure_def)
        with self.plc:
            self.plc.write_structure_by_name(
                handle_name, struct_to_write, structure_def, structure_size=structure_size
            )

        requests = self.test_server.request_history
        received_value = requests[1].ams_header.data[12:].decode("utf-8").rstrip("\x00")
        self.assertEqual(value, received_value)

        # Test with handle passed in
        with self.plc:
            handle = self.plc.get_handle(handle_name)
            self.plc.write_structure_by_name("", struct_to_write, structure_def, handle=handle)

        requests = self.test_server.request_history
        received_value = requests[1].ams_header.data[12:].decode("utf-8").rstrip("\x00")
        self.assertEqual(value, received_value)
        with self.plc:
            self.plc.release_handle(handle)
Beispiel #3
0
    def test_size_of_structure(self):
        # type: () -> None
        """Test size_of_structure function"""
        # known structure size with defined string
        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),
        )
        self.assertEqual(pyads.size_of_structure(structure_def), 173)

        # test for PLC_DEFAULT_STRING_SIZE
        structure_def = (("sVar", pyads.PLCTYPE_STRING, 4), )
        self.assertEqual(
            pyads.size_of_structure(structure_def),
            (pyads.PLC_DEFAULT_STRING_SIZE + 1) * 4,
        )

        # tests for incorrect definitions
        structure_def = (
            ("sVar", pyads.PLCTYPE_STRING, 4),
            ("rVar", 1, 1),
            ("iVar", pyads.PLCTYPE_DINT, 1),
        )
        with self.assertRaises(RuntimeError):
            pyads.size_of_structure(structure_def)

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

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

        # test another correct definition with array of structure
        structure_def = (
            ("bVar", pyads.PLCTYPE_BOOL, 1),
            ("rVar", pyads.PLCTYPE_LREAL, 3),
            ("sVar", pyads.PLCTYPE_STRING, 2),
            ("iVar", pyads.PLCTYPE_DINT, 10),
            ("iVar1", pyads.PLCTYPE_INT, 3),
            ("bVar1", pyads.PLCTYPE_BOOL, 4),
        )
        self.assertEqual(pyads.size_of_structure(structure_def * 5), 1185)