Skip to content

Commit 5b3d564

Browse files
committed
remove kwargs client.
1 parent fbaf05d commit 5b3d564

File tree

8 files changed

+74
-59
lines changed

8 files changed

+74
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ prof/
1919
/pymodbus.egg-info/
2020
venv
2121
downloaded_files/
22+
pymodbus.log

pymodbus/client/base.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusResponse]]):
3737
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
3838
:param on_connect_callback: Will be called when connected/disconnected (bool parameter)
3939
:param no_resend_on_retry: Do not resend request when retrying due to missing response.
40-
:param comm_type: Type of communication (set by interface class)
4140
:param kwargs: Experimental parameters.
4241
4342
.. tip::
@@ -60,30 +59,30 @@ def __init__( # pylint: disable=too-many-arguments
6059
reconnect_delay_max: float = 300,
6160
on_connect_callback: Callable[[bool], None] | None = None,
6261
no_resend_on_retry: bool = False,
63-
comm_type: CommType | None = None,
6462
source_address: tuple[str, int] | None = None,
63+
64+
comm_params: CommParams | None = None,
6565
**kwargs: Any,
6666
) -> None:
6767
"""Initialize a client instance."""
6868
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
69+
if comm_params:
70+
self.comm_params = comm_params
71+
self.comm_params.comm_name="comm"
72+
self.comm_params.source_address=source_address
73+
self.comm_params.reconnect_delay=reconnect_delay
74+
self.comm_params.reconnect_delay_max=reconnect_delay_max
75+
self.comm_params.timeout_connect=timeout
76+
self.comm_params.host=kwargs.get("host", None)
77+
self.comm_params.port=kwargs.get("port", 0)
78+
self.comm_params.baudrate=kwargs.get("baudrate", None)
79+
self.comm_params.bytesize=kwargs.get("bytesize", None)
80+
self.comm_params.parity=kwargs.get("parity", None)
81+
self.comm_params.stopbits=kwargs.get("stopbits", None)
82+
self.comm_params.handle_local_echo=kwargs.get("handle_local_echo", False)
6983
self.ctx = ModbusClientProtocol(
7084
framer,
71-
CommParams(
72-
comm_type=comm_type,
73-
comm_name="comm",
74-
source_address=source_address,
75-
reconnect_delay=reconnect_delay,
76-
reconnect_delay_max=reconnect_delay_max,
77-
timeout_connect=timeout,
78-
host=kwargs.get("host", None),
79-
port=kwargs.get("port", 0),
80-
sslctx=kwargs.get("sslctx", None),
81-
baudrate=kwargs.get("baudrate", None),
82-
bytesize=kwargs.get("bytesize", None),
83-
parity=kwargs.get("parity", None),
84-
stopbits=kwargs.get("stopbits", None),
85-
handle_local_echo=kwargs.get("handle_local_echo", False),
86-
),
85+
self.comm_params,
8786
on_connect_callback,
8887
)
8988
self.no_resend_on_retry = no_resend_on_retry
@@ -276,26 +275,28 @@ def __init__( # pylint: disable=too-many-arguments
276275
no_resend_on_retry: bool = False,
277276
comm_type: CommType | None = None,
278277
source_address: tuple[str, int] | None = None,
278+
comm_params: CommParams | None = None,
279279
**kwargs: Any,
280280
) -> None:
281281
"""Initialize a client instance."""
282282
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
283-
self.comm_params = CommParams(
284-
comm_type=comm_type,
285-
comm_name="comm",
286-
source_address=source_address,
287-
reconnect_delay=reconnect_delay,
288-
reconnect_delay_max=reconnect_delay_max,
289-
timeout_connect=timeout,
290-
host=kwargs.get("host", None),
291-
port=kwargs.get("port", 0),
292-
sslctx=kwargs.get("sslctx", None),
293-
baudrate=kwargs.get("baudrate", None),
294-
bytesize=kwargs.get("bytesize", None),
295-
parity=kwargs.get("parity", None),
296-
stopbits=kwargs.get("stopbits", None),
297-
handle_local_echo=kwargs.get("handle_local_echo", False),
298-
)
283+
if comm_params:
284+
self.comm_params = comm_params
285+
if comm_type:
286+
self.comm_params.comm_type=comm_type
287+
self.comm_params.comm_name="comm"
288+
self.comm_params.source_address=source_address
289+
self.comm_params.reconnect_delay=reconnect_delay
290+
self.comm_params.reconnect_delay_max=reconnect_delay_max
291+
self.comm_params.timeout_connect=timeout
292+
self.comm_params.host=kwargs.get("host", None)
293+
self.comm_params.port=kwargs.get("port", 0)
294+
self.comm_params.sslctx=kwargs.get("sslctx", None)
295+
self.comm_params.baudrate=kwargs.get("baudrate", None)
296+
self.comm_params.bytesize=kwargs.get("bytesize", None)
297+
self.comm_params.parity=kwargs.get("parity", None)
298+
self.comm_params.stopbits=kwargs.get("stopbits", None)
299+
self.comm_params.handle_local_echo=kwargs.get("handle_local_echo", False)
299300
self.params = self._params()
300301
self.params.retries = int(retries)
301302
self.params.retry_on_empty = bool(retry_on_empty)

pymodbus/client/serial.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pymodbus.exceptions import ConnectionException
1010
from pymodbus.framer import FramerType
1111
from pymodbus.logging import Log
12-
from pymodbus.transport import CommType
12+
from pymodbus.transport import CommParams, CommType
1313
from pymodbus.utilities import ModbusTransactionState
1414

1515

@@ -81,10 +81,10 @@ def __init__(
8181
"Serial client requires pyserial "
8282
'Please install with "pip install pyserial" and try again.'
8383
)
84+
self.comm_params = CommParams(comm_type=CommType.SERIAL)
8485
ModbusBaseClient.__init__(
8586
self,
8687
framer,
87-
comm_type=CommType.SERIAL,
8888
host=port,
8989
baudrate=baudrate,
9090
bytesize=bytesize,
@@ -159,9 +159,9 @@ def __init__(
159159
**kwargs: Any,
160160
) -> None:
161161
"""Initialize Modbus Serial Client."""
162+
self.comm_params = CommParams(comm_type=CommType.SERIAL)
162163
super().__init__(
163164
framer,
164-
comm_type=CommType.SERIAL,
165165
host=port,
166166
baudrate=baudrate,
167167
bytesize=bytesize,
@@ -171,9 +171,7 @@ def __init__(
171171
)
172172
self.socket: serial.Serial | None = None
173173
self.strict = bool(strict)
174-
175174
self.last_frame_end = None
176-
177175
self._t0 = float(1 + bytesize + stopbits) / baudrate
178176

179177
# Check every 4 bytes / 2 registers if the reading is ready

pymodbus/client/tcp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pymodbus.exceptions import ConnectionException
1111
from pymodbus.framer import FramerType
1212
from pymodbus.logging import Log
13-
from pymodbus.transport import CommType
13+
from pymodbus.transport import CommParams, CommType
1414

1515

1616
class AsyncModbusTcpClient(ModbusBaseClient):
@@ -63,8 +63,8 @@ def __init__(
6363
**kwargs: Any,
6464
) -> None:
6565
"""Initialize Asyncio Modbus TCP Client."""
66-
if "comm_type" not in kwargs:
67-
kwargs["comm_type"] = CommType.TCP
66+
if not hasattr(self,"comm_params"):
67+
self.comm_params = CommParams(comm_type=CommType.TCP)
6868
if source_address:
6969
kwargs["source_address"] = source_address
7070
ModbusBaseClient.__init__(
@@ -132,8 +132,8 @@ def __init__(
132132
**kwargs: Any,
133133
) -> None:
134134
"""Initialize Modbus TCP Client."""
135-
if "comm_type" not in kwargs:
136-
kwargs["comm_type"] = CommType.TCP
135+
if not hasattr(self,"comm_params"):
136+
self.comm_params = CommParams(comm_type=CommType.TCP)
137137
super().__init__(
138138
framer,
139139
host=host,

pymodbus/client/tls.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ def __init__(
6262
**kwargs: Any,
6363
):
6464
"""Initialize Asyncio Modbus TLS Client."""
65+
self.comm_params = CommParams(comm_type=CommType.TLS)
6566
AsyncModbusTcpClient.__init__(
6667
self,
6768
host,
6869
port=port,
6970
framer=framer,
70-
comm_type=CommType.TLS,
7171
sslctx=sslctx,
7272
**kwargs,
7373
)
@@ -149,8 +149,12 @@ def __init__(
149149
**kwargs: Any,
150150
):
151151
"""Initialize Modbus TLS Client."""
152+
self.comm_params = CommParams(
153+
comm_type=CommType.TLS,
154+
sslctx=sslctx,
155+
)
152156
super().__init__(
153-
host, comm_type=CommType.TLS, port=port, framer=framer, **kwargs
157+
host, port=port, framer=framer, **kwargs
154158
)
155159
self.sslctx = sslctx
156160
self.server_hostname = server_hostname

pymodbus/client/udp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pymodbus.exceptions import ConnectionException
99
from pymodbus.framer import FramerType
1010
from pymodbus.logging import Log
11-
from pymodbus.transport import CommType
11+
from pymodbus.transport import CommParams, CommType
1212

1313

1414
DGRAM_TYPE = socket.SOCK_DGRAM
@@ -62,10 +62,10 @@ def __init__(
6262
**kwargs: Any,
6363
) -> None:
6464
"""Initialize Asyncio Modbus UDP Client."""
65+
self.comm_params = CommParams(comm_type=CommType.UDP)
6566
ModbusBaseClient.__init__(
6667
self,
6768
framer,
68-
comm_type=CommType.UDP,
6969
host=host,
7070
port=port,
7171
**kwargs,
@@ -130,11 +130,11 @@ def __init__(
130130
**kwargs: Any,
131131
) -> None:
132132
"""Initialize Modbus UDP Client."""
133+
self.comm_params = CommParams(comm_type=CommType.UDP)
133134
super().__init__(
134135
framer,
135136
port=port,
136137
host=host,
137-
comm_type=CommType.UDP,
138138
**kwargs,
139139
)
140140
self.params.source_address = source_address

test/framers/test_old_framers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ModbusTlsFramer,
1515
)
1616
from pymodbus.pdu.bit_read_message import ReadCoilsRequest
17-
from pymodbus.transport import CommType
17+
from pymodbus.transport import CommParams, CommType
1818
from pymodbus.utilities import ModbusTransactionState
1919

2020

@@ -330,6 +330,7 @@ async def test_send_packet(self, rtu_framer):
330330
host="localhost",
331331
port=BASE_PORT + 1,
332332
CommType=CommType.TCP,
333+
comm_params=CommParams(),
333334
)
334335
client.state = ModbusTransactionState.TRANSACTION_COMPLETE
335336
client.silent_interval = 1

test/sub_client/test_client.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from pymodbus.datastore.store import ModbusSequentialDataBlock
2323
from pymodbus.exceptions import ConnectionException, ModbusException, ModbusIOException
2424
from pymodbus.pdu import ModbusRequest
25-
from pymodbus.transport import CommType
25+
from pymodbus.transport import CommParams, CommType
2626

2727

2828
BASE_PORT = 6500
@@ -281,6 +281,7 @@ async def test_client_modbusbaseclient():
281281
host="localhost",
282282
port=BASE_PORT + 1,
283283
CommType=CommType.TCP,
284+
comm_params=CommParams(),
284285
)
285286
client.register(pdu_bit_read.ReadCoilsResponse)
286287
assert str(client)
@@ -316,6 +317,7 @@ async def test_client_base_async():
316317
host="localhost",
317318
port=BASE_PORT + 2,
318319
CommType=CommType.TCP,
320+
comm_params=CommParams(),
319321
) as client:
320322
str(client)
321323
p_connect.return_value = asyncio.Future()
@@ -327,7 +329,8 @@ async def test_client_base_async():
327329
@pytest.mark.skip()
328330
async def test_client_protocol_receiver():
329331
"""Test the client protocol data received."""
330-
base = ModbusBaseClient(FramerType.SOCKET)
332+
base = ModbusBaseClient(FramerType.SOCKET, comm_params=CommParams(),
333+
)
331334
transport = mock.MagicMock()
332335
base.ctx.connection_made(transport)
333336
assert base.transport == transport
@@ -349,7 +352,8 @@ async def test_client_protocol_receiver():
349352
@pytest.mark.skip()
350353
async def test_client_protocol_response():
351354
"""Test the udp client protocol builds responses."""
352-
base = ModbusBaseClient(FramerType.SOCKET)
355+
base = ModbusBaseClient(FramerType.SOCKET, comm_params=CommParams(),
356+
)
353357
response = base.build_response(0x00) # pylint: disable=protected-access
354358
excp = response.exception()
355359
assert isinstance(excp, ConnectionException)
@@ -363,7 +367,8 @@ async def test_client_protocol_response():
363367
async def test_client_protocol_handler():
364368
"""Test the client protocol handles responses."""
365369
base = ModbusBaseClient(
366-
FramerType.ASCII, host="localhost", port=+3, CommType=CommType.TCP
370+
FramerType.ASCII, host="localhost", port=+3, CommType=CommType.TCP, comm_params=CommParams(),
371+
367372
)
368373
transport = mock.MagicMock()
369374
base.ctx.connection_made(transport=transport)
@@ -411,7 +416,8 @@ def close(self):
411416

412417
async def test_client_protocol_execute():
413418
"""Test the client protocol execute method."""
414-
base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1")
419+
base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", comm_params=CommParams(),
420+
)
415421
request = pdu_bit_read.ReadCoilsRequest(1, 1)
416422
transport = MockTransport(base, request)
417423
base.ctx.connection_made(transport=transport)
@@ -422,7 +428,8 @@ async def test_client_protocol_execute():
422428

423429
async def test_client_execute_broadcast():
424430
"""Test the client protocol execute method."""
425-
base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1")
431+
base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", comm_params=CommParams(),
432+
)
426433
base.broadcast_enable = True
427434
request = pdu_bit_read.ReadCoilsRequest(1, 1)
428435
transport = MockTransport(base, request)
@@ -432,7 +439,8 @@ async def test_client_execute_broadcast():
432439

433440
async def test_client_protocol_retry():
434441
"""Test the client protocol execute method with retries."""
435-
base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1)
442+
base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1, comm_params=CommParams(),
443+
)
436444
request = pdu_bit_read.ReadCoilsRequest(1, 1)
437445
transport = MockTransport(base, request, retries=2)
438446
base.ctx.connection_made(transport=transport)
@@ -445,7 +453,8 @@ async def test_client_protocol_retry():
445453

446454
async def test_client_protocol_timeout():
447455
"""Test the client protocol execute method with timeout."""
448-
base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1, retries=2)
456+
base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1, retries=2, comm_params=CommParams(),
457+
)
449458
# Avoid creating do_reconnect() task
450459
base.ctx.connection_lost = mock.MagicMock()
451460
request = pdu_bit_read.ReadCoilsRequest(1, 1)
@@ -645,7 +654,8 @@ def test_client_mixin_convert_fail():
645654

646655
async def test_client_build_response():
647656
"""Test fail of build_response."""
648-
client = ModbusBaseClient(FramerType.RTU)
657+
client = ModbusBaseClient(FramerType.RTU, comm_params=CommParams(),
658+
)
649659
with pytest.raises(ConnectionException):
650660
await client.build_response(ModbusRequest(0, 0, 0, False))
651661

0 commit comments

Comments
 (0)