Esempio n. 1
0
 /// <summary>
 /// Retreive the Data from the Serial Prot using Unity Coroutines
 /// </summary>
 /// <param name="target"></param>
 /// <returns>null</returns>
 public IEnumerator ReadSerial(string target = null)
 {
     while (true)
     {
         UduinoDevice uduino = null;
         if (target != null && uduinoDevices.TryGetValue(target, out uduino))
         {
             uduino.WriteToArduinoLoop();
             if (uduino.read != null)
             {
                 uduino.ReadFromArduino(uduino.read);
                 uduino.ReadFromArduinoLoop();
                 yield return(null);
             }
             else
             {
                 yield return(null);
             }
         }
         else
         {
             foreach (KeyValuePair <string, UduinoDevice> uduinoDevice in uduinoDevices)
             {
                 uduinoDevice.Value.ReadFromArduinoLoop();
                 uduinoDevice.Value.WriteToArduinoLoop();
             }
             yield return(null);
         }
     }
 }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        /// <summary>
        /// Retreive the Data from the Serial Prot using Unity Coroutines
        /// </summary>
        /// <param name="target"></param>
        /// <returns>null</returns>
        public IEnumerator ReadSerial(string target)
        {
            while (true)
            {
                UduinoDevice uduino = null;
                if (uduinoDevices.TryGetValue(target, out uduino))
                {
                    if (uduino.read != null)
                    {
                        string data = uduino.ReadFromArduino(uduino.read, 50);
                        uduino.read = null;
                        yield return(null);

                        ReadData(data, target);
                    }
                    else
                    {
                        yield return(null);
                    }
                }
                else
                {
                    yield return(null);
                }
            }
        }
Esempio n. 4
0
        /// <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;
            }
        }
Esempio n. 5
0
        /// <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;
            }
        }
Esempio n. 6
0
        /// <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;
            }
        }