|
@ -4,6 +4,8 @@ import threading |
|
|
import serial |
|
|
import serial |
|
|
|
|
|
|
|
|
class Serial(object): |
|
|
class Serial(object): |
|
|
|
|
|
self._callbacks = dict() |
|
|
|
|
|
|
|
|
def __init__(self, devicename='/dev/ttyUSB0', baudrate=115200): |
|
|
def __init__(self, devicename='/dev/ttyUSB0', baudrate=115200): |
|
|
self.devicename = devicename |
|
|
self.devicename = devicename |
|
|
self.alive = None |
|
|
self.alive = None |
|
@ -46,7 +48,16 @@ class Serial(object): |
|
|
data = self.serialport.read(self.serialport.in_waiting or 1) |
|
|
data = self.serialport.read(self.serialport.in_waiting or 1) |
|
|
if data: |
|
|
if data: |
|
|
print(data) |
|
|
print(data) |
|
|
|
|
|
command = data[0] |
|
|
|
|
|
argument = data[2:] |
|
|
|
|
|
cb = self._callbacks[command] |
|
|
|
|
|
if cb: |
|
|
|
|
|
cb(argument) |
|
|
|
|
|
|
|
|
except serial.SerialException: |
|
|
except serial.SerialException: |
|
|
pass |
|
|
pass |
|
|
#self.alive = False |
|
|
#self.alive = False |
|
|
#raise |
|
|
#raise |
|
|
|
|
|
|
|
|
|
|
|
def add_callback(cmd, callback): |
|
|
|
|
|
self._callbacks[cmd] = callback |
|
|