Skip to content

Commit

Permalink
remove kwargs client.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Jul 22, 2024
1 parent 385bbc5 commit 086f166
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ prof/
/pymodbus.egg-info/
venv
downloaded_files/
pymodbus.log
68 changes: 25 additions & 43 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@
class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusResponse]]):
"""**ModbusBaseClient**.
Fixed parameters:
:param framer: Framer enum name
Optional parameters:
:param timeout: Timeout for a request, in seconds.
:param retries: Max number of retries per request.
:param retry_on_empty: Retry on empty response.
timeout: Timeout for a request, in seconds.
retries: Max number of retries per request.
retry_on_empty: Retry on empty response.
:param broadcast_enable: True to treat id 0 as broadcast address.
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param on_connect_callback: Will be called when connected/disconnected (bool parameter)
:param no_resend_on_retry: Do not resend request when retrying due to missing response.
:param comm_type: Type of communication (set by interface class)
:param kwargs: Experimental parameters.
.. tip::
Expand All @@ -60,30 +53,24 @@ def __init__( # pylint: disable=too-many-arguments
reconnect_delay_max: float = 300,
on_connect_callback: Callable[[bool], None] | None = None,
no_resend_on_retry: bool = False,
comm_type: CommType | None = None,
source_address: tuple[str, int] | None = None,

comm_params: CommParams | None = None,
**kwargs: Any,
) -> None:
"""Initialize a client instance."""
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
if comm_params:
self.comm_params = comm_params
self.comm_params.comm_name="comm"
self.comm_params.source_address=source_address
self.comm_params.reconnect_delay=reconnect_delay
self.comm_params.reconnect_delay_max=reconnect_delay_max
self.comm_params.timeout_connect=timeout
self.comm_params.port=kwargs.get("port", 0)
self.ctx = ModbusClientProtocol(
framer,
CommParams(
comm_type=comm_type,
comm_name="comm",
source_address=source_address,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
host=kwargs.get("host", None),
port=kwargs.get("port", 0),
sslctx=kwargs.get("sslctx", None),
baudrate=kwargs.get("baudrate", None),
bytesize=kwargs.get("bytesize", None),
parity=kwargs.get("parity", None),
stopbits=kwargs.get("stopbits", None),
handle_local_echo=kwargs.get("handle_local_echo", False),
),
self.comm_params,
on_connect_callback,
)
self.no_resend_on_retry = no_resend_on_retry
Expand Down Expand Up @@ -276,26 +263,21 @@ def __init__( # pylint: disable=too-many-arguments
no_resend_on_retry: bool = False,
comm_type: CommType | None = None,
source_address: tuple[str, int] | None = None,
comm_params: CommParams | None = None,
**kwargs: Any,
) -> None:
"""Initialize a client instance."""
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
self.comm_params = CommParams(
comm_type=comm_type,
comm_name="comm",
source_address=source_address,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
host=kwargs.get("host", None),
port=kwargs.get("port", 0),
sslctx=kwargs.get("sslctx", None),
baudrate=kwargs.get("baudrate", None),
bytesize=kwargs.get("bytesize", None),
parity=kwargs.get("parity", None),
stopbits=kwargs.get("stopbits", None),
handle_local_echo=kwargs.get("handle_local_echo", False),
)
if comm_params:
self.comm_params = comm_params
if comm_type:
self.comm_params.comm_type=comm_type
self.comm_params.comm_name="comm"
self.comm_params.source_address=source_address
self.comm_params.reconnect_delay=reconnect_delay
self.comm_params.reconnect_delay_max=reconnect_delay_max
self.comm_params.timeout_connect=timeout
self.comm_params.port=kwargs.get("port", 0)
self.params = self._params()
self.params.retries = int(retries)
self.params.retry_on_empty = bool(retry_on_empty)
Expand Down
35 changes: 23 additions & 12 deletions pymodbus/client/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pymodbus.exceptions import ConnectionException
from pymodbus.framer import FramerType
from pymodbus.logging import Log
from pymodbus.transport import CommType
from pymodbus.transport import CommParams, CommType
from pymodbus.utilities import ModbusTransactionState


Expand All @@ -32,15 +32,15 @@ class AsyncModbusSerialClient(ModbusBaseClient):
Optional parameters:
:param framer: Framer name, default FramerType.RTU
:param baudrate: Bits per second.
:param bytesize: Number of bits per byte 7-8.
:param parity: 'E'ven, 'O'dd or 'N'one
:param stopbits: Number of stop bits 1, 1.5, 2.
:param handle_local_echo: Discard local echo from dongle.
Common optional parameters:
----- OLD ------
:param framer: Framer enum name
:param timeout: Timeout for a request, in seconds.
:param retries: Max number of retries per request.
:param retry_on_empty: Retry on empty response.
Expand Down Expand Up @@ -73,6 +73,9 @@ def __init__(
bytesize: int = 8,
parity: str = "N",
stopbits: int = 1,
handle_local_echo: bool = False,

# ----- OLD ------
**kwargs: Any,
) -> None:
"""Initialize Asyncio Modbus Serial Client."""
Expand All @@ -81,15 +84,18 @@ def __init__(
"Serial client requires pyserial "
'Please install with "pip install pyserial" and try again.'
)
ModbusBaseClient.__init__(
self,
framer,
self.comm_params = CommParams(
comm_type=CommType.SERIAL,
host=port,
baudrate=baudrate,
bytesize=bytesize,
parity=parity,
stopbits=stopbits,
handle_local_echo=handle_local_echo,
)
ModbusBaseClient.__init__(
self,
framer,
**kwargs,
)

Expand All @@ -107,15 +113,16 @@ class ModbusSerialClient(ModbusBaseSyncClient):
Optional parameters:
:param framer: Framer name, default FramerType.RTU
:param baudrate: Bits per second.
:param bytesize: Number of bits per byte 7-8.
:param parity: 'E'ven, 'O'dd or 'N'one
:param stopbits: Number of stop bits 0-2.
:param handle_local_echo: Discard local echo from dongle.
Common optional parameters:
:param framer: Framer enum name
----- OLD ------
:param timeout: Timeout for a request, in seconds.
:param retries: Max number of retries per request.
:param retry_on_empty: Retry on empty response.
Expand Down Expand Up @@ -155,25 +162,29 @@ def __init__(
bytesize: int = 8,
parity: str = "N",
stopbits: int = 1,
handle_local_echo: bool = False,

# ----- OLD ------
strict: bool = True,
**kwargs: Any,
) -> None:
"""Initialize Modbus Serial Client."""
super().__init__(
framer,
self.comm_params = CommParams(
comm_type=CommType.SERIAL,
host=port,
baudrate=baudrate,
bytesize=bytesize,
parity=parity,
stopbits=stopbits,
handle_local_echo=handle_local_echo,
)
super().__init__(
framer,
**kwargs,
)
self.socket: serial.Serial | None = None
self.strict = bool(strict)

self.last_frame_end = None

self._t0 = float(1 + bytesize + stopbits) / baudrate

# Check every 4 bytes / 2 registers if the reading is ready
Expand Down
37 changes: 22 additions & 15 deletions pymodbus/client/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pymodbus.exceptions import ConnectionException
from pymodbus.framer import FramerType
from pymodbus.logging import Log
from pymodbus.transport import CommType
from pymodbus.transport import CommParams, CommType


class AsyncModbusTcpClient(ModbusBaseClient):
Expand All @@ -22,12 +22,13 @@ class AsyncModbusTcpClient(ModbusBaseClient):
Optional parameters:
:param framer: Framer name, default FramerType.SOCKET
----- OLD ------
:param port: Port used for communication
:param source_address: source address of client
Common optional parameters:
:param framer: Framer enum name
:param timeout: Timeout for a request, in seconds.
:param retries: Max number of retries per request.
:param retry_on_empty: Retry on empty response.
Expand Down Expand Up @@ -57,20 +58,23 @@ async def run():
def __init__(
self,
host: str,
port: int = 502,
framer: FramerType = FramerType.SOCKET,
# ----- OLD ------
port: int = 502,
source_address: tuple[str, int] | None = None,
**kwargs: Any,
) -> None:
"""Initialize Asyncio Modbus TCP Client."""
if "comm_type" not in kwargs:
kwargs["comm_type"] = CommType.TCP
if not hasattr(self,"comm_params"):
self.comm_params = CommParams(
comm_type=CommType.TCP,
host=host,
)
if source_address:
kwargs["source_address"] = source_address
ModbusBaseClient.__init__(
self,
framer,
host=host,
port=port,
**kwargs,
)
Expand All @@ -89,12 +93,13 @@ class ModbusTcpClient(ModbusBaseSyncClient):
Optional parameters:
:param framer: Framer name, default FramerType.SOCKET
----- OLD ------
:param port: Port used for communication
:param source_address: source address of client
Common optional parameters:
:param framer: Framer enum name
:param timeout: Timeout for a request, in seconds.
:param retries: Max number of retries per request.
:param retry_on_empty: Retry on empty response.
Expand Down Expand Up @@ -126,17 +131,19 @@ async def run():
def __init__(
self,
host: str,
port: int = 502,
framer: FramerType = FramerType.SOCKET,
# ----- OLD ------
port: int = 502,
source_address: tuple[str, int] | None = None,
**kwargs: Any,
) -> None:
"""Initialize Modbus TCP Client."""
if "comm_type" not in kwargs:
kwargs["comm_type"] = CommType.TCP
if not hasattr(self,"comm_params"):
self.comm_params = CommParams(
comm_type=CommType.TCP,
host=host)
super().__init__(
framer,
host=host,
port=port,
**kwargs,
)
Expand Down
Loading

0 comments on commit 086f166

Please sign in to comment.