From 55f8194261816876f435a5be8e6f685648ecf7ec Mon Sep 17 00:00:00 2001 From: Shan Fur Date: Tue, 7 May 2024 14:58:30 +0200 Subject: [PATCH 01/10] Adaptation of add_device_notification() so that the length of NotificationAttrib is correctly set to _structure_size if a structure is present. --- pyads/symbol.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyads/symbol.py b/pyads/symbol.py index e53bf68..ef54da0 100644 --- a/pyads/symbol.py +++ b/pyads/symbol.py @@ -249,7 +249,10 @@ def add_device_notification( """ if attr is None: - attr = NotificationAttrib(length=sizeof(self.plc_type)) + if self.plc_type is not None: + attr = NotificationAttrib(length=sizeof(self.plc_type)) + elif self.structure_def is not None: + attr = NotificationAttrib(length=self._structure_size) handles = self._plc.add_device_notification( (self.index_group, self.index_offset), attr, callback, user_handle From 074b7fe28d09c9f05acccf0f251d33bd137e7600 Mon Sep 17 00:00:00 2001 From: Shan Fur Date: Tue, 7 May 2024 14:58:30 +0200 Subject: [PATCH 02/10] Adaptation of add_device_notification() so that the length of NotificationAttrib is correctly set to _structure_size if a structure is present. --- pyads/symbol.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyads/symbol.py b/pyads/symbol.py index 762cc99..18590c5 100644 --- a/pyads/symbol.py +++ b/pyads/symbol.py @@ -252,7 +252,10 @@ def add_device_notification( """ if attr is None: - attr = NotificationAttrib(length=sizeof(self.plc_type)) + if self.plc_type is not None: + attr = NotificationAttrib(length=sizeof(self.plc_type)) + elif self.structure_def is not None: + attr = NotificationAttrib(length=self._structure_size) handles = self._plc.add_device_notification( (self.index_group, self.index_offset), attr, callback, user_handle From b6e6faccbadaa5c1c8cfeb636b7a8909390475b9 Mon Sep 17 00:00:00 2001 From: Shan Fur Date: Tue, 8 Oct 2024 13:53:03 +0200 Subject: [PATCH 03/10] Improved implementation of checking if a struct is present. --- pyads/symbol.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyads/symbol.py b/pyads/symbol.py index 18590c5..76da02b 100644 --- a/pyads/symbol.py +++ b/pyads/symbol.py @@ -252,10 +252,10 @@ def add_device_notification( """ if attr is None: - if self.plc_type is not None: - attr = NotificationAttrib(length=sizeof(self.plc_type)) - elif self.structure_def is not None: + if self.structure_def is not None: attr = NotificationAttrib(length=self._structure_size) + else: + attr = NotificationAttrib(length=sizeof(self.plc_type)) handles = self._plc.add_device_notification( (self.index_group, self.index_offset), attr, callback, user_handle From bb00d34a7342f70792e8ff5c99b4ad7c81da4285 Mon Sep 17 00:00:00 2001 From: Shan Fur Date: Tue, 8 Oct 2024 14:26:40 +0200 Subject: [PATCH 04/10] Improved query of whether a structure is present. --- pyads/symbol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyads/symbol.py b/pyads/symbol.py index 76da02b..3ea6566 100644 --- a/pyads/symbol.py +++ b/pyads/symbol.py @@ -252,7 +252,7 @@ def add_device_notification( """ if attr is None: - if self.structure_def is not None: + if self.is_structure: attr = NotificationAttrib(length=self._structure_size) else: attr = NotificationAttrib(length=sizeof(self.plc_type)) From 12d480b2d1bd006cd9a90f3a4e6b4f14bce66ba8 Mon Sep 17 00:00:00 2001 From: Shan Fur Date: Tue, 8 Oct 2024 16:44:19 +0200 Subject: [PATCH 05/10] Add a test for the add_notification() function with structures --- tests/test_symbol.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_symbol.py b/tests/test_symbol.py index d6200a8..51b17a6 100644 --- a/tests/test_symbol.py +++ b/tests/test_symbol.py @@ -491,6 +491,32 @@ def my_callback(*_): self.assertAdsRequestsCount(3) # READWRITE, ADDNOTE and DELNOTE + def test_add_notification_structure(self): + """Test notification registering for 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")) + + def my_callback(*_): + return + + with self.plc: + + symbol = self.plc.get_symbol("TestStructure", structure_def=structure_def, array_size=2) + + handles = symbol.add_device_notification(my_callback) + + symbol.del_device_notification(handles) + + self.assertAdsRequestsCount(3) # READWRITE, ADDNOTE and DELNOTE + def test_add_notification_delete(self): """Test notification registering""" From f486a7204bd4c8342d42b332c377d0d9c0be4d76 Mon Sep 17 00:00:00 2001 From: Rich-Hyena <168060947+Rich-Hyena@users.noreply.github.com> Date: Tue, 15 Oct 2024 13:27:31 +0200 Subject: [PATCH 06/10] Update routing.rst (#429) Small Typo Fix (you plc -> your plc) --- doc/documentation/routing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/documentation/routing.rst b/doc/documentation/routing.rst index ffbce3d..78d12cf 100644 --- a/doc/documentation/routing.rst +++ b/doc/documentation/routing.rst @@ -8,7 +8,7 @@ operating systems in the sections below. To identify each side of a route we will use the terms *client* and *target*. The *client* is your computer where pyads runs on. The -*target* is you plc or remote computer which you want to connect to. +*target* is your plc or remote computer which you want to connect to. Creating routes on Windows -------------------------- From 3e96840bba57a7c835421fcb09dce386bfaed5d0 Mon Sep 17 00:00:00 2001 From: Shan Fur Date: Tue, 7 May 2024 14:58:30 +0200 Subject: [PATCH 07/10] Adaptation of add_device_notification() so that the length of NotificationAttrib is correctly set to _structure_size if a structure is present. --- pyads/symbol.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyads/symbol.py b/pyads/symbol.py index 762cc99..18590c5 100644 --- a/pyads/symbol.py +++ b/pyads/symbol.py @@ -252,7 +252,10 @@ def add_device_notification( """ if attr is None: - attr = NotificationAttrib(length=sizeof(self.plc_type)) + if self.plc_type is not None: + attr = NotificationAttrib(length=sizeof(self.plc_type)) + elif self.structure_def is not None: + attr = NotificationAttrib(length=self._structure_size) handles = self._plc.add_device_notification( (self.index_group, self.index_offset), attr, callback, user_handle From a841b0812140d75154675822a140e6657ae97cb8 Mon Sep 17 00:00:00 2001 From: Shan Fur Date: Tue, 8 Oct 2024 13:53:03 +0200 Subject: [PATCH 08/10] Improved implementation of checking if a struct is present. --- pyads/symbol.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyads/symbol.py b/pyads/symbol.py index 18590c5..76da02b 100644 --- a/pyads/symbol.py +++ b/pyads/symbol.py @@ -252,10 +252,10 @@ def add_device_notification( """ if attr is None: - if self.plc_type is not None: - attr = NotificationAttrib(length=sizeof(self.plc_type)) - elif self.structure_def is not None: + if self.structure_def is not None: attr = NotificationAttrib(length=self._structure_size) + else: + attr = NotificationAttrib(length=sizeof(self.plc_type)) handles = self._plc.add_device_notification( (self.index_group, self.index_offset), attr, callback, user_handle From 5ab1f5831c168b735017b1902296367f995d5c30 Mon Sep 17 00:00:00 2001 From: Shan Fur Date: Tue, 8 Oct 2024 14:26:40 +0200 Subject: [PATCH 09/10] Improved query of whether a structure is present. --- pyads/symbol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyads/symbol.py b/pyads/symbol.py index 76da02b..3ea6566 100644 --- a/pyads/symbol.py +++ b/pyads/symbol.py @@ -252,7 +252,7 @@ def add_device_notification( """ if attr is None: - if self.structure_def is not None: + if self.is_structure: attr = NotificationAttrib(length=self._structure_size) else: attr = NotificationAttrib(length=sizeof(self.plc_type)) From c2ad78ab757d65f7cb6069626b915264339e45bc Mon Sep 17 00:00:00 2001 From: Shan Fur Date: Tue, 8 Oct 2024 16:44:19 +0200 Subject: [PATCH 10/10] Add a test for the add_notification() function with structures --- tests/test_symbol.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_symbol.py b/tests/test_symbol.py index d6200a8..51b17a6 100644 --- a/tests/test_symbol.py +++ b/tests/test_symbol.py @@ -491,6 +491,32 @@ def my_callback(*_): self.assertAdsRequestsCount(3) # READWRITE, ADDNOTE and DELNOTE + def test_add_notification_structure(self): + """Test notification registering for 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")) + + def my_callback(*_): + return + + with self.plc: + + symbol = self.plc.get_symbol("TestStructure", structure_def=structure_def, array_size=2) + + handles = symbol.add_device_notification(my_callback) + + symbol.del_device_notification(handles) + + self.assertAdsRequestsCount(3) # READWRITE, ADDNOTE and DELNOTE + def test_add_notification_delete(self): """Test notification registering"""