Skip to content

Commit

Permalink
Client doc, add common methods (base).
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Sep 30, 2024
1 parent 7abd1d7 commit 7ff7d52
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
Binary file modified doc/source/_static/examples.tgz
Binary file not shown.
Binary file modified doc/source/_static/examples.zip
Binary file not shown.
18 changes: 16 additions & 2 deletions doc/source/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ Pymodbus offers clients with transport different protocols and different framers
- ASCII
- RTU
- RTU_OVER_TCP
- Socket
- SOCKET
- TLS
* - Serial (RS-485)
* - SERIAL (RS-485)
- Yes
- Yes
- No
Expand Down Expand Up @@ -248,6 +248,20 @@ There are a client class for each type of communication and for asynchronous/syn
- :mod:`AsyncModbusUdpClient`
- :mod:`ModbusUdpClient`

Client common
^^^^^^^^^^^^^
Some methods are common to all client:

.. autoclass:: pymodbus.client.base.ModbusBaseClient
:members:
:member-order: bysource
:show-inheritance:

.. autoclass:: pymodbus.client.base.ModbusBaseSyncClient
:members:
:member-order: bysource
:show-inheritance:

Client serial
^^^^^^^^^^^^^
.. autoclass:: pymodbus.client.AsyncModbusSerialClient
Expand Down
31 changes: 25 additions & 6 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def __init__(
on_connect_callback: Callable[[bool], None] | None,
comm_params: CommParams | None = None,
) -> None:
"""Initialize a client instance."""
"""Initialize a client instance.
:meta private:
"""
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
if comm_params:
self.comm_params = comm_params
Expand Down Expand Up @@ -99,13 +102,18 @@ def execute(self, request: ModbusRequest):
:param request: The request to process
:returns: The result of the request execution
:raises ConnectionException: Check exception text.
:meta private:
"""
if not self.ctx.transport:
raise ConnectionException(f"Not connected[{self!s}]")
return self.async_execute(request)

async def async_execute(self, request) -> ModbusResponse:
"""Execute requests asynchronously."""
"""Execute requests asynchronously.
:meta private:
"""
request.transaction_id = self.ctx.transaction.getNextTID()
packet = self.ctx.framer.buildPacket(request)

Expand Down Expand Up @@ -134,7 +142,10 @@ async def async_execute(self, request) -> ModbusResponse:
return resp # type: ignore[return-value]

def build_response(self, request: ModbusRequest):
"""Return a deferred response for the current request."""
"""Return a deferred response for the current request.
:meta private:
"""
my_future: asyncio.Future = asyncio.Future()
request.fut = my_future
if not self.ctx.transport:
Expand Down Expand Up @@ -179,7 +190,10 @@ def __init__(
retries: int,
comm_params: CommParams | None = None,
) -> None:
"""Initialize a client instance."""
"""Initialize a client instance.
:meta private:
"""
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
if comm_params:
self.comm_params = comm_params
Expand All @@ -205,7 +219,7 @@ def __init__(
# Client external interface
# ----------------------------------------------------------------------- #
def register(self, custom_response_class: ModbusResponse) -> None:
"""Register a custom response class with the decoder (call **sync**).
"""Register a custom response class with the decoder.
:param custom_response_class: (optional) Modbus response class.
:raises MessageRegisterException: Check exception text.
Expand All @@ -231,6 +245,8 @@ def execute(self, request: ModbusRequest) -> ModbusResponse:
:param request: The request to process
:returns: The result of the request execution
:raises ConnectionException: Check exception text.
:meta private:
"""
if not self.connect():
raise ConnectionException(f"Failed to connect[{self!s}]")
Expand Down Expand Up @@ -264,7 +280,10 @@ def recv(self, size: int | None) -> bytes:

@classmethod
def get_address_family(cls, address):
"""Get the correct address family."""
"""Get the correct address family.
:meta private:
"""
try:
_ = socket.inet_pton(socket.AF_INET6, address)
except OSError: # not a valid ipv6 address
Expand Down

0 comments on commit 7ff7d52

Please sign in to comment.