@@ -118,53 +118,53 @@ 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 == 0 :
130- full = True
131- expected_response_length = 0
132- elif request .unit_id in self ._no_response_devices :
133- 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'
134124 else :
135- full = False
136- c_str = str (self .client )
137- if "modbusudpclient" in c_str .lower ().strip ():
138- full = True
139- if not expected_response_length :
140- expected_response_length = Defaults .ReadSize
141- response , last_exception = self ._transact (request ,
142- expected_response_length ,
143- full = full
144- )
145- if not response and (
146- request .unit_id not in self ._no_response_devices ):
147- self ._no_response_devices .append (request .unit_id )
148- elif request .unit_id in self ._no_response_devices and response :
149- self ._no_response_devices .remove (request .unit_id )
150- if not response and self .retry_on_empty and retries :
151- while retries > 0 :
152- if hasattr (self .client , "state" ):
153- _logger .debug ("RESETTING Transaction state to "
154- "'IDLE' for retry" )
155- self .client .state = ModbusTransactionState .IDLE
156- _logger .debug ("Retry on empty - {}" .format (retries ))
157- response , last_exception = self ._transact (
158- request ,
159- expected_response_length
160- )
161- if not response :
162- retries -= 1
163- continue
164- # Remove entry
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
135+ else :
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 :
165150 self ._no_response_devices .remove (request .unit_id )
166- break
167- if expected_response_length > 0 :
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
168168 addTransaction = partial (self .addTransaction ,
169169 tid = request .transaction_id )
170170 self .client .framer .processIncomingPacket (response ,
@@ -180,14 +180,12 @@ def execute(self, request):
180180 "/Unable to decode response" )
181181 response = ModbusIOException (last_exception ,
182182 request .function_code )
183- else :
184- _logger .debug ("No response expected when sending to broadcast address 0" )
185- if hasattr (self .client , "state" ):
186- _logger .debug ("Changing transaction state from "
187- "'PROCESSING REPLY' to "
188- "'TRANSACTION_COMPLETE'" )
189- self .client .state = (
190- ModbusTransactionState .TRANSACTION_COMPLETE )
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 )
191189 return response
192190 except ModbusIOException as ex :
193191 # Handle decode errors in processIncomingPacket method
@@ -211,13 +209,20 @@ def _transact(self, packet, response_length, full=False):
211209 if _logger .isEnabledFor (logging .DEBUG ):
212210 _logger .debug ("SEND: " + hexlify_packets (packet ))
213211 size = self ._send (packet )
214- if size :
215- _logger .debug ("Changing transaction state from 'SENDING' "
216- "to 'WAITING FOR REPLY'" )
217- self .client .state = ModbusTransactionState .WAITING_FOR_REPLY
218- result = self ._recv (response_length , full )
219- if _logger .isEnabledFor (logging .DEBUG ):
220- _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''
221226 except (socket .error , ModbusIOException ,
222227 InvalidMessageReceivedException ) as msg :
223228 self .client .close ()
0 commit comments