]> Pileus Git - ~andy/csm213a-hw/blobdiff - vis/device.py
Flush pending input data from device
[~andy/csm213a-hw] / vis / device.py
index 385a142f1e9d14c4cd3fcf8385b16a9a25af8b27..f1766f0f57726b6e9c9121ad70281973fbcc27fa 100644 (file)
@@ -1,5 +1,6 @@
-from re     import compile
-from serial import Serial
+from re       import compile
+from serial   import Serial
+from datetime import datetime
 
 class State:
        acc   = [None]*3
@@ -7,9 +8,10 @@ class State:
        touch = [None]*2
        light = [None]*1
        a2d   = [None]*6
+       time  = None
 
        def __init__(self):
-               pass
+               self.time = datetime.utcnow()
 
 class Device:
        # Attributes
@@ -30,6 +32,7 @@ class Device:
                                bytesize = self.config.databits, \
                                stopbits = self.config.stopbits, \
                                timeout  = 0)
+                       self.serial.flushInput()
                except Exception as ex:
                        return str(ex)
 
@@ -48,6 +51,8 @@ class Device:
                items = []
                count = 0
                limit = 100
+               if not self.running():
+                       return items
                while self.serial.readable():
                        try:
                                char = self.serial.read().decode()
@@ -65,7 +70,7 @@ class Device:
                        else:
                                self.inbuf.append(char)
                        if count > limit:
-                               print("Eror: exceeded read limit")
+                               print("Error: exceeded read limit")
                                break
                        count += 1
                return items