Skip to content

Commit

Permalink
Merge pull request #631 from riptideio/home-assit-fix-#629
Browse files Browse the repository at this point in the history
#629 Add kwarg 'reset_socket' to disable closing sockets on error
  • Loading branch information
dhoomakethu authored May 13, 2021
2 parents c7a582b + a22932e commit 6c15a3a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
6 changes: 4 additions & 2 deletions pymodbus/repl/client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,10 @@ def _process_args(args, string=True):
@click.option("--retry-on-error", is_flag=True, default=False,
help="Retry on error response")
@click.option("--retries", default=3, help="Retry count")
@click.option("--reset-socket/--no-reset-socket", default=True, help="Reset client socket on error")
@click.pass_context
def main(ctx, verbose, broadcast_support, retry_on_empty,
retry_on_error, retries):
retry_on_error, retries, reset_socket):
if verbose:
global log
import logging
Expand All @@ -248,7 +249,8 @@ def main(ctx, verbose, broadcast_support, retry_on_empty,
"broadcast": broadcast_support,
"retry_on_empty": retry_on_empty,
"retry_on_invalid": retry_on_error,
"retries": retries
"retries": retries,
"reset_socket": reset_socket
}


Expand Down
19 changes: 11 additions & 8 deletions pymodbus/repl/server/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
| ___< | |/ \ / _ \ / __ | | __ \| | \/ ___/ \_____ \_/ __ \_ __ \ \/ // __ \_ __ \\
| | \___ | Y Y ( <_> ) /_/ | | \_\ \ | /\___ \ / \ ___/| | \/\ /\ ___/| | \/
|____| / ____|__|_| /\____/\____ | |___ /____//____ > /_______ /\___ >__| \_/ \___ >__|
\/ \/ \/ \/ \/ \/ \/ \/"""
\/ \/ \/ \/ \/ \/ \/ \/
"""

SMALL_TITLE = "Pymodbus server..."
BOTTOM_TOOLBAR = HTML('(MODBUS SERVER) <b><style bg="ansired">Press Ctrl+C or '
Expand Down Expand Up @@ -80,7 +81,9 @@


def info(message):
click.secho(str(message), fg="green")
if not isinstance(message, str):
message = str(message)
click.secho(message, fg="green")


def warning(message):
Expand Down Expand Up @@ -191,12 +194,12 @@ async def interactive_shell(server):


async def main(server):
with patch_stdout():
try:
await interactive_shell(server)
finally:
pass
warning("Bye Bye!!!")
# with patch_stdout():
try:
await interactive_shell(server)
finally:
pass
warning("Bye Bye!!!")


async def run_repl(server):
Expand Down
10 changes: 7 additions & 3 deletions pymodbus/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(self, client, **kwargs):
self.retry_on_invalid = kwargs.get('retry_on_invalid',
Defaults.RetryOnInvalid)
self.retries = kwargs.get('retries', Defaults.Retries) or 1
self.reset_socket = kwargs.get('reset_socket', True)
self._transaction_lock = RLock()
self._no_response_devices = []
if client:
Expand Down Expand Up @@ -217,7 +218,8 @@ def execute(self, request):
"/Unable to decode response")
response = ModbusIOException(last_exception,
request.function_code)
self.client.close()
if self.reset_socket:
self.client.close()
if hasattr(self.client, "state"):
_logger.debug("Changing transaction state from "
"'PROCESSING REPLY' to "
Expand All @@ -230,7 +232,8 @@ def execute(self, request):
# Handle decode errors in processIncomingPacket method
_logger.exception(ex)
self.client.state = ModbusTransactionState.TRANSACTION_COMPLETE
self.client.close()
if self.reset_socket:
self.client.close()
return ex

def _retry_transaction(self, retries, reason,
Expand Down Expand Up @@ -294,7 +297,8 @@ def _transact(self, packet, response_length,

except (socket.error, ModbusIOException,
InvalidMessageReceivedException) as msg:
self.client.close()
if self.reset_socket:
self.client.close()
_logger.debug("Transaction failed. (%s) " % msg)
last_exception = msg
result = b''
Expand Down

0 comments on commit 6c15a3a

Please sign in to comment.