Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions examples/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
)


def setup_client(args):
def setup_client(args=None):
"""Run client setup."""
if not args:
args = get_commandline()
Expand All @@ -68,8 +68,8 @@ def setup_client(args):
)
elif args.comm == "udp":
client = AsyncModbusUdpClient(
"localhost",
# port=502,
"127.0.0.1",
port=args.port,
# Common optional paramers:
# modbus_decoder=ClientDecoder,
framer=args.framer,
Expand Down Expand Up @@ -121,11 +121,11 @@ def setup_client(args):
return client


async def run_client(modbus_calls=None, args=None):
async def run_client(client, modbus_calls=None):
"""Run sync client."""
_logger.info("### Client ready")
client = setup_client(args)
_logger.info("### Client starting")
await client.aConnect()
assert client.protocol
if modbus_calls:
await modbus_calls(client.protocol)
await client.aClose()
Expand Down Expand Up @@ -196,4 +196,5 @@ def get_commandline():

if __name__ == "__main__":
# Connect/disconnect no calls.
asyncio.run(run_client())
testclient = setup_client()
asyncio.run(run_client(testclient))
6 changes: 3 additions & 3 deletions examples/client_async_basic_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""
import asyncio

from examples.client_async import _logger, run_client
from examples.client_async import _logger, run_client, setup_client

UNIT = 0x01

Expand Down Expand Up @@ -118,5 +118,5 @@ async def demonstrate_calls(client):


if __name__ == "__main__":
# Connect/disconnect no calls.
asyncio.run(run_client(demonstrate_calls))
testclient = setup_client()
asyncio.run(run_client(testclient, modbus_calls=demonstrate_calls))
10 changes: 5 additions & 5 deletions examples/client_async_extended_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"""
import asyncio

from examples.client_async import _logger, run_client
from examples.client_async import _logger, run_client, setup_client

from pymodbus.diag_message import (
ChangeAsciiInputDelimiterRequest,
Expand Down Expand Up @@ -57,7 +57,7 @@

async def execute_information_requests(client):
"""Execute extended information requests."""
_logger.info("Running ReadDeviceInformationRequest")
_logger.info("### Running ReadDeviceInformationRequest")
rr = await client.execute(ReadDeviceInformationRequest(unit=UNIT))
assert rr and not rr.isError() # test that calls was OK

Expand Down Expand Up @@ -88,7 +88,7 @@ async def execute_information_requests(client):

async def execute_diagnostic_requests(client):
"""Execute extended diagnostic requests."""
_logger.info("Running ReturnQueryDataRequest")
_logger.info("### Running ReturnQueryDataRequest")
rr = await client.execute(ReturnQueryDataRequest(unit=UNIT))
assert rr and not rr.isError() # test that calls was OK
assert not rr.message[0] # test the resulting message
Expand Down Expand Up @@ -166,5 +166,5 @@ async def demonstrate_calls(client):


if __name__ == "__main__":
# Connect/disconnect no calls.
asyncio.run(run_client(demonstrate_calls))
testclient = setup_client()
asyncio.run(run_client(testclient, modbus_calls=demonstrate_calls))
19 changes: 8 additions & 11 deletions examples/client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
)


def setup_client(args):
def setup_client(args=None):
"""Run client setup."""
if not args:
args = get_commandline()
Expand Down Expand Up @@ -118,16 +118,12 @@ def setup_client(args):
return client


def run_client(modbus_calls=None, args=None):
def run_client(client, modbus_calls=None):
"""Run sync client."""
_logger.info("### Client ready")
client = setup_client(args)
try:
client.connect()
if modbus_calls:
modbus_calls(client)
except: # pylint: disable=bare-except # noqa: E722
_logger.error("Got exception in client.")
_logger.info("### Client starting")
client.connect()
if modbus_calls:
modbus_calls(client)
client.close()
_logger.info("### End of Program")

Expand Down Expand Up @@ -196,4 +192,5 @@ def get_commandline():

if __name__ == "__main__":
# Connect/disconnect no calls.
run_client()
testclient = setup_client()
run_client(testclient)
5 changes: 3 additions & 2 deletions examples/client_sync_basic_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
The corresponding server must be started before e.g. as:
python3 server_sync.py
"""
from examples.client_sync import _logger, run_client
from examples.client_sync import _logger, run_client, setup_client

UNIT = 0x01

Expand Down Expand Up @@ -116,4 +116,5 @@ def demonstrate_calls(client):


if __name__ == "__main__":
run_client(demonstrate_calls)
testclient = setup_client()
run_client(testclient, modbus_calls=demonstrate_calls)
9 changes: 5 additions & 4 deletions examples/client_sync_extended_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
The corresponding server must be started before e.g. as:
python3 server_sync.py
"""
from examples.client_sync import _logger, run_client
from examples.client_sync import _logger, run_client, setup_client

from pymodbus.diag_message import (
ChangeAsciiInputDelimiterRequest,
Expand Down Expand Up @@ -55,7 +55,7 @@

def execute_information_requests(client):
"""Execute extended information requests."""
_logger.info("Running ReadDeviceInformationRequest")
_logger.info("### Running ReadDeviceInformationRequest")
rr = client.execute(ReadDeviceInformationRequest(unit=UNIT))
assert rr and not rr.isError() # test that calls was OK

Expand Down Expand Up @@ -86,7 +86,7 @@ def execute_information_requests(client):

def execute_diagnostic_requests(client):
"""Execute extended diagnostic requests."""
_logger.info("Running ReturnQueryDataRequest")
_logger.info("### Running ReturnQueryDataRequest")
rr = client.execute(ReturnQueryDataRequest(unit=UNIT))
assert rr and not rr.isError() # test that calls was OK
assert not rr.message[0] # test the resulting message
Expand Down Expand Up @@ -164,4 +164,5 @@ def demonstrate_calls(client):


if __name__ == "__main__":
run_client(demonstrate_calls)
testclient = setup_client()
run_client(testclient, modbus_calls=demonstrate_calls)
70 changes: 35 additions & 35 deletions examples/server_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,76 +148,76 @@ async def run_server(args=None):

_logger.info("### start server")
if server_id == "tcp":
server = StartTcpServer(
server = await StartTcpServer(
context=store, # Data storage
identity=identity, # server identify
# TBD host=
# TBD port=
address=("", port), # listen address
custom_functions=[], # allow custom handling
address=("127.0.0.1", port), # listen address
# custom_functions=[], # allow custom handling
framer=framer, # The framer strategy to use
handler=None, # handler for each session
# handler=None, # handler for each session
allow_reuse_address=True, # allow the reuse of an address
ignore_missing_slaves=True, # ignore request to a missing slave
broadcast_enable=False, # treat unit_id 0 as broadcast address,
# ignore_missing_slaves=True, # ignore request to a missing slave
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
# TBD timeout=1, # waiting time for request to complete
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
defer_start=defer_start # Only define server do not activate
)
elif server_id == "udp":
server = StartUdpServer(
server = await StartUdpServer(
context=store, # Data storage
identity=identity, # server identify
address=("", port), # listen address
custom_functions=[], # allow custom handling
address=("127.0.0.1", port), # listen address
# custom_functions=[], # allow custom handling
framer=framer, # The framer strategy to use
handler=None, # handler for each session
# handler=None, # handler for each session
# TBD allow_reuse_address=True, # allow the reuse of an address
ignore_missing_slaves=True, # ignore request to a missing slave
broadcast_enable=False, # treat unit_id 0 as broadcast address,
# ignore_missing_slaves=True, # ignore request to a missing slave
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
# TBD timeout=1, # waiting time for request to complete
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
defer_start=defer_start # Only define server do not activate
)
elif server_id == "serial":
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,
# link=/tmp/ttyp0,raw,echo=0,ospeed=9600
server = StartSerialServer(
server = await StartSerialServer(
context=store, # Data storage
identity=identity, # server identify
timeout=0.005, # waiting time for request to complete
# timeout=0.005, # waiting time for request to complete
port=port, # serial port
custom_functions=[], # allow custom handling
# custom_functions=[], # allow custom handling
framer=framer, # The framer strategy to use
handler=None, # handler for each session
stopbits=1, # The number of stop bits to use
bytesize=8, # The bytesize of the serial messages
parity="N", # Which kind of parity to use
baudrate=9600, # The baud rate to use for the serial device
handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
ignore_missing_slaves=True, # ignore request to a missing slave
broadcast_enable=False, # treat unit_id 0 as broadcast address,
strict=True, # use strict timing, t1.5 for Modbus RTU
# handler=None, # handler for each session
# stopbits=1, # The number of stop bits to use
# bytesize=8, # The bytesize of the serial messages
# parity="N", # Which kind of parity to use
# baudrate=9600, # The baud rate to use for the serial device
# handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
# ignore_missing_slaves=True, # ignore request to a missing slave
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
# strict=True, # use strict timing, t1.5 for Modbus RTU
defer_start=defer_start # Only define server do not activate
)
elif server_id == "tls":
server = StartTlsServer(
server = await StartTlsServer(
context=store, # Data storage
host="localhost", # define tcp address where to connect to.
port=port, # on which port
# port=port, # on which port
identity=identity, # server identify
custom_functions=[], # allow custom handling
# custom_functions=[], # allow custom handling
address=("", 5020), # listen address
framer=framer, # The framer strategy to use
handler=None, # handler for each session
# handler=None, # handler for each session
allow_reuse_address=True, # allow the reuse of an address
certfile=None, # The cert file path for TLS (used if sslctx is None)
sslctx=None, # The SSLContext to use for TLS (default None and auto create)
keyfile=None, # The key file path for TLS (used if sslctx is None)
password=None, # The password for for decrypting the private key file
reqclicert=False, # Force the sever request client"s certificate
ignore_missing_slaves=True, # ignore request to a missing slave
broadcast_enable=False, # treat unit_id 0 as broadcast address,
# certfile=None, # The cert file path for TLS (used if sslctx is None)
# sslctx=None, # The SSLContext to use for TLS (default None and auto create)
# keyfile=None, # The key file path for TLS (used if sslctx is None)
# password=None, # The password for for decrypting the private key file
# reqclicert=False, # Force the sever request client"s certificate
# ignore_missing_slaves=True, # ignore request to a missing slave
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
# TBD timeout=1, # waiting time for request to complete
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
defer_start=defer_start # Only define server do not activate
Expand Down
44 changes: 22 additions & 22 deletions examples/server_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,39 +186,39 @@ def run_server(args=None):
server = StartSerialServer(
context=store, # Data storage
identity=identity, # server identify
timeout=0.005, # waiting time for request to complete
# timeout=0.005, # waiting time for request to complete
port=port, # serial port
custom_functions=[], # allow custom handling
# custom_functions=[], # allow custom handling
framer=framer, # The framer strategy to use
handler=None, # handler for each session
stopbits=1, # The number of stop bits to use
bytesize=7, # The bytesize of the serial messages
parity="E", # Which kind of parity to use
baudrate=9600, # The baud rate to use for the serial device
handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
ignore_missing_slaves=True, # ignore request to a missing slave
broadcast_enable=False, # treat unit_id 0 as broadcast address,
strict=True, # use strict timing, t1.5 for Modbus RTU
# handler=None, # handler for each session
# stopbits=1, # The number of stop bits to use
# bytesize=7, # The bytesize of the serial messages
# parity="E", # Which kind of parity to use
# baudrate=9600, # The baud rate to use for the serial device
# handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
# ignore_missing_slaves=True, # ignore request to a missing slave
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
# strict=True, # use strict timing, t1.5 for Modbus RTU
defer_start=defer_start # Only define server do not activate
)
elif server_id == "tls":
server = StartTlsServer(
context=store, # Data storage
host="localhost", # define tcp address where to connect to.
port=port, # on which port
# port=port, # on which port
identity=identity, # server identify
custom_functions=[], # allow custom handling
# custom_functions=[], # allow custom handling
address=("", 5020), # listen address
framer=framer, # The framer strategy to use
handler=None, # handler for each session
allow_reuse_address=True, # allow the reuse of an address
certfile=None, # The cert file path for TLS (used if sslctx is None)
sslctx=None, # The SSLContext to use for TLS (default None and auto create)
keyfile=None, # The key file path for TLS (used if sslctx is None)
password=None, # The password for for decrypting the private key file
reqclicert=False, # Force the sever request client"s certificate
ignore_missing_slaves=True, # ignore request to a missing slave
broadcast_enable=False, # treat unit_id 0 as broadcast address,
# handler=None, # handler for each session
# allow_reuse_address=True, # allow the reuse of an address
# certfile=None, # The cert file path for TLS (used if sslctx is None)
# sslctx=None, # The SSLContext to use for TLS (default None and auto create)
# keyfile=None, # The key file path for TLS (used if sslctx is None)
# password=None, # The password for for decrypting the private key file
# reqclicert=False, # Force the sever request client"s certificate
# ignore_missing_slaves=True, # ignore request to a missing slave
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
# TBD timeout=1, # waiting time for request to complete
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
defer_start=defer_start # Only define server do not activate
Expand Down
1 change: 1 addition & 0 deletions pymodbus/client/async_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AsyncModbusTcpClient(ModbusBaseClient):

:param host: (positional) Host IP address
:param port: (optional default 502) The TCP port used for communication.
:param framer: (optional, default ModbusSocketFramer) Framer class.
:param source_address: (optional, default none) source address of client,
:param \*\*kwargs: (optional) Extra experimental parameters for transport
:return: client object
Expand Down
Loading