Browse Source

callbacks fix

main
Hendrik Langer 4 years ago
parent
commit
18487181ee
  1. 11
      raspberry/roberto/Serial.py

11
raspberry/roberto/Serial.py

@ -45,24 +45,29 @@ class Serial(object):
def reader(self): def reader(self):
try: try:
while self.alive and self.serialport: while self.alive and self.serialport:
data = self.serialport.read(self.serialport.in_waiting or 1) #data = self.serialport.read(self.serialport.in_waiting or 1)
data = self.serialport.readline()
if data: if data:
print(data) print(data)
if isinstance(data, bytes):
data = data.decode(encoding='utf8')
command = data[0] command = data[0]
argument = data[2:].strip() argument = data[2:].strip()
if command and command in self._callbacks: if command and command in self._callbacks:
cb = self._callbacks[command] cb = self._callbacks[command]
if cb: if cb:
cb(argument) cb(argument)
else:
print("no callback found for command '%s'" % command)
except serial.SerialException: except serial.SerialException:
pass pass
#self.alive = False #self.alive = False
#raise #raise
def add_callback(cmd, callback): def add_callback(self, cmd, callback):
self._callbacks[cmd] = callback self._callbacks[cmd] = callback
def del_callback(cmd, callback): def del_callback(self, cmd, callback):
if cmd in self._callbacks: if cmd in self._callbacks:
del self._callbacks[cmd] del self._callbacks[cmd]

Loading…
Cancel
Save