Esempio n. 1
0
        private void ShowGraph(string noOfClicked, string mesValue, string type, string date, string product, string user)
        {
            lbTitle.Text = "Ölçüm No: " + noOfClicked + " - " + date;
            lbN.Text     = mesValue + " N";
            if (type == "Eğilme")
            {
                lbNmm2.Text = (Double.Parse(mesValue) * Settings.Default.BendCoef).ToString() + " N/mm²";
            }
            else if (type == "Basınç")
            {
                lbNmm2.Text = (Double.Parse(mesValue) * Settings.Default.PresCoef).ToString() + " N/mm²";
            }
            else
            {
                lbNmm2.Text = "Ölçüm tipi belli değil";
            }
            lbType.Text = product + " " + type + " Testi (" + user + ")";

            int LocX = pnChart.Width / 50;
            int LocY = pnChart.Height / 8;

            crtVls = new ChartValues <MeasureModel>();

            #region InitChart
            cartesianChart1 = new LiveCharts.WinForms.CartesianChart
            {
                Anchor = AnchorStyles.Top | AnchorStyles.Bottom
                         | AnchorStyles.Left
                         | AnchorStyles.Right,
                Location        = new Point(LocX, LocY),
                Margin          = new Padding(30),
                Name            = "cartesianChart1",
                Size            = new Size(pnChart.Width - (LocX * 2), pnChart.Height - LocY * 3),
                TabIndex        = 57,
                Text            = "cartesianChart1",
                BackColor       = System.Drawing.Color.FromArgb(0, 86, 168),
                AnimationsSpeed = TimeSpan.FromMilliseconds(250),
                Hoverable       = false,
                Series          = new SeriesCollection
                {
                    new LineSeries
                    {
                        Values            = crtVls,
                        PointGeometrySize = 4,
                        StrokeThickness   = 2,

                        Fill = new LinearGradientBrush(
                            System.Windows.Media.Color.FromArgb(0xcc, 0xff, 0xff, 0xff),
                            System.Windows.Media.Color.FromArgb(0x64, 0xff, 0xff, 0xff),
                            90),
                        Stroke = new SolidColorBrush(
                            System.Windows.Media.Color.FromArgb(0xff, 0xff, 0xff, 0xff))
                    }
                }
            };

            cartesianChart1.AxisX.Add(new Axis
            {
                LabelFormatter = value => new TimeSpan((long)value)
                                 .TotalMilliseconds.ToString() + "ms",
                Separator = new Separator
                {
                    Step = TimeSpan.FromMilliseconds(500).Ticks
                },
                MinValue = 0
            });
            cartesianChart1.AxisY.Add(new Axis
            {
                MinValue = 0
            });
            #endregion

            pnChart.Controls.Add(cartesianChart1);
            cartesianChart1.BringToFront();
            pnChart.Enabled = true;
            pnChart.Visible = true;

            try
            {
                var models = new MeasureModel[0];

                #region SQLite Reading
                connection.Open();
                string query = "SELECT * FROM graf WHERE \"No\" IS " + noOfClicked;
                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Array.Resize(ref models, models.Length + 1);
                            models[models.Length - 1] = new MeasureModel
                            {
                                Time  = TimeSpan.FromTicks(reader.GetInt64(2)),
                                Value = reader.GetFloat(1)
                            };
                        }
                        reader.Close();
                    }
                }
                connection.Close();
                #endregion


                crtVls.AddRange(models);
                cartesianChart1.AxisX[0].MaxValue = models.Last().Time.Ticks
                                                    + TimeSpan.FromMilliseconds(100).Ticks;
                models = null;
            }
            catch (Exception ex)
            {
                connection.Close();
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 2
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            string  addressActMes = addressAct;
            string  addressMaxMes = addressMax;
            CpuType cpu           = (CpuType)Enum.Parse(typeof(CpuType),
                                                        Settings.Default.CpuType);
            string ip   = Settings.Default.IP;
            short  rack = Convert.ToInt16(Settings.Default.Rack);
            short  slot = Convert.ToInt16(Settings.Default.Slot);
            Plc    plc1 = new Plc(cpu, ip, rack, slot)
            {
                ReadTimeout = 0
            };

            plc1.Open();

            if (plc1.IsConnected)
            {
                try
                {
                    Stopwatch st = new Stopwatch();
                    st.Start();
                    plc1.Write(Settings.Default.MeasureAddr.ToUpper(), true);
                    measuring = (bool)plc1.Read(Settings.Default.MeasureAddr);
                    while (measuring)
                    {
                        if (worker.CancellationPending)
                        {
                            plc1.Write(Settings.Default.MeasureAddr.ToUpper(), false);
                            st.Reset();
                            plc1.Close();
                            e.Cancel = true;
                            break;
                        }

                        long     time1   = st.ElapsedTicks;
                        uint     uintres = (uint)plc1.Read(addressActMes);
                        long     time2   = st.ElapsedTicks;
                        TimeSpan time    = TimeSpan.FromTicks((time1 + time2) / 2);
                        //int res = uintres.ConvertToInt();
                        float        res        = uintres.ConvertToFloat();
                        MeasureModel resAndTime = new MeasureModel()
                        {
                            Value = res,
                            Time  = time
                        };

                        measuring = (bool)plc1.Read(Settings.Default.MeasureAddr);
                        if (measuring)
                        {
                            worker.ReportProgress(0, resAndTime);
                        }
                    }

                    if (!worker.CancellationPending)
                    {
                        TimeSpan time3   = TimeSpan.FromTicks(st.ElapsedTicks);
                        uint     uintRes = (uint)plc1.Read(addressMaxMes);
                        float    result  = uintRes.ConvertToFloat();
                        e.Result = new MeasureModel()
                        {
                            Value = result,
                            Time  = time3
                        };

                        st.Reset();

                        plc1.Close();
                    }
                }
                catch (Exception ex)
                {
                    plc1.Close();
                    MessageBox.Show(ex.Message);
                    e.Cancel = true;
                }
            }
            else
            {
                plc1.Close();
                MessageBox.Show(msgNotConnected);
                e.Cancel = true;
            }
        }