Ejemplo n.º 1
0
        public void CicloPrincipale()
        {
            int[] buffer         = new int[DIMENSIONE_BUFFER];
            int   indiceCorrente = 0;

            while (!_dovrebbeFermarsi)
            {
                try
                {
                    // Leggi la riga
                    string line = _serialPort.ReadLine();
                    if (line != null)
                    {
                        line = line.Trim();
                        // Controllo della validità
                        if (line.StartsWith("START ") && line.EndsWith(" END"))
                        {
                            // Parsa il valore
                            int value = int.Parse(line.Split(' ')[1]);

                            // Riempi il buffer e, quando è pieno, notifica i ricevitori
                            if (indiceCorrente < DIMENSIONE_BUFFER)
                            {
                                buffer[indiceCorrente] = value;
                                indiceCorrente++;
                            }
                            else
                            {
                                // Invio i dati
                                RicevitoriDatiSensore?.Invoke(buffer);

                                // Resetto il buffer
                                indiceCorrente = 0;
                            }
                        }
                    }
                }
                catch (TimeoutException e)
                {
                    Console.WriteLine(e.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
            _serialPort.Close();
        }
Ejemplo n.º 2
0
        public void CicloPrincipale()
        {
            while (!_dovrebbeFermarsi)
            {
                // Genero un buffer casuale
                int[] buffer = new int[DIMENSIONE_BUFFER];
                for (int i = 0; i < DIMENSIONE_BUFFER; i++)
                {
                    int picco = 0;
                    if (_random.Next(1, 1000) > 997)
                    {
                        picco = 300;
                    }
                    buffer[i] = (int)(Math.Cos(_time) * 20 + 500) + picco + _random.Next(1, 50);
                    _time    += 0.1;
                }

                // Invio i dati
                RicevitoriDatiSensore?.Invoke(buffer);

                // Dormo un po
                System.Threading.Thread.Sleep(10);
            }
        }