@@ -91,7 +91,7 @@ def _init_robot_main_run_codes(self):
91
91
92
92
def _finish_robot_main_run_codes (self , error_exit = True , stop_exit = True ):
93
93
if self ._listen_tgpio_digital or self ._listen_tgpio_analog or self ._listen_cgpio_state \
94
- or len (self ._tgpio_digital_callbacks ) or len (self ._tgpio_analog_callbacks ) or len (self ._cgpio_digital_callbacks ) or len (self ._cgpio_analog_callbacks ):
94
+ or len (self ._tgpio_digital_callbacks ) or len (self ._tgpio_analog_callbacks ) or len (self ._cgpio_digital_callbacks ) or len (self ._cgpio_analog_callbacks ) or len ( self . _count_callbacks ) or len ( self . _holding_callbacks ) :
95
95
self ._append_main_code (' # Event Loop' , indent = - 1 )
96
96
self ._append_main_code (' while self.is_alive:' , indent = - 1 )
97
97
self ._append_main_code (' time.sleep(0.5)' , indent = - 1 )
@@ -153,14 +153,18 @@ def _init_robot_main_class_codes(self, init=True, wait_seconds=1, mode=0, state=
153
153
self ._append_main_init_code (' self._cgpio_analog_callbacks = []' )
154
154
if len (self ._count_callbacks ):
155
155
self ._append_main_init_code (' self._count_callbacks = []' )
156
+ if len (self ._holding_callbacks ):
157
+ self ._append_main_init_code (' self._holding_callbacks = []' )
156
158
if self ._listen_cgpio_state or len (self ._cgpio_digital_callbacks ) or len (self ._cgpio_analog_callbacks ):
157
159
self ._append_main_init_code (' self._cgpio_state = None' )
158
160
159
161
if self ._listen_count or len (self ._count_callbacks ):
160
162
self ._append_main_init_code (' self._counter_val = None' )
161
-
163
+ if self ._listen_holding or len (self ._holding_callbacks ):
164
+ self ._append_main_init_code (' self._holding_dict = {}' )
165
+
162
166
if len (self ._tgpio_digital_callbacks ) or len (self ._tgpio_analog_callbacks ) or len (self ._cgpio_digital_callbacks ) or len (self ._cgpio_analog_callbacks )\
163
- or len (self ._count_callbacks ):
167
+ or len (self ._count_callbacks ) or len ( self . _holding_callbacks ) :
164
168
self ._append_main_init_code (' self._callback_in_thread = kwargs.get(\' callback_in_thread\' , True)' )
165
169
self ._append_main_init_code (' self._callback_que = queue.Queue()' )
166
170
@@ -170,20 +174,26 @@ def _init_robot_main_class_codes(self, init=True, wait_seconds=1, mode=0, state=
170
174
self ._append_main_init_code (' gpio_t.start()' )
171
175
172
176
if len (self ._tgpio_digital_callbacks ) or len (self ._tgpio_analog_callbacks ) or len (self ._cgpio_digital_callbacks ) or len (self ._cgpio_analog_callbacks )\
173
- or len (self ._count_callbacks ):
177
+ or len (self ._count_callbacks ) or len ( self . _holding_callbacks ) :
174
178
self ._append_main_init_code (' callback_t = threading.Thread(target=self._event_callback_handle_thread, daemon=True)' )
175
179
self ._append_main_init_code (' callback_t.start()' )
176
180
177
181
if self ._listen_count or len (self ._count_callbacks ):
178
182
self ._append_main_init_code (
179
183
' count_t = threading.Thread(target=self._listen_count_thread, daemon=True)' )
180
184
self ._append_main_init_code (' count_t.start()' )
181
-
185
+
186
+ if self ._holding_callbacks or len (self ._holding_callbacks ):
187
+ self ._append_main_init_code (
188
+ ' holding_t = threading.Thread(target=self._listen_holding_thread, daemon=True)' )
189
+ self ._append_main_init_code (' holding_t.start()' )
190
+
182
191
self ._append_main_init_code ('' )
183
192
184
193
self .__define_callback_thread_func ()
185
194
self .__define_listen_gpio_thread_func ()
186
195
self .__define_listen_count_thread_func ()
196
+ self .__define_listen_holding_registers_thread_func ()
187
197
self .__define_run_blockly_func ()
188
198
self .__define_robot_init_func (init = init , wait_seconds = wait_seconds , mode = mode , state = state , error_exit = error_exit , stop_exit = stop_exit )
189
199
self .__define_error_warn_changed_callback_func (error_exit = error_exit )
@@ -238,7 +248,7 @@ def __define_property(self):
238
248
239
249
def __define_callback_thread_func (self ):
240
250
# Define callback thread function
241
- if len (self ._tgpio_digital_callbacks ) or len (self ._tgpio_analog_callbacks ) or len (self ._cgpio_digital_callbacks ) or len (self ._cgpio_analog_callbacks ) or len (self ._count_callbacks ):
251
+ if len (self ._tgpio_digital_callbacks ) or len (self ._tgpio_analog_callbacks ) or len (self ._cgpio_digital_callbacks ) or len (self ._cgpio_analog_callbacks ) or len (self ._count_callbacks ) or len ( self . _holding_callbacks ) :
242
252
self ._append_main_init_code (' def _event_callback_handle_thread(self):' )
243
253
self ._append_main_init_code (' while self.alive:' )
244
254
self ._append_main_init_code (' try:' )
@@ -328,6 +338,23 @@ def __define_listen_count_thread_func(self):
328
338
self ._append_main_init_code (' self._callback_que.put(item[\' callback\' ])' )
329
339
self ._append_main_init_code (' self._counter_val = values' )
330
340
self ._append_main_init_code (' time.sleep(0.01)\n ' )
341
+
342
+ def __define_listen_holding_registers_thread_func (self ):
343
+ # Define listen holding registers value thread function
344
+ if self ._listen_holding or len (self ._holding_callbacks ):
345
+ self ._append_main_init_code (' def _listen_holding_thread(self):' )
346
+
347
+ self ._append_main_init_code (' while self.alive:' )
348
+ self ._append_main_init_code (' for index, item in enumerate(self._holding_callbacks):' )
349
+ self ._append_main_init_code (' for i in range(2):' )
350
+ self ._append_main_init_code (' _, values = self._arm.read_holding_registers(int(item[\' addr\' ]), 1)' )
351
+ self ._append_main_init_code (
352
+ ' if _ == 0 and self._holding_dict.get(index, None) is not None:' )
353
+ self ._append_main_init_code (
354
+ ' if eval(\' {} == {}\' .format(values[0], item[\' trigger\' ])) and not eval(\' {} == {}\' .format(self._holding_dict[index], item[\' trigger\' ])):' )
355
+ self ._append_main_init_code (' self._callback_que.put(item[\' callback\' ])' )
356
+ self ._append_main_init_code (' self._holding_dict[index] = values[0] if _ == 0 else self._holding_dict.get(index, None)' )
357
+ self ._append_main_init_code (' time.sleep(0.01)\n ' )
331
358
332
359
def __define_run_blockly_func (self ):
333
360
if self ._is_run_blockly and not self ._is_exec :
0 commit comments