00001 import serial 00002 import threading 00003 00004 00005 class SerialHandler(object): 00006 def __init__(self, dev): 00007 self._ser = serial.Serial('/dev/' + dev) 00008 self._input_lock = threading.Condition() 00009 self._output_lock = threading.Condition() 00010 00011 def read(self, size): 00012 with self._input_lock: 00013 return self._ser.read(size) 00014 00015 def write(self, data): 00016 with self._output_lock: 00017 self._ser.write(data) 00018 00019 def flush(self): 00020 with self._output_lock: 00021 while self._ser.inWaiting() > 0: 00022 print self._ser.inWaiting() 00023 self._ser.read(1) 00024 00025 def available(self): 00026 with self._input_lock: 00027 return self._ser.inWaiting()