Esempio n. 1
0
 public void Clear()
 {
     SensorValues.Clear();
     MaxSensorValues.Clear();
     NoiseOffsetValues.Clear();
     rawSensorValues.Clear();
     songAvgValues.Clear();
     ModeValues.Clear();
 }
Esempio n. 2
0
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            var    sp     = (SerialPort)sender;
            string indata = _lastFragment + sp.ReadExisting();

            sp.DiscardInBuffer();

            if (!IsReading)
            {
                return;
            }

            indata = indata.Replace("\r\n", "\n");

            var lines     = indata.Split(SEP_LINES);
            var noOfLines = lines.Length;

            if (!indata.EndsWith(new string(SEP_LINES, 1)))
            {
                // last fragment is not complete
                --noOfLines;
                _lastFragment = lines[noOfLines];
            }
            else
            {
                _lastFragment = "";
            }

            const string suffixTime = "| ";

            for (int index = 0; index < noOfLines; index++)
            {
                var line = lines[index];
                var pos  = line.IndexOf(suffixTime, StringComparison.Ordinal);
                if (pos >= 0)
                {
                    line = line.Substring(pos + suffixTime.Length);
                }

                if (line.StartsWith(COMMENT_PREFIX))
                {
                    continue;
                }

                const string animationPrefix = @"=== ";
                if (line.StartsWith(animationPrefix))
                {
                    _animationTitle = line.Substring(animationPrefix.Length);
                    _animationTitle = _animationTitle.Replace(@" ===", "");
                }

                var fragments = line.Split(SEP_TIME_VALUE);
                try
                {
                    switch (fragments.Length)
                    {
                    case 1:
                        fragments = fragments[0].Split(';');

                        if (fragments.Length != 6)
                        {
                            continue;
                        }

                        AddValue(fragments[0], modeValueBuf, ModeValues);
                        AddValue(fragments[1], noiceOffsetValueBuf, NoiseOffsetValues);
                        AddValue(fragments[2], sensorValueBuf, SensorValues);
                        AddValue(fragments[3], rawSensorValueBuf, rawSensorValues, _animationTitle);
                        _animationTitle = null;
                        AddValue(fragments[4], maxSensorValueBuf, MaxSensorValues);
                        AddValue(fragments[5], songAvgValueBuf, songAvgValues);
                        break;

                    case 2:
                        DataItem di;
                        di.Time  = Parse(fragments[0]);
                        di.Value = Parse(fragments[1]);

                        if (di.Time < _lastTime)
                        {
                            continue;
                        }
                        _lastTime = di.Time;

                        SensorValues.Add(di.Value);
                        break;
                    }
                }
                catch (FormatException)
                {
                }
            }

            ModeValues.AddRange(modeValueBuf); modeValueBuf.Clear();
            SensorValues.AddRange(sensorValueBuf); sensorValueBuf.Clear();
            MaxSensorValues.AddRange(maxSensorValueBuf); maxSensorValueBuf.Clear();
            NoiseOffsetValues.AddRange(noiceOffsetValueBuf); noiceOffsetValueBuf.Clear();
            rawSensorValues.AddRange(rawSensorValueBuf); rawSensorValueBuf.Clear();
            songAvgValues.AddRange(songAvgValueBuf); songAvgValueBuf.Clear();
        }