@@ -0,0 +1,2887 @@
+%0A%0Aimport threading%0Aimport time%0Aimport serial%0A%0A%0Aclass  ThreadedSerial(threading.Thread):%0A%0A  def __init__(self, port=None, baud=None):%0A    # First set up thread related  code%0A    threading.Thread.__init__(self)%0A    self.__dataLock = threading.RLock()%0A    self.__stopRunning = False%0A    self.__finished =  True%0A%0A    # now set up serial related code%0A    self.__serialPort = port%0A    self.__serialBaud = baud%0A    self.__serialRecieveFunc = None%0A    self.__serialConnection = None%0A%0A  def __del__(self):%0A    if self.__stopRunning:%0A      return%0A    self.__dataLock.acquire()%0A    self.__stopRunning = True%0A    self.__dataLock.release()%0A%0A    waiting = True%0A%0A    while waiting:%0A      if self.__dataLock.acquire(False):%0A        if self.__finished == True:%0A          waiting = False%0A%0A        self.__dataLock.release()%0A      else:%0A        # if can't get a lock wait for it%0A        time.sleep(0.2)%0A%0A  # This method takes a function as a paramater,%0A  # that function should take 1 paramater which will be passed a line of serial data%0A  def setSerialRecieveFunction(self, RecvFunc):%0A    self.__dataLock.acquire()%0A    self.__serialRecieveFunc = RecvFunc%0A    self.__dataLock.release()%0A%0A  def setSerialConfig(self, port, baud):%0A    self.__serialPort = port%0A    self.__serialBaud = baud%0A%0A  def create(self):%0A    # Create Serial connection - use a long timeout of 999 as its running in a thread and could%0A    # really wait forever for readline%0A    # TBD: handle exception%0A    self.__dataLock.acquire()%0A    self.__serialConnection = serial.Serial(self.__serialPort, self.__serialBaud, timeout=None)%0A    self.__dataLock.release()%0A%0A  # writes data to the serial port, automatically re encodes data to%0A  def write(self, data):%0A    self.__serialConnection.write(data)%0A    self.__serialConnection.flush()%0A%0A  # overloads the theads run class to provide non blocking Seral responses%0A  # @note create should be called before start%0A  def run(self):%0A%0A    while True:%0A      # take out lock for access to stop running%0A      # Use a non blocking lock, there is no harm in looping more times here while waiting for%0A      # something else to unlock%0A      if self.__dataLock.acquire():%0A        # if its time to stop running stop%0A        if self.__stopRunning == True:%0A          ## do cleanup here%0A%0A          self.__dataLock.release()%0A          return%0A%0A        self.__dataLock.release()%0A%0A      # this code blocks until it recieves a new line char%0A      read = self.__serialConnection.readline()%0A%0A      decoded = str(read.decode('utf-8', errors='ignore').rstrip().replace('%5Cn', '').replace('%5Cr', ''))%0A%0A      # check whatever you need to check here%0A      if self.__serialRecieveFunc is not None:%0A        # Call the function passing it the line as a parami%0A        self.__serialRecieveFunc(decoded)%0A      time.sleep(0.5)%0A%0A  def stop(self):%0A    self.__dataLock.acquire()%0A    self.__stopRunning = True%0A    self.__dataLock.release()%0A

Add threadedserial class