This is an automated email from the ASF dual-hosted git repository.

hutcheb pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 74e94bdfd3 fix(plc4py): Fix exception on closing the connection
74e94bdfd3 is described below

commit 74e94bdfd3146c8ab839ef51fc81a6f83497db9a
Author: Ben Hutcheson <ben.hut...@gmail.com>
AuthorDate: Tue Apr 9 04:14:05 2024 +0200

    fix(plc4py): Fix exception on closing the connection
---
 plc4py/plc4py/api/exceptions/exceptions.py         |  4 +--
 plc4py/plc4py/spi/Plc4xBaseProtocol.py             |  3 +-
 .../manual/__init__.py}                            | 20 -----------
 .../exceptions.py => tests/manual/examples.py}     | 40 ++++++++--------------
 .../drivers/modbus/test_modbus_connection.py       | 36 ++++++-------------
 5 files changed, 29 insertions(+), 74 deletions(-)

diff --git a/plc4py/plc4py/api/exceptions/exceptions.py 
b/plc4py/plc4py/api/exceptions/exceptions.py
index b1b2e117fc..fe48724686 100644
--- a/plc4py/plc4py/api/exceptions/exceptions.py
+++ b/plc4py/plc4py/api/exceptions/exceptions.py
@@ -28,11 +28,11 @@ class PlcRuntimeException(PlcException):
 
 
 class PlcConnectionException(Exception):
-    logging.error("Unable to establish a connection to the plc")
+    pass
 
 
 class PlcDataTypeNotFoundException(Exception):
-    logging.error("Unable to find data type in data dictionary")
+    pass
 
 
 class PlcFieldParseException(Exception):
diff --git a/plc4py/plc4py/spi/Plc4xBaseProtocol.py 
b/plc4py/plc4py/spi/Plc4xBaseProtocol.py
index bf9d4115c0..69d04180bd 100644
--- a/plc4py/plc4py/spi/Plc4xBaseProtocol.py
+++ b/plc4py/plc4py/spi/Plc4xBaseProtocol.py
@@ -35,4 +35,5 @@ class Plc4xBaseProtocol(Protocol):
 
     def connection_lost(self, exc: Union[Exception, None]) -> None:
         self.connected = False
-        raise ConnectionError
+        if exc is not None:
+            raise ConnectionError
diff --git a/plc4py/plc4py/spi/Plc4xBaseProtocol.py 
b/plc4py/tests/manual/__init__.py
similarity index 61%
copy from plc4py/plc4py/spi/Plc4xBaseProtocol.py
copy to plc4py/tests/manual/__init__.py
index bf9d4115c0..342be71799 100644
--- a/plc4py/plc4py/spi/Plc4xBaseProtocol.py
+++ b/plc4py/tests/manual/__init__.py
@@ -16,23 +16,3 @@
 # specific language governing permissions and limitations
 # under the License.
 #
-
-from asyncio import Protocol, Future, BaseProtocol
-from dataclasses import dataclass
-from typing import Union
-
-
-@dataclass
-class Plc4xBaseProtocol(Protocol):
-    handler: Future
-    connected: bool = False
-
-    def data_received(self, data) -> None:
-        self.handler.set_result(data)
-
-    def connection_made(self, transport):
-        self.connected = True
-
-    def connection_lost(self, exc: Union[Exception, None]) -> None:
-        self.connected = False
-        raise ConnectionError
diff --git a/plc4py/plc4py/api/exceptions/exceptions.py 
b/plc4py/tests/manual/examples.py
similarity index 58%
copy from plc4py/plc4py/api/exceptions/exceptions.py
copy to plc4py/tests/manual/examples.py
index b1b2e117fc..9d525b694b 100644
--- a/plc4py/plc4py/api/exceptions/exceptions.py
+++ b/plc4py/tests/manual/examples.py
@@ -16,36 +16,24 @@
 # specific language governing permissions and limitations
 # under the License.
 #
-import logging
 
+import asyncio
+from plc4py.PlcDriverManager import PlcDriverManager
 
-class PlcException(Exception):
-    pass
-
-
-class PlcRuntimeException(PlcException):
-    pass
+connection_string = "modbus://127.0.0.1:5020"
+driver_manager = PlcDriverManager()
 
 
-class PlcConnectionException(Exception):
-    logging.error("Unable to establish a connection to the plc")
+async def communicate_with_plc():
+    async with driver_manager.connection(connection_string) as connection:
+        with connection.read_request_builder() as builder:
+            builder.add_item("Random Tag", "4x00001[10]")
+            request = builder.build()
 
-
-class PlcDataTypeNotFoundException(Exception):
-    logging.error("Unable to find data type in data dictionary")
-
-
-class PlcFieldParseException(Exception):
+        future = connection.execute(request)
+        await future
+        response = future.result()
+        print(response)
     pass
 
-
-class PlcNotImplementedException(Exception):
-    pass
-
-
-class SerializationException(Exception):
-    pass
-
-
-class ParseException(Exception):
-    pass
+asyncio.run(communicate_with_plc())
diff --git a/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_connection.py 
b/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_connection.py
index 109c9063b6..36dc4357ac 100644
--- a/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_connection.py
+++ b/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_connection.py
@@ -36,35 +36,21 @@ async def manual_test_plc_driver_modbus_connect():
 
 
 @pytest.mark.asyncio
-async def manual_test_plc_driver_modbus_read():
+async def test_plc_driver_modbus_read():
+
+    connection_string = "modbus://127.0.0.1:5020"
     driver_manager = PlcDriverManager()
-    async with driver_manager.connection("modbus://192.168.23.12:502") as 
connection:
-        with connection.read_request_builder() as builder:
-            builder.add_item("Random Tag", "4x00001[10]")
-            request = builder.build()
+    response = None
+
+    async def communicate_with_plc():
+        async with driver_manager.connection(connection_string) as connection:
+            with connection.read_request_builder() as builder:
+                builder.add_item("Random Tag", "4x00001[10]")
+                request = builder.build()
 
         future = connection.execute(request)
         await future
         response = future.result()
 
-        for tag_name in response.tag_names:
-            if response.tags[tag_name].response_code == PlcResponseCode.OK:
-                num_values: int = len(response.tags[tag_name].value)
-                # If it's just one element, output just one single line.
-                if num_values == 1:
-                    logger.info(
-                        "Value[" + tag_name + "]: " + 
response.tags[tag_name].value
-                    )
-
-                # If it's more than one element, output each in a single row.
-                else:
-                    logger.info("Value[" + tag_name + "]:")
-                    for i in response.tags[tag_name].value.get_list():
-                        logger.info(" - " + str(i))
-
-            # Something went wrong, to output an error message instead.
-            else:
-                logger.error(
-                    "Error[" + tag_name + "]: " + 
response.tags[tag_name].name()
-                )
+    communicate_with_plc()
     pass

Reply via email to