from panoptes.utils import logger

import threading
import serial
import time

@logger.has_logger
class SerialData(object):

    """
    Main serial class
    """

    def __init__(self, port=None, baudrate=9600, threaded=True, name="serial_data"):

        try:
            self.ser = serial.Serial()
            self.ser.port = port
            self.ser.baudrate = baudrate
            self.is_threaded = threaded

            self.ser.bytesize = serial.EIGHTBITS
            self.ser.parity = serial.PARITY_NONE
            self.ser.stopbits = serial.STOPBITS_ONE
            self.ser.timeout = 0.1
            self.ser.xonxoff = 0
            self.ser.rtscts = 0
            self.ser.interCharTimeout = None

            self.serial_receiving = ''

            if self.is_threaded:
                self.logger.debug("Using threads")
                self.thread = threading.Thread(target=self.receiving_function)
                self.thread.name = name

            self.logger.debug(
                'Serial connection set up to mount, sleeping for two seconds')
            time.sleep(2)
        except:
            self.ser = None
            self.logger.critical('Could not set up serial port')

        self.logger.info('SerialData created')

    @property
    def is_connected(self):
        """
        Checks the serial connection on the mount to determine if connection is open
        """
        return self.ser.isOpen()

    def connect(self):
        """ Actually set up the Thrad and connect to serial """

        self.logger.info('Serial connect called')
        if not self.ser.isOpen():
            try:
                self.ser.open()
            except serial.serialutil.SerialException as err:
                raise error.BadSerialConnection(msg=err)

        if not self.ser.isOpen():
            raise error.BadSerialConnection

        self.logger.info('Serial connection established to mount')
        return self.ser.isOpen()

    def receiving_function(self):
        buffer = ''
        while True:
            try:
                buffer = buffer + self.read()
                if '\n' in buffer:
                    lines = buffer.split('\n')  # Guaranteed to have at least 2 entries
                    self.serial_receiving = lines[-2]
                    # If the Arduino sends lots of empty lines, you'll lose the
                    # last filled line, so you could make the above statement conditional
                    # like so: if lines[-2]: serial_receiving = lines[-2]
                    buffer = lines[-1]
            except IOError as err:
                print("Device is not sending messages. IOError: {}".format(err))
                time.sleep(2)
            except UnicodeDecodeError:
                print("Unicode problem")
                time.sleep(2)
            except:
                print("Uknown problem")

    def write(self, value):
        """
            For now just pass the value along to serial object
        """
        assert self.ser
        assert self.ser.isOpen()

        self.logger.debug('Serial write: {}'.format(value))
        return self.ser.write(value.encode())

    def read(self):
        """
        Reads value using readline
        If no response is given, delay and then try to read again. Fail after 10 attempts
        """
        assert self.ser
        assert self.ser.isOpen()

        retry_limit = 5
        delay = 0.5

        while True and retry_limit:
            response_string = self.ser.readline(self.ser.inWaiting()).decode()
            if response_string > '':
                break

            time.sleep(delay)
            retry_limit -= 1

        self.logger.debug('Serial read: {}'.format(response_string))

        return response_string

    def get_reading(self):
        if not self.ser:
            return 0
        for i in range(40):
            raw_line = self.serial_receiving
            try:
                return raw_line.strip()
            except ValueError:
                time.sleep(.005)
        return 0.

    def clear_buffer(self):
        """ Clear Response Buffer """
        count = 0
        while self.ser.inWaiting() > 0:
            count += 1
            contents = self.ser.read(1)

        self.logger.debug('Cleared {} bytes from buffer'.format(count))


    def __del__(self):
        if self.ser:
            self.ser.close()

@@ -35,22 +35,28 @@
 %0Aimport 
-thread
+multiprocess
 ing%0Aimpo
@@ -861,33 +861,41 @@
 elf.
-thread = threading.Thread
+process = multiprocessing.Process
 (tar
@@ -944,22 +944,23 @@
    self.
-thread
+process
 .name = 
@@ -1436,32 +1436,127 @@
 f.ser.isOpen()%0A%0A
+    def start(self):%0A        %22%22%22 Starts the separate process %22%22%22%0A        self.process.start()%0A%0A
     def connect(

Switch from threading to multiprocessing