Skip to content

Add retries to ModbusPDU class #2672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pymodbus/pdu/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self,
self.status: int = status
self.exception_code: int = 0
self.fut: asyncio.Future
self.retries: int = 0

def isError(self) -> bool:
"""Check if the error is a success or failure."""
Expand Down Expand Up @@ -67,7 +68,8 @@ def __str__(self) -> str:
f"count={self.count}, "
f"bits={self.bits!s}, "
f"registers={self.registers!s}, "
f"status={self.status!s})"
f"status={self.status!s}"
f"retries={self.retries})"
)

def get_response_pdu_size(self) -> int:
Expand Down
5 changes: 4 additions & 1 deletion pymodbus/transaction/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def sync_execute(self, no_response_expected: bool, request: ModbusPDU) -> Modbus
if no_response_expected:
return ExceptionResponse(0xff)
try:
return self.sync_get_response(request.dev_id, request.transaction_id)
response = self.sync_get_response(request.dev_id, request.transaction_id)
response.retries = count_retries
return response
except asyncio.exceptions.TimeoutError:
count_retries += 1
if self.count_until_disconnect < 0:
Expand Down Expand Up @@ -170,6 +172,7 @@ async def execute(self, no_response_expected: bool, request: ModbusPDU) -> Modbu
raise ModbusIOException(
f"ERROR: request ask for id={request.dev_id} but got id={response.dev_id}, CLOSING CONNECTION."
)
response.retries = count_retries
return response
except asyncio.exceptions.TimeoutError:
count_retries += 1
Expand Down