Skip to content

Commit d3926fa

Browse files
committed
Minor enhancements + added tests for transaction.py
1 parent 5a38412 commit d3926fa

File tree

2 files changed

+149
-71
lines changed

2 files changed

+149
-71
lines changed

pymodbus/transaction.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,6 @@ def _calculate_exception_length(self):
9797

9898
return None
9999

100-
def _check_response(self, response):
101-
''' Checks if the response is a Modbus Exception.
102-
'''
103-
if isinstance(self.client.framer, ModbusSocketFramer):
104-
if len(response) >= 8 and byte2int(response[7]) > 128:
105-
return False
106-
elif isinstance(self.client.framer, ModbusAsciiFramer):
107-
if len(response) >= 5 and int(response[3:5], 16) > 128:
108-
return False
109-
elif isinstance(self.client.framer, (ModbusRtuFramer, ModbusBinaryFramer)):
110-
if len(response) >= 2 and byte2int(response[1]) > 128:
111-
return False
112-
113-
return True
114-
115100
def execute(self, request):
116101
''' Starts the producer to send the next request to
117102
consumer.write(Frame(request))
@@ -145,15 +130,15 @@ def execute(self, request):
145130
if "modbusudpclient" in c_str.lower().strip():
146131
full = True
147132
if not expected_response_length:
148-
expected_response_length = 1024
133+
expected_response_length = Defaults.ReadSize
149134
response, last_exception = self._transact(request,
150135
expected_response_length,
151136
full=full
152137
)
153138
if not response and (
154139
request.unit_id not in self._no_response_devices):
155140
self._no_response_devices.append(request.unit_id)
156-
elif request.unit_id in self._no_response_devices:
141+
elif request.unit_id in self._no_response_devices and response:
157142
self._no_response_devices.remove(request.unit_id)
158143
if not response and self.retry_on_empty and retries:
159144
while retries > 0:
@@ -169,6 +154,8 @@ def execute(self, request):
169154
if not response:
170155
retries -= 1
171156
continue
157+
# Remove entry
158+
self._no_response_devices.remove(request.unit_id)
172159
break
173160
addTransaction = partial(self.addTransaction,
174161
tid=request.transaction_id)
@@ -191,10 +178,11 @@ def execute(self, request):
191178
self.client.state = (
192179
ModbusTransactionState.TRANSACTION_COMPLETE)
193180
return response
194-
except Exception as ex:
181+
except ModbusIOException as ex:
182+
# Handle decode errors in processIncomingPacket method
195183
_logger.exception(ex)
196184
self.client.state = ModbusTransactionState.TRANSACTION_COMPLETE
197-
raise
185+
return ex
198186

199187
def _transact(self, packet, response_length, full=False):
200188
"""
@@ -246,6 +234,11 @@ def _recv(self, expected_response_length, full):
246234
min_size = expected_response_length
247235

248236
read_min = self.client.framer.recvPacket(min_size)
237+
if len(read_min) != min_size:
238+
raise InvalidMessageReceivedException(
239+
"Incomplete message received, expected at least %d bytes "
240+
"(%d received)" % (min_size, len(read_min))
241+
)
249242
if read_min:
250243
if isinstance(self.client.framer, ModbusSocketFramer):
251244
func_code = byte2int(read_min[-1])

0 commit comments

Comments
 (0)