//Listens to each available port for (listeningTime) seconds. //If during this time the metho OnMessageArrived() gets //called, we check whether this message is equal to the //RECEIVER_IDENTIFICATOR string. If yes, we listen to only //this port from now on. private IEnumerator SearchForReceiver() { currentMode = Modes.Setup; bool foundReceiver = false; foreach (string port in SerialPort.GetPortNames()) { Debug.Log("Listening to port " + port); sc.StartListeningToPort(port); yield return(new WaitForSeconds(listeningTime)); //if during the listeningTime, OnMessageArrived was called, //currentMode will have been set to Modes.Receiving if (currentMode == Modes.Receiving) { foundReceiver = true; break; } else { sc.StopListeningToCurrentPort(); } } if (foundReceiver) { Debug.Log("We found a receiver!"); sc.SendSerialMessage(RECEIVER_RECOGNIZED); } else { Debug.Log("No receiver was found."); currentMode = Modes.NoReceiver; } }