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