Exemplo n.º 1
0
        public static Task Delay(int millisecond, Select sel = Select.TSK, bool force = false)
        {
            if (force != true && millisecond >= 15)
            {
                return(Task.Delay(millisecond));
            }
            else if (millisecond >= 1)
            {
                switch (sel)
                {
                case Select.MMT: return(MultimediaTimer.Delay(millisecond));

                case Select.TQT: return(TQTDelay(millisecond));

                case Select.TRD: return(StaticThreadTimer(millisecond));

                case Select.TSK:
                default: return(Task.Delay(millisecond));
                }
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        public void loop()
        {
            stopExecuting = false;

            using (RobustSerial arduinoPort = connectArduino())
                using (Cls2SimSocket brunnerSocket = connectBrunner(required: false))
                {
                    var timer = new HighPrecisionTimer.MultimediaTimer();
                    timer.Interval   = timerMs;
                    timer.Resolution = 2;
                    //do what you need with the serial port here
                    try
                    {
                        arduinoPort.WaitForConnection();
                        logger.Info("Checking Arduino Firmware version");
                        CheckArduinoSketchVersion(arduinoPort, maxSeconds: 3);
                        _isArduinoConnected = true;
                        if (brunnerSocket != null)
                        {
                            _isBrunnerConnected = true;
                        }

                        timer.Elapsed += (o, e) => Communicate(arduinoPort, brunnerSocket);
                        timer.Start();

                        while (arduinoPort.IsOpen && !stopExecuting)
                        {
                            var currentPosition = new int[2];
                            _position.CopyTo(currentPosition, 0);
                            positionHistory.Enqueue(currentPosition);
                            var prueba = positionHistory.ToArray();
                            if (positionHistory.Count > 60)
                            {
                                positionHistory.Dequeue();
                            }
                            System.Threading.Thread.Sleep(100);
                        }
                        timer.Stop();
                        System.Threading.Thread.Sleep(500); // Give it time to the timer events to finish
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, ex.StackTrace);
                        logger.Error(ex, ex.Message);
                    }
                    finally
                    {
                        _isArduinoConnected = false;
                        _isBrunnerConnected = false;
                        if (timer.IsRunning)
                        {
                            timer.Stop();
                        }
                    }
                }
        }