From b386402a0627e55d0338b7c76f8e388d86b94803 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Tue, 4 Aug 2020 13:44:18 +0200 Subject: [PATCH] serial callbacks --- raspberry/roberto/Serial.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/raspberry/roberto/Serial.py b/raspberry/roberto/Serial.py index 9114455..cfa7f5f 100644 --- a/raspberry/roberto/Serial.py +++ b/raspberry/roberto/Serial.py @@ -4,6 +4,8 @@ import threading import serial class Serial(object): + self._callbacks = dict() + def __init__(self, devicename='/dev/ttyUSB0', baudrate=115200): self.devicename = devicename self.alive = None @@ -46,7 +48,16 @@ class Serial(object): data = self.serialport.read(self.serialport.in_waiting or 1) if data: print(data) + command = data[0] + argument = data[2:] + cb = self._callbacks[command] + if cb: + cb(argument) + except serial.SerialException: pass #self.alive = False #raise + + def add_callback(cmd, callback): + self._callbacks[cmd] = callback