Skip to content

Commit

Permalink
transport_emulator, part II. (#1710)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Aug 14, 2023
1 parent 2f01596 commit e11fe6b
Show file tree
Hide file tree
Showing 36 changed files with 476 additions and 520 deletions.
8 changes: 4 additions & 4 deletions examples/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def setup_async_client(description=None, cmdline=None):
port=args.port, # on which port
# Common optional paramers:
framer=args.framer,
timeout=5,
timeout=args.timeout,
retries=3,
reconnect_delay=1,
reconnect_delay_max=10,
Expand All @@ -74,7 +74,7 @@ def setup_async_client(description=None, cmdline=None):
port=args.port,
# Common optional paramers:
framer=args.framer,
# timeout=10,
timeout=args.timeout,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,
Expand All @@ -87,7 +87,7 @@ def setup_async_client(description=None, cmdline=None):
args.port,
# Common optional paramers:
# framer=ModbusRtuFramer,
# timeout=10,
timeout=args.timeout,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,
Expand All @@ -105,7 +105,7 @@ def setup_async_client(description=None, cmdline=None):
port=args.port,
# Common optional paramers:
framer=args.framer,
# timeout=10,
timeout=args.timeout,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,
Expand Down
8 changes: 4 additions & 4 deletions examples/client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def setup_sync_client(description=None, cmdline=None):
port=args.port,
# Common optional paramers:
framer=args.framer,
# timeout=10,
timeout=args.timeout,
# retries=3,
# retry_on_empty=False,y
# close_comm_on_error=False,
Expand All @@ -72,7 +72,7 @@ def setup_sync_client(description=None, cmdline=None):
port=args.port,
# Common optional paramers:
framer=args.framer,
# timeout=10,
timeout=args.timeout,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,
Expand All @@ -85,7 +85,7 @@ def setup_sync_client(description=None, cmdline=None):
port=args.port, # serial port
# Common optional paramers:
# framer=ModbusRtuFramer,
# timeout=10,
timeout=args.timeout,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,.
Expand All @@ -103,7 +103,7 @@ def setup_sync_client(description=None, cmdline=None):
port=args.port,
# Common optional paramers:
framer=args.framer,
# timeout=10,
timeout=args.timeout,
# retries=3,
# retry_on_empty=False,
# close_comm_on_error=False,
Expand Down
5 changes: 4 additions & 1 deletion examples/datastore_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
usage: server_simulator.py [-h]
[--log {critical,error,warning,info,debug}]
[--port PORT]
[--host HOST]
Command line options for examples
Expand All @@ -14,6 +15,7 @@
--log {critical,error,warning,info,debug}
"critical", "error", "warning", "info" or "debug"
--port PORT the port to use
--host HOST the interface to listen on
The corresponding client can be started as:
python3 client_sync.py
Expand Down Expand Up @@ -129,6 +131,7 @@ def get_commandline(cmdline=None):
type=str,
)
parser.add_argument("--port", help="set port", type=str, default="5020")
parser.add_argument("--host", help="set interface", type=str, default="localhost")
args = parser.parse_args(cmdline)

return args
Expand Down Expand Up @@ -168,7 +171,7 @@ async def run_server_simulator(args):

await StartAsyncTcpServer(
context=args.context,
address=("", args.port),
address=(args.host, args.port),
framer=args.framer,
)

Expand Down
28 changes: 20 additions & 8 deletions examples/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
_logger = logging.getLogger(__file__)


def get_framer(framer):
"""Convert framer name to framer class"""
framers = {
"ascii": ModbusAsciiFramer,
"binary": ModbusBinaryFramer,
"rtu": ModbusRtuFramer,
"socket": ModbusSocketFramer,
"tls": ModbusTlsFramer,
}
return framers[framer]


def get_commandline(server=False, description=None, extras=None, cmdline=None):
"""Read and validate command line arguments"""
parser = argparse.ArgumentParser(description=description)
Expand Down Expand Up @@ -90,6 +102,13 @@ def get_commandline(server=False, description=None, extras=None, cmdline=None):
help="ADVANCED USAGE: set datastore context object",
default=None,
)
else:
parser.add_argument(
"--timeout",
help="ADVANCED USAGE: set client timeout",
default=10,
type=float,
)
if extras: # pragma no cover
for extra in extras:
parser.add_argument(extra[0], **extra[1])
Expand All @@ -102,16 +121,9 @@ def get_commandline(server=False, description=None, extras=None, cmdline=None):
"serial": ["rtu", "/dev/ptyp0"],
"tls": ["tls", 5020],
}
framers = {
"ascii": ModbusAsciiFramer,
"binary": ModbusBinaryFramer,
"rtu": ModbusRtuFramer,
"socket": ModbusSocketFramer,
"tls": ModbusTlsFramer,
}
pymodbus_apply_logging_config(args.log.upper())
_logger.setLevel(args.log.upper())
args.framer = framers[args.framer or comm_defaults[args.comm][0]]
args.framer = get_framer(args.framer or comm_defaults[args.comm][0])
args.port = args.port or comm_defaults[args.comm][1]
if args.comm != "serial" and args.port:
args.port = int(args.port)
Expand Down
4 changes: 2 additions & 2 deletions examples/simple_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)


async def run_async_simple_client(comm, host, port):
async def run_async_simple_client(comm, host, port, framer=ModbusSocketFramer):
"""Run async client."""

# activate debugging
Expand All @@ -44,7 +44,7 @@ async def run_async_simple_client(comm, host, port):
client = AsyncModbusTcpClient(
host,
port=port,
framer=ModbusSocketFramer,
framer=framer,
# timeout=10,
# retries=3,
# retry_on_empty=False,
Expand Down
4 changes: 2 additions & 2 deletions examples/simple_sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)


def run_sync_simple_client(comm, host, port):
def run_sync_simple_client(comm, host, port, framer=ModbusSocketFramer):
"""Run sync client."""

# activate debugging
Expand All @@ -42,7 +42,7 @@ def run_sync_simple_client(comm, host, port):
client = ModbusTcpClient(
host,
port=port,
framer=ModbusSocketFramer,
framer=framer,
# timeout=10,
# retries=3,
# retry_on_empty=False,y
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__( # pylint: disable=too-many-arguments
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
host=kwargs.get("host", None),
port=kwargs.get("port", None),
port=kwargs.get("port", 0),
sslctx=kwargs.get("sslctx", None),
baudrate=kwargs.get("baudrate", None),
bytesize=kwargs.get("bytesize", None),
Expand Down
11 changes: 9 additions & 2 deletions pymodbus/server/async_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation of a Threaded Modbus Server."""
# pylint: disable=missing-type-doc
import asyncio
import os
import time
import traceback
from contextlib import suppress
Expand Down Expand Up @@ -654,7 +655,10 @@ async def async_stop(cls):
if not cls.active_server:
raise RuntimeError("ServerAsyncStop called without server task active.")
await cls.active_server.server.shutdown()
await asyncio.sleep(1)
if os.name == "nt":
await asyncio.sleep(1)
else:
await asyncio.sleep(0.1)
cls.active_server = None

@classmethod
Expand All @@ -667,7 +671,10 @@ def stop(cls):
Log.info("ServerStop called with loop stopped.")
return
asyncio.run_coroutine_threadsafe(cls.async_stop(), cls.active_server.loop)
time.sleep(10)
if os.name == "nt":
time.sleep(10)
else:
time.sleep(0.5)


async def StartAsyncTcpServer( # pylint: disable=invalid-name,dangerous-default-value
Expand Down
Loading

0 comments on commit e11fe6b

Please sign in to comment.