Sfoglia il codice sorgente

fix input interval timer

subDesTagesMitExtraKaese 2 anni fa
parent
commit
d31771ccb0
1 ha cambiato i file con 8 aggiunte e 4 eliminazioni
  1. 8 4
      box-pc/plc-connector/inputs/common.py

+ 8 - 4
box-pc/plc-connector/inputs/common.py

@@ -6,12 +6,14 @@ import logging
 
 from structures.measurement import *
 
+logger = logging.getLogger(__name__)
+
 class Input:
   _t = None
   _stop = False
   _q = Queue()
 
-  interval = 1
+  interval = 1.0
   _read_cb = None
 
   def __init__(self, read_cb) -> None:
@@ -40,15 +42,17 @@ class Input:
       try:
         self._read_cb()
       except Exception as e:
-        logging.exception(F"An exception occured while reading from {type(self)}!")
+        logger.exception(F"An exception occured while reading from {type(self)}!")
         time.sleep(1)
 
       end_time = time.monotonic()
       remaining = self.interval + start_time - end_time
-      start_time = end_time
       if remaining > 0:
+        start_time += self.interval
         time.sleep(remaining)
-
+      else:
+        start_time = end_time
+        
   def queue_ifm_from_bytes(self, source, timestamp, raw, channels = 16):
     data = struct.unpack(">" + "B" * 16 + "HHHHHBxH", raw)
     current = tuple([x / 10 for x in data[0:channels]])