Skip to content

Commit

Permalink
temp.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Jul 5, 2023
1 parent 2fecfe2 commit 98f69ed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 1 addition & 2 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__(
Expand Down
4 changes: 0 additions & 4 deletions pymodbus/client/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -68,7 +67,6 @@ def __init__(
stopbits=stopbits,
**kwargs,
)
self.params.handle_local_echo = handle_local_echo

@property
def connected(self):
Expand Down Expand Up @@ -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."""
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -281,13 +283,18 @@ 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:]

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:]
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 98f69ed

Please sign in to comment.