From 98f69edd23f369cfefd19c7885959e1a6a8948b6 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Wed, 5 Jul 2023 17:59:29 +0200 Subject: [PATCH] temp. --- pymodbus/client/base.py | 3 +-- pymodbus/client/serial.py | 4 ---- pymodbus/transport/transport.py | 9 +++++++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pymodbus/client/base.py b/pymodbus/client/base.py index fccf7ca346..8d4d77f124 100644 --- a/pymodbus/client/base.py +++ b/pymodbus/client/base.py @@ -59,8 +59,6 @@ class _params: broadcast_enable: bool = None reconnect_delay: int = None - handle_local_echo: bool = None - source_address: tuple[str, int] = None server_hostname: str = None @@ -96,6 +94,7 @@ def __init__( # pylint: disable=too-many-arguments bytesize=kwargs.get("bytesize", None), parity=kwargs.get("parity", None), stopbits=kwargs.get("stopbits", None), + handle_local_echo=kwargs.get("handle_local_echo", False), ) if not self.use_sync: ModbusProtocol.__init__( diff --git a/pymodbus/client/serial.py b/pymodbus/client/serial.py index 50f7597dac..addb5bdda2 100644 --- a/pymodbus/client/serial.py +++ b/pymodbus/client/serial.py @@ -52,7 +52,6 @@ def __init__( bytesize: int = 8, parity: str = "N", stopbits: int = 1, - handle_local_echo: bool = False, **kwargs: Any, ) -> None: """Initialize Asyncio Modbus Serial Client.""" @@ -68,7 +67,6 @@ def __init__( stopbits=stopbits, **kwargs, ) - self.params.handle_local_echo = handle_local_echo @property def connected(self): @@ -127,7 +125,6 @@ def __init__( bytesize: int = 8, parity: str = "N", stopbits: int = 1, - handle_local_echo: bool = False, **kwargs: Any, ) -> None: """Initialize Modbus Serial Client.""" @@ -144,7 +141,6 @@ def __init__( stopbits=stopbits, **kwargs, ) - self.params.handle_local_echo = handle_local_echo self.socket = None self.last_frame_end = None diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 2fdf1df645..6f9d46cd66 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -38,6 +38,7 @@ class CommParams: host: str = "127.0.0.1" port: int = 0 source_address: tuple[str, int] = ("127.0.0.1", 0) + handle_local_echo: bool = False # tls sslctx: ssl.SSLContext = None @@ -134,6 +135,7 @@ def __init__( self.unique_id: str = str(id(self)) self.reconnect_task: asyncio.Task = None self.reconnect_delay_current: float = 0.0 + self.sent_buffer: bytes = b"" # ModbusProtocol specific setup if self.comm_params.comm_type == CommType.SERIAL: @@ -281,6 +283,8 @@ def data_received(self, data: bytes): :param data: non-empty bytes object with incoming data. """ Log.debug("recv: {}", data, ":hex") + if self.comm_params.handle_local_echo and self.sent_buffer == data: + return self.recv_buffer += data cut = self.callback_data(self.recv_buffer) self.recv_buffer = self.recv_buffer[cut:] @@ -288,6 +292,9 @@ def data_received(self, data: bytes): def datagram_received(self, data: bytes, addr: tuple): """Receive datagram (UDP connections).""" Log.debug("recv: {} addr={}", data, ":hex", addr) + if self.comm_params.handle_local_echo and self.sent_buffer == data: + self.sent_buffer = b"" + return self.recv_buffer += data cut = self.callback_data(self.recv_buffer, addr=addr) self.recv_buffer = self.recv_buffer[cut:] @@ -327,6 +334,8 @@ def transport_send(self, data: bytes, addr: tuple = None) -> None: :param addr: optional addr, only used for UDP server. """ Log.debug("send: {}", data, ":hex") + if self.comm_params.handle_local_echo: + self.sent_buffer = data if self.comm_params.comm_type == CommType.UDP: if addr: self.transport.sendto(data, addr=addr)