@@ -118,70 +118,74 @@ def execute(self, request):
118118 _logger .debug ("Clearing current Frame : - {}" .format (_buffer ))
119119 self .client .framer .resetFrame ()
120120
121- expected_response_length = None
122- if not isinstance (self .client .framer , ModbusSocketFramer ):
123- if hasattr (request , "get_response_pdu_size" ):
124- response_pdu_size = request .get_response_pdu_size ()
125- if isinstance (self .client .framer , ModbusAsciiFramer ):
126- response_pdu_size = response_pdu_size * 2
127- if response_pdu_size :
128- expected_response_length = self ._calculate_response_length (response_pdu_size )
129- if request .unit_id in self ._no_response_devices :
130- full = True
121+ if request .unit_id == 0 and self .client .broadcast_enable :
122+ response , last_exception = self ._transact (request , None )
123+ response = b'Broadcast write sent - no response expected'
131124 else :
132- full = False
133- c_str = str (self .client )
134- if "modbusudpclient" in c_str .lower ().strip ():
135- full = True
136- if not expected_response_length :
137- expected_response_length = Defaults .ReadSize
138- response , last_exception = self ._transact (request ,
139- expected_response_length ,
140- full = full
141- )
142- if not response and (
143- request .unit_id not in self ._no_response_devices ):
144- self ._no_response_devices .append (request .unit_id )
145- elif request .unit_id in self ._no_response_devices and response :
146- self ._no_response_devices .remove (request .unit_id )
147- if not response and self .retry_on_empty and retries :
148- while retries > 0 :
149- if hasattr (self .client , "state" ):
150- _logger .debug ("RESETTING Transaction state to "
151- "'IDLE' for retry" )
152- self .client .state = ModbusTransactionState .IDLE
153- _logger .debug ("Retry on empty - {}" .format (retries ))
154- response , last_exception = self ._transact (
155- request ,
156- expected_response_length
157- )
158- if not response :
159- retries -= 1
160- continue
161- # Remove entry
162- self ._no_response_devices .remove (request .unit_id )
163- break
164- addTransaction = partial (self .addTransaction ,
165- tid = request .transaction_id )
166- self .client .framer .processIncomingPacket (response ,
167- addTransaction ,
168- request .unit_id )
169- response = self .getTransaction (request .transaction_id )
170- if not response :
171- if len (self .transactions ):
172- response = self .getTransaction (tid = 0 )
125+ expected_response_length = None
126+ if not isinstance (self .client .framer , ModbusSocketFramer ):
127+ if hasattr (request , "get_response_pdu_size" ):
128+ response_pdu_size = request .get_response_pdu_size ()
129+ if isinstance (self .client .framer , ModbusAsciiFramer ):
130+ response_pdu_size = response_pdu_size * 2
131+ if response_pdu_size :
132+ expected_response_length = self ._calculate_response_length (response_pdu_size )
133+ if request .unit_id in self ._no_response_devices :
134+ full = True
173135 else :
174- last_exception = last_exception or (
175- "No Response received from the remote unit"
176- "/Unable to decode response" )
177- response = ModbusIOException (last_exception ,
178- request .function_code )
179- if hasattr (self .client , "state" ):
180- _logger .debug ("Changing transaction state from "
181- "'PROCESSING REPLY' to "
182- "'TRANSACTION_COMPLETE'" )
183- self .client .state = (
184- ModbusTransactionState .TRANSACTION_COMPLETE )
136+ full = False
137+ c_str = str (self .client )
138+ if "modbusudpclient" in c_str .lower ().strip ():
139+ full = True
140+ if not expected_response_length :
141+ expected_response_length = Defaults .ReadSize
142+ response , last_exception = self ._transact (request ,
143+ expected_response_length ,
144+ full = full
145+ )
146+ if not response and (
147+ request .unit_id not in self ._no_response_devices ):
148+ self ._no_response_devices .append (request .unit_id )
149+ elif request .unit_id in self ._no_response_devices and response :
150+ self ._no_response_devices .remove (request .unit_id )
151+ if not response and self .retry_on_empty and retries :
152+ while retries > 0 :
153+ if hasattr (self .client , "state" ):
154+ _logger .debug ("RESETTING Transaction state to "
155+ "'IDLE' for retry" )
156+ self .client .state = ModbusTransactionState .IDLE
157+ _logger .debug ("Retry on empty - {}" .format (retries ))
158+ response , last_exception = self ._transact (
159+ request ,
160+ expected_response_length
161+ )
162+ if not response :
163+ retries -= 1
164+ continue
165+ # Remove entry
166+ self ._no_response_devices .remove (request .unit_id )
167+ break
168+ addTransaction = partial (self .addTransaction ,
169+ tid = request .transaction_id )
170+ self .client .framer .processIncomingPacket (response ,
171+ addTransaction ,
172+ request .unit_id )
173+ response = self .getTransaction (request .transaction_id )
174+ if not response :
175+ if len (self .transactions ):
176+ response = self .getTransaction (tid = 0 )
177+ else :
178+ last_exception = last_exception or (
179+ "No Response received from the remote unit"
180+ "/Unable to decode response" )
181+ response = ModbusIOException (last_exception ,
182+ request .function_code )
183+ if hasattr (self .client , "state" ):
184+ _logger .debug ("Changing transaction state from "
185+ "'PROCESSING REPLY' to "
186+ "'TRANSACTION_COMPLETE'" )
187+ self .client .state = (
188+ ModbusTransactionState .TRANSACTION_COMPLETE )
185189 return response
186190 except ModbusIOException as ex :
187191 # Handle decode errors in processIncomingPacket method
@@ -205,13 +209,20 @@ def _transact(self, packet, response_length, full=False):
205209 if _logger .isEnabledFor (logging .DEBUG ):
206210 _logger .debug ("SEND: " + hexlify_packets (packet ))
207211 size = self ._send (packet )
208- if size :
209- _logger .debug ("Changing transaction state from 'SENDING' "
210- "to 'WAITING FOR REPLY'" )
211- self .client .state = ModbusTransactionState .WAITING_FOR_REPLY
212- result = self ._recv (response_length , full )
213- if _logger .isEnabledFor (logging .DEBUG ):
214- _logger .debug ("RECV: " + hexlify_packets (result ))
212+ if response_length is not None :
213+ if size :
214+ _logger .debug ("Changing transaction state from 'SENDING' "
215+ "to 'WAITING FOR REPLY'" )
216+ self .client .state = ModbusTransactionState .WAITING_FOR_REPLY
217+ result = self ._recv (response_length , full )
218+ if _logger .isEnabledFor (logging .DEBUG ):
219+ _logger .debug ("RECV: " + hexlify_packets (result ))
220+ else :
221+ if size :
222+ _logger .debug ("Changing transaction state from 'SENDING' "
223+ "to 'TRANSACTION_COMPLETE'" )
224+ self .client .state = ModbusTransactionState .TRANSACTION_COMPLETE
225+ result = b''
215226 except (socket .error , ModbusIOException ,
216227 InvalidMessageReceivedException ) as msg :
217228 self .client .close ()
0 commit comments