Skip to content

Commit 59fc0e2

Browse files
authored
Add retries to ModbusPDU class (#2672)
1 parent cc01d1a commit 59fc0e2

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

pymodbus/pdu/pdu.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(self,
3838
self.status: int = status
3939
self.exception_code: int = 0
4040
self.fut: asyncio.Future
41+
self.retries: int = 0
4142

4243
def isError(self) -> bool:
4344
"""Check if the error is a success or failure."""
@@ -67,7 +68,8 @@ def __str__(self) -> str:
6768
f"count={self.count}, "
6869
f"bits={self.bits!s}, "
6970
f"registers={self.registers!s}, "
70-
f"status={self.status!s})"
71+
f"status={self.status!s}"
72+
f"retries={self.retries})"
7173
)
7274

7375
def get_response_pdu_size(self) -> int:

pymodbus/transaction/transaction.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ def sync_execute(self, no_response_expected: bool, request: ModbusPDU) -> Modbus
129129
if no_response_expected:
130130
return ExceptionResponse(0xff)
131131
try:
132-
return self.sync_get_response(request.dev_id, request.transaction_id)
132+
response = self.sync_get_response(request.dev_id, request.transaction_id)
133+
response.retries = count_retries
134+
return response
133135
except asyncio.exceptions.TimeoutError:
134136
count_retries += 1
135137
if self.count_until_disconnect < 0:
@@ -170,6 +172,7 @@ async def execute(self, no_response_expected: bool, request: ModbusPDU) -> Modbu
170172
raise ModbusIOException(
171173
f"ERROR: request ask for id={request.dev_id} but got id={response.dev_id}, CLOSING CONNECTION."
172174
)
175+
response.retries = count_retries
173176
return response
174177
except asyncio.exceptions.TimeoutError:
175178
count_retries += 1

0 commit comments

Comments
 (0)