@@ -91,7 +91,7 @@ def _init_robot_main_run_codes(self):
9191
9292 def _finish_robot_main_run_codes (self , error_exit = True , stop_exit = True ):
9393 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 ) :
9595 self ._append_main_code (' # Event Loop' , indent = - 1 )
9696 self ._append_main_code (' while self.is_alive:' , indent = - 1 )
9797 self ._append_main_code (' time.sleep(0.5)' , indent = - 1 )
@@ -153,12 +153,16 @@ def _init_robot_main_class_codes(self, init=True, wait_seconds=1, mode=0, state=
153153 self ._append_main_init_code (' self._cgpio_analog_callbacks = []' )
154154 if len (self ._count_callbacks ):
155155 self ._append_main_init_code (' self._count_callbacks = []' )
156+ if len (self ._holding_callbacks ):
157+ self ._append_main_init_code (' self._holding_callbacks = []' )
156158 if self ._listen_cgpio_state or len (self ._cgpio_digital_callbacks ) or len (self ._cgpio_analog_callbacks ):
157159 self ._append_main_init_code (' self._cgpio_state = None' )
158160
159161 if self ._listen_count or len (self ._count_callbacks ):
160162 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+
162166 if len (self ._tgpio_digital_callbacks ) or len (self ._tgpio_analog_callbacks ) or len (self ._cgpio_digital_callbacks ) or len (self ._cgpio_analog_callbacks )\
163167 or len (self ._count_callbacks ):
164168 self ._append_main_init_code (' self._callback_in_thread = kwargs.get(\' callback_in_thread\' , True)' )
@@ -170,15 +174,20 @@ def _init_robot_main_class_codes(self, init=True, wait_seconds=1, mode=0, state=
170174 self ._append_main_init_code (' gpio_t.start()' )
171175
172176 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 ) :
174178 self ._append_main_init_code (' callback_t = threading.Thread(target=self._event_callback_handle_thread, daemon=True)' )
175179 self ._append_main_init_code (' callback_t.start()' )
176180
177181 if self ._listen_count or len (self ._count_callbacks ):
178182 self ._append_main_init_code (
179183 ' count_t = threading.Thread(target=self._listen_count_thread, daemon=True)' )
180184 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+
182191 self ._append_main_init_code ('' )
183192
184193 self .__define_callback_thread_func ()
@@ -238,7 +247,7 @@ def __define_property(self):
238247
239248 def __define_callback_thread_func (self ):
240249 # 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 ):
250+ 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 ) :
242251 self ._append_main_init_code (' def _event_callback_handle_thread(self):' )
243252 self ._append_main_init_code (' while self.alive:' )
244253 self ._append_main_init_code (' try:' )
@@ -328,6 +337,23 @@ def __define_listen_count_thread_func(self):
328337 self ._append_main_init_code (' self._callback_que.put(item[\' callback\' ])' )
329338 self ._append_main_init_code (' self._counter_val = values' )
330339 self ._append_main_init_code (' time.sleep(0.01)\n ' )
340+
341+ def __define_listen_holding_registers_thread_func (self ):
342+ # Define listen holding registers value thread function
343+ if self ._listen_holding or len (self ._holding_callbacks ):
344+ self ._append_main_init_code (' def _listen_holding_thread(self):' )
345+
346+ self ._append_main_init_code (' while self.alive:' )
347+ self ._append_main_init_code (' for index, item in enumerate(self._holding_callbacks):' )
348+ self ._append_main_init_code (' for i in range(2):' )
349+ self ._append_main_init_code (' _, values = self._arm.read_holding_registers(int(item[\' addr\' ]), 1)' )
350+ self ._append_main_init_code (
351+ ' if _ == 0 and self._holding_dict.get(index, None) is not None:' )
352+ self ._append_main_init_code (
353+ ' if eval(\' {} == {}\' .format(values[0], item[\' trigger\' ])) and not eval(\' {} == {}\' .format(self._holding_dict[index], item[\' trigger\' ])):' )
354+ self ._append_main_init_code (' self._callback_que.put(item[\' callback\' ])' )
355+ self ._append_main_init_code (' self._holding_dict[index] = values[0] if _ == 0 else self._holding_dict.get(index, None)' )
356+ self ._append_main_init_code (' time.sleep(0.01)\n ' )
331357
332358 def __define_run_blockly_func (self ):
333359 if self ._is_run_blockly and not self ._is_exec :
0 commit comments