Ejemplo n.º 1
0
        public void ConnectSerialPort()
        {
            SerialConnection.SerialPortName = SettingsModel.SerialPort;
            SerialConnection.SerialBaudRate = SettingsModel.BaudRate;

            if (!SerialConnection.IsConnected())
            {
                IsConnected = true;
                MultiController.Clear();
                SerialConnection.CreateConnection();

                Stopwatch watch = new Stopwatch();
                watch.Start();
                double timecounter = 0;
                Task.Factory.StartNew(() =>
                {
                    while (SerialConnection.IsConnected())
                    {
                        try
                        {
                            string data = SerialConnection.SerialPort.ReadLine();
                            data        = data.Replace(".", ",");

                            string[] dataIn = data.Replace('\r', '\0').Replace('\n', '\0').Split(' ');

                            if (dataIn.Length > 1)
                            {
                                List <DoubleDataPoint> yValues = new List <DoubleDataPoint>();
                                List <DoubleDataPoint> xValues = new List <DoubleDataPoint>();


                                for (int i = 0; i < dataIn.Length - 1; i++)
                                {
                                    double time = 0;
                                    //time = timecounter / 1000;
                                    //timecounter++;
                                    double.TryParse(dataIn[0], out double x);
                                    time = x / 1000;

                                    double.TryParse(dataIn[i + 1], out double value);

                                    xValues.Add(time);
                                    yValues.Add(value);


                                    if (saveDataPointActive)
                                    {
                                        DataPoint dataPoint = new DataPoint
                                        {
                                            VariableName = DataModels[i].SeriesName,
                                            X            = time,
                                            Y            = value
                                        };
                                        _ = FileManager.Instance.SaveDataPoint(dataPoint);
                                    }
                                }
                                MultiController.PushData(xValues, yValues);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    watch.Stop();
                    IsConnected         = false;
                    saveDataPointActive = false;
                });
            }
        }