@@ -97,21 +97,6 @@ def _calculate_exception_length(self):
97
97
98
98
return None
99
99
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
-
115
100
def execute (self , request ):
116
101
''' Starts the producer to send the next request to
117
102
consumer.write(Frame(request))
@@ -145,15 +130,15 @@ def execute(self, request):
145
130
if "modbusudpclient" in c_str .lower ().strip ():
146
131
full = True
147
132
if not expected_response_length :
148
- expected_response_length = 1024
133
+ expected_response_length = Defaults . ReadSize
149
134
response , last_exception = self ._transact (request ,
150
135
expected_response_length ,
151
136
full = full
152
137
)
153
138
if not response and (
154
139
request .unit_id not in self ._no_response_devices ):
155
140
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 :
157
142
self ._no_response_devices .remove (request .unit_id )
158
143
if not response and self .retry_on_empty and retries :
159
144
while retries > 0 :
@@ -169,6 +154,8 @@ def execute(self, request):
169
154
if not response :
170
155
retries -= 1
171
156
continue
157
+ # Remove entry
158
+ self ._no_response_devices .remove (request .unit_id )
172
159
break
173
160
addTransaction = partial (self .addTransaction ,
174
161
tid = request .transaction_id )
@@ -191,10 +178,11 @@ def execute(self, request):
191
178
self .client .state = (
192
179
ModbusTransactionState .TRANSACTION_COMPLETE )
193
180
return response
194
- except Exception as ex :
181
+ except ModbusIOException as ex :
182
+ # Handle decode errors in processIncomingPacket method
195
183
_logger .exception (ex )
196
184
self .client .state = ModbusTransactionState .TRANSACTION_COMPLETE
197
- raise
185
+ return ex
198
186
199
187
def _transact (self , packet , response_length , full = False ):
200
188
"""
@@ -246,6 +234,11 @@ def _recv(self, expected_response_length, full):
246
234
min_size = expected_response_length
247
235
248
236
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
+ )
249
242
if read_min :
250
243
if isinstance (self .client .framer , ModbusSocketFramer ):
251
244
func_code = byte2int (read_min [- 1 ])
0 commit comments