Skip to content

Commit 80ed3d3

Browse files
committed
Update with a lot more testing.
1 parent bff51e9 commit 80ed3d3

13 files changed

+307
-283
lines changed

examples/client_async.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
)
4343

4444

45-
def setup_client(args=None):
45+
def setup_async_client(args=None):
4646
"""Run client setup."""
4747
if not args:
4848
args = get_commandline()
49-
if args.comm != "serial":
49+
if args.comm != "serial" and args.port:
5050
args.port = int(args.port)
5151
_logger.info("### Create client object")
5252

@@ -116,7 +116,7 @@ def setup_client(args=None):
116116
return client
117117

118118

119-
async def run_client(client, modbus_calls=None):
119+
async def run_async_client(client, modbus_calls=None):
120120
"""Run sync client."""
121121
_logger.info("### Client starting")
122122
await client.connect()
@@ -191,5 +191,5 @@ def get_commandline():
191191

192192
if __name__ == "__main__":
193193
# Connect/disconnect no calls.
194-
testclient = setup_client()
195-
asyncio.run(run_client(testclient))
194+
testclient = setup_async_client()
195+
asyncio.run(run_async_client(testclient))

examples/client_async_basic_calls.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
python3 server_sync.py
88
"""
99
import asyncio
10+
import logging
1011

11-
from examples.client_async import _logger, run_client, setup_client
12+
from examples.client_async import run_async_client, setup_async_client
1213

1314

1415
SLAVE = 0x01
1516

1617

17-
async def handle_coils(client):
18+
async def _handle_coils(client):
1819
"""Read/Write coils."""
1920
_logger.info("### Reading Coil")
2021
rr = await client.read_coils(1, 1, slave=SLAVE)
@@ -58,7 +59,7 @@ async def handle_coils(client):
5859
_logger.debug(txt)
5960

6061

61-
async def handle_discrete_input(client):
62+
async def _handle_discrete_input(client):
6263
"""Read discrete inputs."""
6364
_logger.info("### Reading discrete input, Read address:0-7")
6465
rr = await client.read_discrete_inputs(0, 8, slave=SLAVE)
@@ -67,7 +68,7 @@ async def handle_discrete_input(client):
6768
_logger.debug(txt)
6869

6970

70-
async def handle_holding_registers(client):
71+
async def _handle_holding_registers(client):
7172
"""Read/write holding registers."""
7273
_logger.info("### write holding register and read holding registers")
7374
rq = await client.write_register(1, 10, slave=SLAVE)
@@ -101,7 +102,7 @@ async def handle_holding_registers(client):
101102
_logger.debug(txt)
102103

103104

104-
async def handle_input_registers(client):
105+
async def _handle_input_registers(client):
105106
"""Read input registers."""
106107
_logger.info("### read input registers")
107108
rr = await client.read_input_registers(1, 8, slave=SLAVE)
@@ -110,14 +111,21 @@ async def handle_input_registers(client):
110111
_logger.debug(txt)
111112

112113

113-
async def demonstrate_calls(client):
114+
async def run_async_basic_calls(client):
114115
"""Demonstrate basic read/write calls."""
115-
await handle_coils(client)
116-
await handle_discrete_input(client)
117-
await handle_holding_registers(client)
118-
await handle_input_registers(client)
116+
await _handle_coils(client)
117+
await _handle_discrete_input(client)
118+
await _handle_holding_registers(client)
119+
await _handle_input_registers(client)
120+
121+
# --------------------------------------------------------------------------- #
122+
# Extra code, to allow commandline parameters instead of changing the code
123+
# --------------------------------------------------------------------------- #
124+
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
125+
logging.basicConfig(format=FORMAT)
126+
_logger = logging.getLogger()
119127

120128

121129
if __name__ == "__main__":
122-
testclient = setup_client()
123-
asyncio.run(run_client(testclient, modbus_calls=demonstrate_calls))
130+
testclient = setup_async_client()
131+
asyncio.run(run_async_client(testclient, modbus_calls=run_async_basic_calls))

examples/client_async_extended_calls.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
python3 server_sync.py
2424
"""
2525
import asyncio
26+
import logging
2627

27-
from examples.client_async import _logger, run_client, setup_client
28+
from examples.client_async import run_async_client, setup_async_client
2829

2930
from pymodbus.diag_message import (
3031
ChangeAsciiInputDelimiterRequest,
@@ -56,7 +57,7 @@
5657
UNIT = 0x01
5758

5859

59-
async def execute_information_requests(client):
60+
async def _execute_information_requests(client):
6061
"""Execute extended information requests."""
6162
_logger.info("### Running ReadDeviceInformationRequest")
6263
rr = await client.execute(ReadDeviceInformationRequest(unit=UNIT))
@@ -87,7 +88,7 @@ async def execute_information_requests(client):
8788
assert not rr.events # test the number of events
8889

8990

90-
async def execute_diagnostic_requests(client):
91+
async def _execute_diagnostic_requests(client):
9192
"""Execute extended diagnostic requests."""
9293
_logger.info("### Running ReturnQueryDataRequest")
9394
rr = await client.execute(ReturnQueryDataRequest(unit=UNIT))
@@ -160,12 +161,19 @@ async def execute_diagnostic_requests(client):
160161
assert rr and not rr.isError() # test that calls was OK
161162

162163

163-
async def demonstrate_calls(client):
164+
async def run_async_ext_calls(client):
164165
"""Demonstrate basic read/write calls."""
165-
await execute_information_requests(client)
166-
await execute_diagnostic_requests(client)
166+
await _execute_information_requests(client)
167+
await _execute_diagnostic_requests(client)
168+
169+
# --------------------------------------------------------------------------- #
170+
# Extra code, to allow commandline parameters instead of changing the code
171+
# --------------------------------------------------------------------------- #
172+
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
173+
logging.basicConfig(format=FORMAT)
174+
_logger = logging.getLogger()
167175

168176

169177
if __name__ == "__main__":
170-
testclient = setup_client()
171-
asyncio.run(run_client(testclient, modbus_calls=demonstrate_calls))
178+
testclient = setup_async_client()
179+
asyncio.run(run_async_client(testclient, modbus_calls=run_async_ext_calls))

examples/client_sync.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
)
4242

4343

44-
def setup_client(args=None):
44+
def setup_sync_client(args=None):
4545
"""Run client setup."""
4646
if not args:
4747
args = get_commandline()
48-
if args.comm != "serial":
48+
if args.comm != "serial" and args.port:
4949
args.port = int(args.port)
5050
_logger.info("### Create client object")
5151
if args.comm == "tcp":
@@ -114,7 +114,7 @@ def setup_client(args=None):
114114
return client
115115

116116

117-
def run_client(client, modbus_calls=None):
117+
def run_sync_client(client, modbus_calls=None):
118118
"""Run sync client."""
119119
_logger.info("### Client starting")
120120
client.connect()
@@ -188,5 +188,5 @@ def get_commandline():
188188

189189
if __name__ == "__main__":
190190
# Connect/disconnect no calls.
191-
testclient = setup_client()
192-
run_client(testclient)
191+
testclient = setup_sync_client()
192+
run_sync_client(testclient)

examples/client_sync_basic_calls.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
The corresponding server must be started before e.g. as:
77
python3 server_sync.py
88
"""
9-
from examples.client_sync import _logger, run_client, setup_client
9+
import logging
10+
11+
from examples.client_sync import run_sync_client, setup_sync_client
1012

1113

1214
SLAVE = 0x01
1315

1416

15-
def handle_coils(client):
17+
def _handle_coils(client):
1618
"""Read/Write coils."""
1719
_logger.info("### Reading Coil")
1820
rr = client.read_coils(1, 1, slave=SLAVE)
@@ -56,7 +58,7 @@ def handle_coils(client):
5658
_logger.debug(txt)
5759

5860

59-
def handle_discrete_input(client):
61+
def _handle_discrete_input(client):
6062
"""Read discrete inputs."""
6163
_logger.info("### Reading discrete input, Read address:0-7")
6264
rr = client.read_discrete_inputs(0, 8, slave=SLAVE)
@@ -65,7 +67,7 @@ def handle_discrete_input(client):
6567
_logger.debug(txt)
6668

6769

68-
def handle_holding_registers(client):
70+
def _handle_holding_registers(client):
6971
"""Read/write holding registers."""
7072
_logger.info("### write holding register and read holding registers")
7173
rq = client.write_register(1, 10, slave=SLAVE)
@@ -99,7 +101,7 @@ def handle_holding_registers(client):
99101
_logger.debug(txt)
100102

101103

102-
def handle_input_registers(client):
104+
def _handle_input_registers(client):
103105
"""Read input registers."""
104106
_logger.info("### read input registers")
105107
rr = client.read_input_registers(1, 8, slave=SLAVE)
@@ -108,14 +110,22 @@ def handle_input_registers(client):
108110
_logger.debug(txt)
109111

110112

111-
def demonstrate_calls(client):
113+
def run_sync_basic_calls(client):
112114
"""Demonstrate basic read/write calls."""
113-
handle_coils(client)
114-
handle_discrete_input(client)
115-
handle_holding_registers(client)
116-
handle_input_registers(client)
115+
_handle_coils(client)
116+
_handle_discrete_input(client)
117+
_handle_holding_registers(client)
118+
_handle_input_registers(client)
119+
120+
121+
# --------------------------------------------------------------------------- #
122+
# Extra code, to allow commandline parameters instead of changing the code
123+
# --------------------------------------------------------------------------- #
124+
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
125+
logging.basicConfig(format=FORMAT)
126+
_logger = logging.getLogger()
117127

118128

119129
if __name__ == "__main__":
120-
testclient = setup_client()
121-
run_client(testclient, modbus_calls=demonstrate_calls)
130+
testclient = setup_sync_client()
131+
run_sync_client(testclient, modbus_calls=run_sync_basic_calls)

examples/client_sync_extended_calls.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
The corresponding server must be started before e.g. as:
2323
python3 server_sync.py
2424
"""
25-
from examples.client_sync import _logger, run_client, setup_client
25+
import logging
26+
27+
from examples.client_sync import run_sync_client, setup_sync_client
2628

2729
from pymodbus.diag_message import (
2830
ChangeAsciiInputDelimiterRequest,
@@ -54,7 +56,7 @@
5456
UNIT = 0x01
5557

5658

57-
def execute_information_requests(client):
59+
def _execute_information_requests(client):
5860
"""Execute extended information requests."""
5961
_logger.info("### Running ReadDeviceInformationRequest")
6062
rr = client.execute(ReadDeviceInformationRequest(unit=UNIT))
@@ -85,7 +87,7 @@ def execute_information_requests(client):
8587
assert not rr.events # test the number of events
8688

8789

88-
def execute_diagnostic_requests(client):
90+
def _execute_diagnostic_requests(client):
8991
"""Execute extended diagnostic requests."""
9092
_logger.info("### Running ReturnQueryDataRequest")
9193
rr = client.execute(ReturnQueryDataRequest(unit=UNIT))
@@ -158,12 +160,20 @@ def execute_diagnostic_requests(client):
158160
assert rr and not rr.isError() # test that calls was OK
159161

160162

161-
def demonstrate_calls(client):
163+
def run_sync_ext_calls(client):
162164
"""Demonstrate basic read/write calls."""
163-
execute_information_requests(client)
164-
execute_diagnostic_requests(client)
165+
_execute_information_requests(client)
166+
_execute_diagnostic_requests(client)
167+
168+
169+
# --------------------------------------------------------------------------- #
170+
# Extra code, to allow commandline parameters instead of changing the code
171+
# --------------------------------------------------------------------------- #
172+
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
173+
logging.basicConfig(format=FORMAT)
174+
_logger = logging.getLogger()
165175

166176

167177
if __name__ == "__main__":
168-
testclient = setup_client()
169-
run_client(testclient, modbus_calls=demonstrate_calls)
178+
testclient = setup_sync_client()
179+
run_sync_client(testclient, modbus_calls=run_sync_ext_calls)

0 commit comments

Comments
 (0)