Skip to content

Commit abdbeca

Browse files
authored
Fix sync serial client, loop. (#2510)
1 parent 692e59a commit abdbeca

File tree

6 files changed

+8
-118
lines changed

6 files changed

+8
-118
lines changed

pymodbus/client/base.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pymodbus.pdu import DecodePDU, ModbusPDU
1212
from pymodbus.transaction import TransactionManager
1313
from pymodbus.transport import CommParams
14-
from pymodbus.utilities import ModbusTransactionState
1514

1615

1716
class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusPDU]]):
@@ -44,7 +43,6 @@ def __init__(
4443
trace_pdu,
4544
trace_connect,
4645
)
47-
self.state = ModbusTransactionState.IDLE
4846

4947
@property
5048
def connected(self) -> bool:
@@ -146,7 +144,6 @@ def __init__(
146144
)
147145
self.reconnect_delay_current = self.comm_params.reconnect_delay or 0
148146
self.use_udp = False
149-
self.state = ModbusTransactionState.IDLE
150147
self.last_frame_end: float | None = 0
151148
self.silent_interval: float = 0
152149

@@ -191,15 +188,6 @@ def execute(self, no_response_expected: bool, request: ModbusPDU) -> ModbusPDU:
191188
# ----------------------------------------------------------------------- #
192189
# Internal methods
193190
# ----------------------------------------------------------------------- #
194-
def _start_send(self):
195-
"""Send request.
196-
197-
:meta private:
198-
"""
199-
if self.state != ModbusTransactionState.RETRYING:
200-
Log.debug('New Transaction state "SENDING"')
201-
self.state = ModbusTransactionState.SENDING
202-
203191
@abstractmethod
204192
def send(self, request: bytes, addr: tuple | None = None) -> int:
205193
"""Send request.

pymodbus/client/serial.py

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from pymodbus.logging import Log
1414
from pymodbus.pdu import ModbusPDU
1515
from pymodbus.transport import CommParams, CommType
16-
from pymodbus.utilities import ModbusTransactionState
1716

1817

1918
with contextlib.suppress(ImportError):
@@ -158,10 +157,6 @@ def run():
158157
Please refer to :ref:`Pymodbus internals` for advanced usage.
159158
"""
160159

161-
state = ModbusTransactionState.IDLE
162-
inter_byte_timeout: float = 0
163-
silent_interval: float = 0
164-
165160
def __init__( # pylint: disable=too-many-arguments
166161
self,
167162
port: str,
@@ -219,6 +214,8 @@ def __init__( # pylint: disable=too-many-arguments
219214
# Set a minimum of 1ms for high baudrates
220215
self._recv_interval = max(self._recv_interval, 0.001)
221216

217+
self.inter_byte_timeout: float = 0
218+
self.silent_interval: float = 0
222219
if baudrate > 19200:
223220
self.silent_interval = 1.75 / 1000 # ms
224221
else:
@@ -264,14 +261,9 @@ def _in_waiting(self):
264261
"""Return waiting bytes."""
265262
return getattr(self.socket, "in_waiting") if hasattr(self.socket, "in_waiting") else getattr(self.socket, "inWaiting")()
266263

267-
def _send(self, request: bytes) -> int: # pragma: no cover
268-
"""Send data on the underlying socket.
269-
270-
If receive buffer still holds some data then flush it.
271-
272-
Sleep if last send finished less than 3.5 character times ago.
273-
"""
274-
super()._start_send()
264+
def send(self, request: bytes, addr: tuple | None = None) -> int:
265+
"""Send data on the underlying socket."""
266+
_ = addr
275267
if not self.socket:
276268
raise ConnectionException(str(self))
277269
if request:
@@ -283,52 +275,6 @@ def _send(self, request: bytes) -> int: # pragma: no cover
283275
return size
284276
return 0
285277

286-
def send(self, request: bytes, addr: tuple | None = None) -> int: # pragma: no cover
287-
"""Send data on the underlying socket."""
288-
_ = addr
289-
start = time.time()
290-
if hasattr(self,"ctx"):
291-
timeout = start + self.ctx.comm_params.timeout_connect
292-
else:
293-
timeout = start + self.comm_params.timeout_connect
294-
while self.state != ModbusTransactionState.IDLE:
295-
if self.state == ModbusTransactionState.TRANSACTION_COMPLETE:
296-
timestamp = round(time.time(), 6)
297-
Log.debug(
298-
"Changing state to IDLE - Last Frame End - {} Current Time stamp - {}",
299-
self.last_frame_end,
300-
timestamp,
301-
)
302-
if self.last_frame_end:
303-
idle_time = self.idle_time()
304-
if round(timestamp - idle_time, 6) <= self.silent_interval:
305-
Log.debug(
306-
"Waiting for 3.5 char before next send - {} ms",
307-
self.silent_interval * 1000,
308-
)
309-
time.sleep(self.silent_interval)
310-
else:
311-
# Recovering from last error ??
312-
time.sleep(self.silent_interval)
313-
self.state = ModbusTransactionState.IDLE
314-
elif self.state == ModbusTransactionState.RETRYING:
315-
# Simple lets settle down!!!
316-
# To check for higher baudrates
317-
time.sleep(self.comm_params.timeout_connect)
318-
break
319-
elif time.time() > timeout:
320-
Log.debug(
321-
"Spent more time than the read time out, "
322-
"resetting the transaction to IDLE"
323-
)
324-
self.state = ModbusTransactionState.IDLE
325-
else:
326-
Log.debug("Sleeping")
327-
time.sleep(self.silent_interval)
328-
size = self._send(request)
329-
self.last_frame_end = round(time.time(), 6)
330-
return size
331-
332278
def _wait_for_data(self) -> int:
333279
"""Wait for data."""
334280
size = 0

pymodbus/client/tcp.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ async def run():
137137
Please refer to :ref:`Pymodbus internals` for advanced usage.
138138
"""
139139

140-
socket: socket.socket | None
141-
142140
def __init__( # pylint: disable=too-many-arguments
143141
self,
144142
host: str,
@@ -177,7 +175,7 @@ def __init__( # pylint: disable=too-many-arguments
177175
trace_pdu,
178176
trace_connect,
179177
)
180-
self.socket = None
178+
self.socket: socket.socket | None = None
181179

182180
@property
183181
def connected(self) -> bool:
@@ -217,7 +215,6 @@ def close(self):
217215
def send(self, request, addr: tuple | None = None):
218216
"""Send data on the underlying socket."""
219217
_ = addr
220-
super()._start_send()
221218
if not self.socket:
222219
raise ConnectionException(str(self))
223220
if request:

pymodbus/client/udp.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ async def run():
138138
Please refer to :ref:`Pymodbus internals` for advanced usage.
139139
"""
140140

141-
socket: socket.socket | None
142-
143141
def __init__( # pylint: disable=too-many-arguments
144142
self,
145143
host: str,
@@ -177,15 +175,15 @@ def __init__( # pylint: disable=too-many-arguments
177175
trace_pdu,
178176
trace_connect,
179177
)
180-
self.socket = None
178+
self.socket: socket.socket | None = None
181179

182180
@property
183181
def connected(self) -> bool:
184182
"""Connect internal."""
185183
return self.socket is not None
186184

187185
def connect(self):
188-
"""Connect to the modbus tcp server.
186+
"""Connect to the modbus udp server.
189187
190188
:meta private:
191189
"""
@@ -212,7 +210,6 @@ def send(self, request: bytes, addr: tuple | None = None) -> int:
212210
:meta private:
213211
"""
214212
_ = addr
215-
super()._start_send()
216213
if not self.socket:
217214
raise ConnectionException(str(self))
218215
if request:

pymodbus/utilities.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,6 @@
99
import struct
1010

1111

12-
class ModbusTransactionState: # pylint: disable=too-few-public-methods
13-
"""Modbus Client States."""
14-
15-
IDLE = 0
16-
SENDING = 1
17-
WAITING_FOR_REPLY = 2
18-
WAITING_TURNAROUND_DELAY = 3
19-
PROCESSING_REPLY = 4
20-
PROCESSING_ERROR = 5
21-
TRANSACTION_COMPLETE = 6
22-
RETRYING = 7
23-
NO_RESPONSE_STATE = 8
24-
25-
@classmethod
26-
def to_string(cls, state):
27-
"""Convert to string."""
28-
states = {
29-
ModbusTransactionState.IDLE: "IDLE",
30-
ModbusTransactionState.SENDING: "SENDING",
31-
ModbusTransactionState.WAITING_FOR_REPLY: "WAITING_FOR_REPLY",
32-
ModbusTransactionState.WAITING_TURNAROUND_DELAY: "WAITING_TURNAROUND_DELAY",
33-
ModbusTransactionState.PROCESSING_REPLY: "PROCESSING_REPLY",
34-
ModbusTransactionState.PROCESSING_ERROR: "PROCESSING_ERROR",
35-
ModbusTransactionState.TRANSACTION_COMPLETE: "TRANSACTION_COMPLETE",
36-
ModbusTransactionState.RETRYING: "RETRYING TRANSACTION",
37-
}
38-
return states.get(state, None)
39-
40-
4112
def dict_property(store, index):
4213
"""Create class properties from a dictionary.
4314

test/client/test_client.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from pymodbus.exceptions import ConnectionException, ModbusException
2020
from pymodbus.pdu import ModbusPDU
2121
from pymodbus.transport import CommParams, CommType
22-
from pymodbus.utilities import ModbusTransactionState
2322

2423

2524
BASE_PORT = 6500
@@ -543,14 +542,6 @@ def test_idle_time(self):
543542
client.last_frame_end = None
544543
assert not client.idle_time()
545544

546-
def test_start_send(self):
547-
"""Test idle_time()."""
548-
client = lib_client.ModbusTcpClient("127.0.0.1")
549-
client.state = ModbusTransactionState.IDLE
550-
client._start_send()
551-
client.state = ModbusTransactionState.RETRYING
552-
client._start_send()
553-
554545
def test_sync_block(self):
555546
"""Test idle_time()."""
556547
with lib_client.ModbusTcpClient("127.0.0.1") as client:

0 commit comments

Comments
 (0)