public void createTrainWithoutSerialPort() { int fakeTrainID = 0; for (int i = 1; i < 6; i++) { if (portDictionary.ContainsKey(i)) { continue; } fakeTrainID = i; break; } if (fakeTrainID == 0) { Debug.Log("All Users are assigned"); return; } SerialPort sp = new SerialPort("fake" + fakeTrainID); RecieveIAm fakeReciept = new RecieveIAm(sp); fakeReciept.recieverID = fakeTrainID; fakeReciept.executePopulatedMessage(); StartCoroutine(readPort(sp)); }
//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); } }