void Close()
    {
        Debug.Log("Stopping Thread");

        ChildThreadWait.Close();
        MainThreadWait.Close();
        try
        {
            ChildThread.Abort();
        }
        catch (Exception e)
        {
            Debug.Log(e);
        }

        if (serial != null)
        {
            try
            {
                serial.Close();
                serial = null;
                Debug.Log("Serial closed.");
            }
            catch (Exception e)
            {
                Debug.Log(e);
            }
        }
    }
    void Update()
    {
        MainThreadWait.WaitOne();
        MainThreadWait.Reset();


        if (mainThreadCallback != null)
        {
            try
            {
                mainThreadCallback();
            } catch (Exception e) {
                Debug.Log(e);
            }
            mainThreadCallback = null;
        }



        if (useReceiveEvents)
        {
            lock (readQueue)
            {
                if (readQueue.Count > 0)
                {
                    ValueReceivedEvent.Invoke(readQueue.Dequeue());
                }
            }
        }

        ChildThreadWait.Set();
    }