//each port is constantly running this function, basically just lisening for chars
    //to come through and will build a command, once it finds a null character then the
    //command must be complete
    public IEnumerator readPort(SerialPort sp)
    {
        //List<char> currentCommand = new List<char>();

        Debug.Log("starting reading on port: " + sp.PortName);
        RecieveCommand currentCommand = null;

        //constantly checking if there is an incoming char to the specific serial port
        //if there is a char to read, then it adds it to the current command
        //if there is not a char to read it just continues.

        while (true)
        {
            //check if device was disconnected
            bool debugInitializedAndPopulated = debugCommandsDictionary.ContainsKey(sp) && debugCommandsDictionary[sp].Count != 0;

            //if it has bytes to read OR the debug array exists AND had stuff in it

            //if (debugInitializedAndPopulated) {
            //    Debug.Log(debugCommandsDictionary[sp].Dequeue());
            //}



            while ((debugInitializedAndPopulated || (sp.IsOpen && sp.BytesToRead > 0)))
            {
                try
                {
                    //I should be parsing it as they come in
                    byte incomingByte;


                    if (debugInitializedAndPopulated)
                    {
                        incomingByte = debugCommandsDictionary[sp].Dequeue();
                        debugInitializedAndPopulated = debugCommandsDictionary.ContainsKey(sp) && debugCommandsDictionary[sp].Count != 0;
                    }
                    else
                    {
                        //incomingByte = (byte)sp.ReadChar();

                        if (sp.IsOpen)
                        {
                            incomingByte = (byte)sp.ReadChar();
                        }
                        else
                        {
                            throw new TimeoutException();
                        }
                    }
                    //reject all commands if the game is paused except for vote:)
                    if (currentCommand == null)
                    {
                        //spawns a new command
                        switch ((char)incomingByte)
                        {
                        case 'r':
                            if (gamePaused)
                            {
                                break;
                            }
                            Debug.Log("setting new command to I AM for sp " + sp.PortName);
                            currentCommand = new RecieveIAm(sp);
                            break;

                        case 'n':
                            if (gamePaused || !gameStarted)
                            {
                                break;
                            }
                            Debug.Log("setting new command to CREATE TRAIN");
                            currentCommand = new CreateTrain();
                            break;

                        case 'o':
                            if (gamePaused || !gameStarted)
                            {
                                break;
                            }
                            Debug.Log("setting new command to STOP TRAIN PRESSED");
                            currentCommand = new StopTrainPressed();
                            break;

                        case 'p':
                            if (gamePaused || !gameStarted)
                            {
                                break;
                            }
                            Debug.Log("setting new command to ANSWER TRAIN");
                            currentCommand = new AnswerTrain();
                            break;

                        case 'q':
                            Debug.Log("setting new command to SEND VOTE");
                            currentCommand = new SendVote();
                            break;

                        case 'i':
                            Debug.Log("setting new command to RECIEVE SET SPEED");
                            currentCommand = new RecieveSetSpeed();
                            break;

                        case 'u':
                            Debug.Log("setting new command to START RESYNC");
                            currentCommand = new RecieveStartResync();
                            break;

                        case 'v':
                            Debug.Log("setting new command to END RESYNC");
                            currentCommand = new RecieveEndResync();
                            break;

                        case 's':
                            //Debug.Log("setting new command to DEBUG");
                            currentCommand = new ReadError();
                            ((ReadError)currentCommand).whoAmI = sp.PortName;
                            break;

                        case 'z':
                            //Debug.Log("setting new command to DEBUG");
                            Debug.Log("setting new command to RECIEVE PONG");
                            currentCommand = new RecievePongFromLEDArduino();
                            break;

                        case 'A':
                            Debug.Log("setting new command to RECIEVE TRAIN PING");
                            //Debug.Log("setting new command to DEBUG");
                            currentCommand = new RecieveTrainPing();
                            break;

                        default:
                            Debug.LogError("Unable to figure out command sent to me: " + incomingByte + " " + (char)incomingByte);
                            break;
                        }
                    }
                    else
                    {
                        if (incomingByte == 0)
                        {
                            if (currentCommand != null)
                            {
                                currentCommand.executePopulatedMessage();
                                currentCommand = null;
                            }
                        }
                        else
                        {
                            currentCommand.readNextByte(incomingByte);
                        }
                    }
                }
                catch (TimeoutException)
                {
                    //Debug.Log("timeout exception");
                }
            }
            yield return(null);
        }
    }
    private void CheckTrain()
    {
        if (RailCreateManager.Instance.trainExistence && CheckFrag)
        {
            SpeedUpButton.SetActive(true);
            SpeedDownButton.SetActive(true);
            SpeedMeter.SetActive(true);

            headText.text = ("はしらせよう");
            //電車を取得する
            CreateTrain createtrain = this.gameObject.GetComponent <CreateTrain> ();

            TrainObject = createtrain.GetTrainInst();
            Debug.Log("電車" + TrainObject);

            //電車についたObjectControllerを持ってくる
            objectcontroller = TrainObject.GetComponent <ObjectController> ();

            // 初速度
            objectcontroller.SetSpeed(0.05f);

            // イベントトリガー取得
            EventTrigger SpUpBtnTrig   = SpeedUpButton.GetComponent <EventTrigger> ();
            EventTrigger SpDownBtnTrig = SpeedDownButton.GetComponent <EventTrigger> ();

            // イベントが重複しないように削除
            SpUpBtnTrig.triggers   = null;
            SpDownBtnTrig.triggers = null;

            // イベントトリガーに登録
            EventTrigger.Entry entryGo = new EventTrigger.Entry();
            entryGo.eventID = EventTriggerType.PointerDown;
            entryGo.callback.AddListener((x) => objectcontroller.SpeedUp(true));
            SpUpBtnTrig.triggers.Add(entryGo);

            EventTrigger.Entry entryGoF = new EventTrigger.Entry();
            entryGoF.eventID = EventTriggerType.PointerUp;
            entryGoF.callback.AddListener((x) => objectcontroller.SpeedUp(false));
            SpUpBtnTrig.triggers.Add(entryGoF);



            EventTrigger.Entry entryStop = new EventTrigger.Entry();
            entryStop.eventID = EventTriggerType.PointerDown;
            entryStop.callback.AddListener((x) => objectcontroller.SpeeDown(true));
            SpDownBtnTrig.triggers.Add(entryStop);

            EventTrigger.Entry entryStopF = new EventTrigger.Entry();
            entryStopF.eventID = EventTriggerType.PointerUp;
            entryStopF.callback.AddListener((x) => objectcontroller.SpeeDown(false));
            SpDownBtnTrig.triggers.Add(entryStopF);

            // イベントが重複しないように削除
            // SpeedUpButton.onClick.RemoveAllListeners ();
            // SpeedDownButton.onClick.RemoveAllListeners ();

            //ボタンが押された時に実行する関数の指定
            // SpeedUpButton.onClick.AddListener (() => objectcontroller.GoButton ());
            // SpeedDownButton.onClick.AddListener (() => objectcontroller.StopButton ());

            CheckFrag = false;
        }
    }