Skip to content

Commit 66aeeba

Browse files
authored
Add test_examples. (#1032)
1 parent 679c495 commit 66aeeba

File tree

13 files changed

+179
-92
lines changed

13 files changed

+179
-92
lines changed

examples/client_async.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@
4343
)
4444

4545

46-
def setup_async_client():
46+
def setup_client(args):
4747
"""Run client setup."""
48-
args = get_commandline()
48+
if not args:
49+
args = get_commandline()
50+
if args.comm != "serial":
51+
args.port = int(args.port)
4952
_logger.info("### Create client object")
5053

5154
if args.comm == "tcp":
@@ -118,10 +121,10 @@ def setup_async_client():
118121
return client
119122

120123

121-
async def run_async_client(modbus_calls=None):
124+
async def run_client(modbus_calls=None, args=None):
122125
"""Run sync client."""
123126
_logger.info("### Client ready")
124-
client = setup_async_client()
127+
client = setup_client(args)
125128
await client.aConnect()
126129
if modbus_calls:
127130
await modbus_calls(client.protocol)
@@ -186,12 +189,11 @@ def get_commandline():
186189
args.comm = "tcp"
187190
if not args.framer:
188191
args.framer = comm_defaults[args.comm][0]
189-
if not args.port:
190-
args.port = comm_defaults[args.comm][1]
192+
args.port = args.port or comm_defaults[args.comm][1]
191193
args.framer = framers[args.framer]
192194
return args
193195

194196

195197
if __name__ == "__main__":
196198
# Connect/disconnect no calls.
197-
asyncio.run(run_async_client())
199+
asyncio.run(run_client())

examples/client_async_basic_calls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99
import asyncio
1010

11-
from examples.client_async import _logger, run_async_client
11+
from examples.client_async import _logger, run_client
1212

1313
UNIT = 0x01
1414

@@ -119,4 +119,4 @@ async def demonstrate_calls(client):
119119

120120
if __name__ == "__main__":
121121
# Connect/disconnect no calls.
122-
asyncio.run(run_async_client(demonstrate_calls))
122+
asyncio.run(run_client(demonstrate_calls))

examples/client_async_extended_calls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"""
2525
import asyncio
2626

27-
from examples.client_async import _logger, run_async_client
27+
from examples.client_async import _logger, run_client
2828

2929
from pymodbus.diag_message import (
3030
ChangeAsciiInputDelimiterRequest,
@@ -167,4 +167,4 @@ async def demonstrate_calls(client):
167167

168168
if __name__ == "__main__":
169169
# Connect/disconnect no calls.
170-
asyncio.run(run_async_client(demonstrate_calls))
170+
asyncio.run(run_client(demonstrate_calls))

examples/client_sync.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
)
4242

4343

44-
def setup_sync_client():
44+
def setup_client(args):
4545
"""Run client setup."""
46-
args = get_commandline()
46+
if not args:
47+
args = get_commandline()
48+
if args.comm != "serial":
49+
args.port = int(args.port)
4750
_logger.info("### Create client object")
4851
if args.comm == "tcp":
4952
client = ModbusTcpClient(
@@ -115,10 +118,10 @@ def setup_sync_client():
115118
return client
116119

117120

118-
def run_sync_client(modbus_calls=None):
121+
def run_client(modbus_calls=None, args=None):
119122
"""Run sync client."""
120123
_logger.info("### Client ready")
121-
client = setup_sync_client()
124+
client = setup_client(args)
122125
client.connect()
123126
if modbus_calls:
124127
modbus_calls(client)
@@ -183,12 +186,11 @@ def get_commandline():
183186
args.comm = "tcp"
184187
if not args.framer:
185188
args.framer = comm_defaults[args.comm][0]
186-
if not args.port:
187-
args.port = comm_defaults[args.comm][1]
189+
args.port = args.port or comm_defaults[args.comm][1]
188190
args.framer = framers[args.framer]
189191
return args
190192

191193

192194
if __name__ == "__main__":
193195
# Connect/disconnect no calls.
194-
run_sync_client()
196+
run_client()

examples/client_sync_basic_calls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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_sync_client
9+
from examples.client_sync import _logger, run_client
1010

1111
UNIT = 0x01
1212

@@ -116,4 +116,4 @@ def demonstrate_calls(client):
116116

117117

118118
if __name__ == "__main__":
119-
run_sync_client(demonstrate_calls)
119+
run_client(demonstrate_calls)

examples/client_sync_extended_calls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
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_sync_client
25+
from examples.client_sync import _logger, run_client
2626

2727
from pymodbus.diag_message import (
2828
ChangeAsciiInputDelimiterRequest,
@@ -164,4 +164,4 @@ def demonstrate_calls(client):
164164

165165

166166
if __name__ == "__main__":
167-
run_sync_client(demonstrate_calls)
167+
run_client(demonstrate_calls)

examples/server_async.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@
5757
from pymodbus.version import version
5858

5959

60-
def setup_async_server():
60+
def setup_server(args):
6161
"""Run server setup."""
62-
args = get_commandline()
62+
if not args:
63+
args = get_commandline()
6364

6465
# The datastores only respond to the addresses that are initialized
6566
# If you initialize a DataBlock to addresses of 0x00 to 0xFF, a request to
@@ -133,12 +134,14 @@ def setup_async_server():
133134
"MajorMinorRevision": version.short(),
134135
}
135136
)
137+
if args.comm != "serial":
138+
args.port = int(args.port)
136139
return args.comm, args.port, store, identity, args.framer
137140

138141

139-
async def run_server():
142+
async def run_server(args=None):
140143
"""Run server."""
141-
server, port, store, identity, framer = setup_async_server()
144+
server, port, store, identity, framer = setup_server(args)
142145

143146
_logger.info("### start server")
144147
if server == "tcp":
@@ -287,8 +290,6 @@ def get_commandline():
287290
if not args.framer:
288291
args.framer = comm_defaults[args.comm][0]
289292
args.port = args.port or comm_defaults[args.comm][1]
290-
if args.comm != "serial":
291-
args.port = int(args.port)
292293
args.framer = framers[args.framer]
293294
return args
294295

examples/server_sync.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@
5656
from pymodbus.version import version
5757

5858

59-
def setup_sync_server():
59+
def setup_server(args):
6060
"""Run server setup."""
61-
args = get_commandline()
61+
if not args:
62+
args = get_commandline()
6263

6364
# The datastores only respond to the addresses that are initialized
6465
# If you initialize a DataBlock to addresses of 0x00 to 0xFF, a request to
@@ -132,16 +133,18 @@ def setup_sync_server():
132133
"MajorMinorRevision": version.short(),
133134
}
134135
)
135-
return args.comm, args.port, store, identity, args.framer
136+
if args.comm != "serial":
137+
args.port = int(args.port)
138+
return args.comm, args.port, store, identity, args.framer, args.prepare
136139

137140

138-
def run_server():
141+
def run_server(args=None):
139142
"""Run server."""
140-
server, port, store, identity, framer = setup_sync_server()
143+
server_id, port, store, identity, framer, prepare = setup_server(args)
141144

142145
_logger.info("### start server")
143-
if server == "tcp":
144-
StartTcpServer(
146+
if server_id == "tcp":
147+
server = StartTcpServer(
145148
context=store, # Data storage
146149
identity=identity, # server identify
147150
# TBD host=
@@ -155,9 +158,10 @@ def run_server():
155158
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
156159
# TBD timeout=1, # waiting time for request to complete
157160
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
161+
prepare=prepare # Only prepare server do not activate, INTERNAL.
158162
)
159-
elif server == "udp":
160-
StartUdpServer(
163+
elif server_id == "udp":
164+
server = StartUdpServer(
161165
context=store, # Data storage
162166
identity=identity, # server identify
163167
address=("", port), # listen address
@@ -170,10 +174,10 @@ def run_server():
170174
# TBD timeout=1, # waiting time for request to complete
171175
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
172176
)
173-
elif server == "serial":
177+
elif server_id == "serial":
174178
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,
175179
# link=/tmp/ttyp0,raw,echo=0,ospeed=9600
176-
StartSerialServer(
180+
server = StartSerialServer(
177181
context=store, # Data storage
178182
identity=identity, # server identify
179183
timeout=0.005, # waiting time for request to complete
@@ -190,8 +194,8 @@ def run_server():
190194
broadcast_enable=False, # treat unit_id 0 as broadcast address,
191195
strict=True, # use strict timing, t1.5 for Modbus RTU
192196
)
193-
elif server == "tls":
194-
StartTlsServer(
197+
elif server_id == "tls":
198+
server = StartTlsServer(
195199
context=store, # Data storage
196200
host="localhost", # define tcp address where to connect to.
197201
port=port, # on which port
@@ -211,6 +215,7 @@ def run_server():
211215
# TBD timeout=1, # waiting time for request to complete
212216
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
213217
)
218+
return server
214219

215220

216221
# --------------------------------------------------------------------------- #
@@ -284,11 +289,10 @@ def get_commandline():
284289
if not args.framer:
285290
args.framer = comm_defaults[args.comm][0]
286291
args.port = args.port or comm_defaults[args.comm][1]
287-
if args.comm != "serial":
288-
args.port = int(args.port)
289292
args.framer = framers[args.framer]
290293
return args
291294

292295

293296
if __name__ == "__main__":
294-
run_server()
297+
server = run_server()
298+
server.shutdown()

pymodbus/client/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ def __init__(
9797
"""Initialize a client instance."""
9898
self.params = self._params()
9999
self.params.framer = framer
100-
self.params.timeout = timeout
101-
self.params.retries = retries
102-
self.params.retry_on_empty = retry_on_empty
103-
self.params.close_comm_on_error = close_comm_on_error
104-
self.params.strict = strict
105-
self.params.broadcast_enable = broadcast_enable
100+
self.params.timeout = int(timeout)
101+
self.params.retries = int(retries)
102+
self.params.retry_on_empty = bool(retry_on_empty)
103+
self.params.close_comm_on_error = bool(close_comm_on_error)
104+
self.params.strict = bool(strict)
105+
self.params.broadcast_enable = bool(broadcast_enable)
106106
self.params.kwargs = kwargs
107107

108108
# Common variables.

pymodbus/framer/rtu_framer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def sendPacket(self, message):
265265
:return:
266266
"""
267267
start = time.time()
268-
timeout = start + self.client.timeout
268+
timeout = start + self.client.params.timeout
269269
while self.client.state != ModbusTransactionState.IDLE:
270270
if self.client.state == ModbusTransactionState.TRANSACTION_COMPLETE:
271271
timestamp = round(time.time(), 6)
@@ -291,7 +291,7 @@ def sendPacket(self, message):
291291
elif self.client.state == ModbusTransactionState.RETRYING:
292292
# Simple lets settle down!!!
293293
# To check for higher baudrates
294-
time.sleep(self.client.timeout)
294+
time.sleep(self.client.params.timeout)
295295
break
296296
elif time.time() > timeout:
297297
_logger.debug(

0 commit comments

Comments
 (0)