/// <summary> /// Find a board connected to a specific port /// </summary> /// <param name="portName">Port open</param> IEnumerator FindBoardPort(string portName) { UduinoDevice uduinoDevice = new UduinoDevice(portName, baudRate, readTimeout, writeTimeout); int tries = 0; do { if (uduinoDevice.getStatus() == SerialStatus.OPEN) { string reading = uduinoDevice.ReadFromArduino("identity", instant: true); if (reading != null && reading.Split(new char[0])[0] == "uduinoIdentity") { string name = reading.Split(new char[0])[1]; uduinoDevice.name = name; lock (uduinoDevices) uduinoDevices.Add(name, uduinoDevice); //Add the new device to the devices array if (!ReadOnThread) { StartCoroutine(ReadSerial(name)); // Initiate the Async reading of variables } Log.Warning("Board <color=#ff3355>" + name + "</color> <color=#2196F3>[" + uduinoDevice.getPort() + "]</color> added to dictionnary"); uduinoDevice.UduinoFound(); if (Application.isPlaying && _thread == null && readOnThread) { StartThread(); } Write(name, "connected"); //uduinoDevice.WriteToArduino(); InitAllArduinos(); break; } else { Log.Debug("Impossible to get name on <color=#2196F3>[" + portName + "]</color>. Retrying."); } } //TODO : compare with yield return new WaitForSeconds(0.5f); yield return(null); //Wait one frame with yield return null } while (uduinoDevice.getStatus() != SerialStatus.UNDEF && tries++ < discoverTries); if (uduinoDevice.getStatus() != SerialStatus.FOUND) { Log.Warning("Impossible to get name on <color=#2196F3>[" + portName + "]</color>. Closing."); uduinoDevice.Close(); uduinoDevice = null; } }
bool TryToFind(UduinoDevice uduinoDevice, bool callAsync = false) { if (uduinoDevice.getStatus() == BoardStatus.Finding) { string reading = uduinoDevice.ReadFromArduino("identity", instant: true); Log.Debug("Trying to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>.", true); if (reading != null && reading.Split(new char[0])[0] == "uduinoIdentity") { string name = reading.Split(new char[0])[1]; uduinoDevice.name = name; if (callAsync) { _manager.InvokeAsync(() => { uduinoDevice.UduinoFound(); _manager.AddUduinoBoard(name, uduinoDevice); }); } else { uduinoDevice.UduinoFound(); _manager.AddUduinoBoard(name, uduinoDevice); } BoardFound(name); return(true); } else { Log.Debug("Impossible to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>. Retrying."); } } return(false); }
/// <summary> /// Find a board connected to a specific port /// </summary> /// <param name="portName">Port open</param> public virtual IEnumerator DetectUduino(UduinoDevice uduinoDevice) { int tries = 0; do { if (uduinoDevice.getStatus() == BoardStatus.Open) { string reading = uduinoDevice.ReadFromArduino("identity", instant: true); if (reading != null && reading.Split(new char[0])[0] == "uduinoIdentity") { string name = reading.Split(new char[0])[1]; uduinoDevice.name = name; _manager.AddUduinoBoard(name, uduinoDevice); uduinoDevice.UduinoFound(); if (!_manager.ReadOnThread) { _manager.StartCoroutine(_manager.CoroutineRead(name)); // Initiate the Async reading of variables } else { _manager.StartThread(); } uduinoDevice.WriteToArduino("connected"); _manager.InitAllArduinos(); break; } else { Log.Debug("Impossible to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>. Retrying."); } } yield return(new WaitForSeconds(0.05f)); //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ? // yield return null; //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ? } while (uduinoDevice.getStatus() != BoardStatus.Undef && tries++ < _manager.DiscoverTries); if (uduinoDevice.getStatus() != BoardStatus.Found) { Log.Warning("Impossible to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>. Closing."); uduinoDevice.Close(); uduinoDevice = null; } }
public virtual bool BoardNotFound(UduinoDevice uduinoDevice) { if (uduinoDevice.getStatus() != BoardStatus.Found) { Log.Warning("Impossible to get name on <color=#2196F3>[" + uduinoDevice.identity + "]</color>. Closing."); uduinoDevice.Close(); uduinoDevice = null; return(false); } else { return(true); } }
/// <summary> /// Find a board connected to a specific port /// </summary> /// <param name="portName">Port open</param> IEnumerator FindBoardPort(string portName) { UduinoDevice uduinoDevice = new UduinoDevice(portName, baudRate); int tries = 0; do { if (uduinoDevice.getStatus() == SerialStatus.OPEN) { string reading = uduinoDevice.ReadFromArduino("IDENTITY", 200); if (reading != null && reading.Split(new char[0])[0] == "uduinoIdentity") { string name = reading.Split(new char[0])[1]; uduinoDevices.Add(name, uduinoDevice); //Add the new device to the devices array if (!ReadOnThread) { StartCoroutine(ReadSerial(name)); // Initiate the Async reading of variables } Log.Info("Board <color=#ff3355>" + name + "</color> <color=#2196F3>[" + uduinoDevice.getPort() + "]</color> added to dictionnary"); uduinoDevice.UduinoFound(); break; } else { Log.Warning("Impossible to get name on <color=#2196F3>[" + portName + "]</color>. Retrying."); } } yield return(new WaitForSeconds(0.1f)); //Wait one frame } while (uduinoDevice.getStatus() != SerialStatus.UNDEF && tries++ < discoverTries); if (uduinoDevice.getStatus() != SerialStatus.FOUND) { Log.Warning("Impossible to get name on <color=#2196F3>[" + portName + "]</color>. Closing."); uduinoDevice.Close(); uduinoDevice = null; } }
/// <summary> /// Find a board connected to a specific port /// </summary> /// <param name="uduinoDevice">uduinoDevice</param> public IEnumerator DetectUduinoCoroutine(UduinoDevice uduinoDevice) { uduinoDevice.boardStatus = BoardStatus.Finding; int tries = 0; yield return(new WaitForSeconds(UduinoManager.Instance.delayBeforeDiscover)); do { if (TryToFind(uduinoDevice)) { break; } yield return(new WaitForSeconds(0.1f)); //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ? // yield return null; //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ? } while (uduinoDevice.getStatus() != BoardStatus.Found && tries++ < _manager.DiscoverTries - 1); BoardNotFound(uduinoDevice); }
/// <summary> /// Detect Uduino device from a thread /// </summary> /// <param name="uduinoDevice"></param> public void DetectUduinoThread(UduinoDevice uduinoDevice) { #if UNITY_ANDROID if (_manager.ReadOnThread) { AndroidJNI.AttachCurrentThread(); // Sepcific android related code } #endif uduinoDevice.boardStatus = BoardStatus.Finding; int tries = 0; Thread.Sleep(Mathf.FloorToInt(UduinoManager.Instance.delayBeforeDiscover * 1000)); do { if (TryToFind(uduinoDevice, true)) { return; } Thread.Sleep(100); //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ? // yield return null; //Wait one frame. Todo : use yield return new WaitForSeconds(0.5f); ? } while (uduinoDevice.getStatus() != BoardStatus.Found && tries++ < _manager.DiscoverTries); BoardNotFound(uduinoDevice); }