@@ -112,14 +112,17 @@ def execute(self, request):
112
112
)
113
113
retries = self .retries
114
114
request .transaction_id = self .getNextTID ()
115
- _logger .debug ("Running transaction %d" % request .transaction_id )
115
+ _logger .debug ("Running transaction "
116
+ "{}" .format (request .transaction_id ))
116
117
_buffer = hexlify_packets (self .client .framer ._buffer )
117
118
if _buffer :
118
- _logger .debug ("Clearing current Frame : - {}" .format (_buffer ))
119
+ _logger .debug ("Clearing current Frame "
120
+ ": - {}" .format (_buffer ))
119
121
self .client .framer .resetFrame ()
120
-
121
- if request .unit_id == 0 and self .client .broadcast_enable :
122
- response , last_exception = self ._transact (request , None )
122
+ broadcast = (self .client .broadcast_enable
123
+ and request .unit_id == 0 )
124
+ if broadcast :
125
+ self ._transact (request , None , broadcast = True )
123
126
response = b'Broadcast write sent - no response expected'
124
127
else :
125
128
expected_response_length = None
@@ -139,10 +142,12 @@ def execute(self, request):
139
142
full = True
140
143
if not expected_response_length :
141
144
expected_response_length = Defaults .ReadSize
142
- response , last_exception = self ._transact (request ,
143
- expected_response_length ,
144
- full = full
145
- )
145
+ response , last_exception = self ._transact (
146
+ request ,
147
+ expected_response_length ,
148
+ full = full ,
149
+ broadcast = broadcast
150
+ )
146
151
if not response and (
147
152
request .unit_id not in self ._no_response_devices ):
148
153
self ._no_response_devices .append (request .unit_id )
@@ -193,7 +198,7 @@ def execute(self, request):
193
198
self .client .state = ModbusTransactionState .TRANSACTION_COMPLETE
194
199
return ex
195
200
196
- def _transact (self , packet , response_length , full = False ):
201
+ def _transact (self , packet , response_length , full = False , broadcast = False ):
197
202
"""
198
203
Does a Write and Read transaction
199
204
:param packet: packet to be sent
@@ -209,20 +214,20 @@ def _transact(self, packet, response_length, full=False):
209
214
if _logger .isEnabledFor (logging .DEBUG ):
210
215
_logger .debug ("SEND: " + hexlify_packets (packet ))
211
216
size = self ._send (packet )
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 :
217
+ if broadcast :
221
218
if size :
222
219
_logger .debug ("Changing transaction state from 'SENDING' "
223
220
"to 'TRANSACTION_COMPLETE'" )
224
221
self .client .state = ModbusTransactionState .TRANSACTION_COMPLETE
225
- result = b''
222
+ return b'' , None
223
+ if size :
224
+ _logger .debug ("Changing transaction state from 'SENDING' "
225
+ "to 'WAITING FOR REPLY'" )
226
+ self .client .state = ModbusTransactionState .WAITING_FOR_REPLY
227
+ result = self ._recv (response_length , full )
228
+ if _logger .isEnabledFor (logging .DEBUG ):
229
+ _logger .debug ("RECV: " + hexlify_packets (result ))
230
+
226
231
except (socket .error , ModbusIOException ,
227
232
InvalidMessageReceivedException ) as msg :
228
233
self .client .close ()
0 commit comments