-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuart_logger.py
46 lines (36 loc) · 1.12 KB
/
uart_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import serial
import threading
from kivy.logger import Logger
class UartLogger():
def __init__(self, arg):
super(UartLogger, self).__init__()
self.port = arg
self.cb = None
self.ser = None
def open(self, cb):
try:
self.ser = serial.Serial(self.port, baudrate=115200)
self.ser.flushInput()
except Exception as e:
Logger.error("UartLogger: Failed to open uart: {}".format(e))
self.ser = None
return False
self.cb = cb
self.comms_thread = threading.Thread(target=self._async_read, daemon=True)
self.comms_thread.start()
return True
def close(self):
if self.ser:
self.ser.close()
self.ser = None
def _async_read(self):
while True:
try:
ser_bytes = self.ser.readline()
decoded_bytes = ser_bytes.decode("utf-8")
if self.cb is not None:
self.cb(decoded_bytes)
except Exception:
pass
if self.ser is None:
return