Skip to content

Commit

Permalink
Ready to test.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Nov 11, 2024
1 parent 9a18b1a commit 0a39a4d
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions pymodbus/transaction/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio
from collections.abc import Callable

from pymodbus.exceptions import ModbusIOException
from pymodbus.exceptions import ConnectionException, ModbusIOException
from pymodbus.framer import FramerBase
from pymodbus.logging import Log
from pymodbus.pdu import ModbusPDU
Expand Down Expand Up @@ -58,7 +58,8 @@ async def execute(self, no_response_expected: bool, request) -> ModbusPDU | None
"""Execute requests asynchronously."""
if not self.transport:
Log.warning("Not connected, trying to connect!")
await self.connect()
if not await self.connect():
raise ConnectionException("Client cannot connect (automatic retry continuing) !!")
async with self._lock:
request.transaction_id = self.getNextTID()
if self.trace_send_pdu:
Expand All @@ -71,10 +72,10 @@ async def execute(self, no_response_expected: bool, request) -> ModbusPDU | None
self.send(packet)
if no_response_expected:
return None
response_future = asyncio.Future()
self.response_future = asyncio.Future()
try:
response = await asyncio.wait_for(
response_future, timeout=self.comm_params.timeout_connect
self.response_future, timeout=self.comm_params.timeout_connect
)
self.count_no_responses = 0
return response
Expand All @@ -95,6 +96,7 @@ def callback_new_connection(self):
def callback_connected(self) -> None:
"""Call when connection is succcesfull."""
self.count_no_responses = 0
self.next_tid = 0

def callback_disconnected(self, exc: Exception | None) -> None:
"""Call when connection is lost."""
Expand All @@ -111,21 +113,17 @@ def callback_data(self, data: bytes, _addr: tuple | None = None) -> int:
"""Handle received data."""
if self.trace_recv_packet:
data = self.trace_recv_packet(data)
return len(data)





used_len, pdu = self.framer.processIncomingFrame(data)
if pdu:
if self.trace_recv_pdu:
pdu = self.trace_recv_pdu(pdu)
self.response_future.set_result(pdu)
return used_len

def getNextTID(self) -> int:
"""Retrieve the next transaction identifier."""
if self.next_tid < 65000:
self.next_tid += 1
else:
if self.next_tid > 65000:
self.next_tid = 1
else:
self.next_tid += 1
return self.next_tid

def reset(self):
"""Reset the transaction identifier."""
self.next_tid = 0

0 comments on commit 0a39a4d

Please sign in to comment.