Lỗi khi vẽ đồ thị ZedGraph

nguyenducrin11d2

Trứng gà
Xin chào anh chị.
em làm đề tài ổn định mực nước gửi mực nước đo được từ cảm biến siêu âm lên từ pic16f877a để vẽ độ thì với mực nước đặt. dữ liệu để vẽ em lấy từ 2 ô " gửi " và "nhận" trong c# sau đó chuyển thành kiểu double để vẽ nhưng khi vẽ bị lỗi như sau.
mực nước ko thể xuống 0 nhưng nó xuống 0 .Nhờ anh chị giúp em fix lỗi này ạ.em xin cảm ơn
sai.JPG
 

nguyenducrin11d2

Trứng gà
đây là code của em
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using ZedGraph;
 
 
namespace USART
{    public partial class frmMain : Form
    {
        int tickStart = 0;
        public static SerialPort _SerialPort = new SerialPort();
        public static String Data = "";
        private void _Read_Com()
        {
            var temp = System.IO.Ports.SerialPort.GetPortNames();//doc cong com hien co tren may tinh
            cbxCom.Items.Clear();//xoa du lieu trong o nhap cong com
            for (int i = 0; i < temp.Length; i++)
            {
                cbxCom.Items.Add(temp[i].ToString());//them cong com hien co vao o nhap cong com
            }
 
            if (cbxCom.SelectedIndex > 0)//kiem tra neu co nhieu hon 1 cong com doc duoc tren may tinh
            {
                cbxCom.SelectedIndex = 1;//nap gia tri cong com thu 2 vao o nhap cong com
            }
        }
      private void _Received_Data(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {               
                Data = _SerialPort.ReadExisting();//doc du lieu vua nhan duoc tu vi dieu khien gui len
                this.Invoke(new EventHandler(_Receiving));
               
            }
            catch (Exception)
            {
 
            }
        }
        string data1;
        private void _Receiving(object sender, EventArgs e)
        {
            if (Data == "/")
            {
                txbReceive.ResetText();//xoa trang du lieu tren bang du lieu nhan duoc
            }
            else
            {
                // Convert the input string to an int.
                int j;
               
                System.Int32.TryParse(Data, out j);
                txbReceive.Text += Data;
                data1 += Data;
                //txbReceive.SelectionStart = txbReceive.TextLength;
                //txbReceive.ScrollToCaret();
             
            }
        }
      public frmMain()
        {
            InitializeComponent();
        }
 
        private void frmMain_Load(object sender, EventArgs e)
        {
            _Read_Com();
            _SerialPort.DataReceived += new SerialDataReceivedEventHandler(_Received_Data);
            //this.reportViewer1.RefreshReport();
            GraphPane myPane = zedGraphControl1.GraphPane; // Khai báo sửa dụng Graph loại GraphPane;
            // Các thông tin cho đồ thị của mình
            myPane.Title.Text = "ỔN ĐỊNH MỰC NƯỚC-NHÓM 2";
            myPane.XAxis.Title.Text = "Time";
            myPane.YAxis.Title.Text = "Mực Nước";
            // Định nghĩa list để vẽ đồ thị. Để các bạn hiểu rõ cơ chế làm việc ở đây khai báo 2 list điểm <=> 2 đường đồ thị
            RollingPointPairList list1 = new RollingPointPairList(6000);
            // Ở đây sử dụng list với 1200 điểm (có thể thêm nhiều liệu tại đây)
            RollingPointPairList list2 = new RollingPointPairList(6000);
            // dòng dưới là định nghĩa curve để vẽ.
            LineItem curve1 = myPane.AddCurve("setpoint", list1, Color.Red, SymbolType.None); // Color màu đỏ, đặc trưng cho đường 1
            // SymbolType là kiểu biểu thị đồ thị : điểm, đường tròn, tam giác ....
            LineItem curve2 = myPane.AddCurve("current", list2, Color.Blue, SymbolType.None); //  Color màu Xanh, đặc trưng cho đường 2
 
            // ví dụ khoảng cách là 50ms 1 lần
            timer1.Interval =550;
            //timer1.Enabled = true; // Kích hoạt cho timer1
            //timer1.Start(); // Chạy Timer1
 
            // Định hiện thị cho trục thời gian (Trục X)
            myPane.XAxis.Scale.Min = 0;  // Min  = 0;
            myPane.XAxis.Scale.Max = 30; // Mã  = 30;
            myPane.XAxis.Scale.MinorStep = 1;  // Đơn vị chia nhỏ nhất 1
            myPane.XAxis.Scale.MajorStep = 5; // Đơn vị chia lớn 5
 
            // Gọi hàm xác định cỡ trục
            zedGraphControl1.AxisChange();
 
            // Khởi động timer về vị trí ban đầu
            tickStart = Environment.TickCount;
        }
        public void draw(String setpoint, string current) // Ở ví dụ này chúng ta có 2 đường
        {
            double intsetpoint;
            double intcurrent;
            double.TryParse(setpoint, out intsetpoint);
            double.TryParse(current, out intcurrent);
            if (zedGraphControl1.GraphPane.CurveList.Count <= 0)
                return;
            // Kiểm tra việc khởi tạo các đường curve
            // Đưa về điểm xuất phát
            LineItem curve1 = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
            LineItem curve2 = zedGraphControl1.GraphPane.CurveList[1] as LineItem;
            if (curve1 == null)
                return;
            if (curve2 == null)
                return;
 
            // list chứa các điểm.
            // Get the PointPairList
            IPointListEdit list1 = curve1.Points as IPointListEdit;
            IPointListEdit list2 = curve2.Points as IPointListEdit;
 
            if (list1 == null)
                return;
            if (list2 == null)
                return;
 
            // Time được tính bằng ms
            double time = (Environment.TickCount - tickStart) / 1000.0;
 
            // Tính toán giá trị hiện thị
 
            // Muốn hiện thị cái gì thì chỉ việc thay vào setpointx
            list1.Add(time, intsetpoint); // Đây chính là hàm hiển thị dữ liệu của mình lên đồ thị
                                        // list2.Add(time, setpoint2); // Đây chính là hàm hiển thị dữ liệu của mình lên đồ thị
                                        // Ko vẽ setpoint2 mà thử vẽ đồ thị hình sin với 3 seconds per cycle
            list2.Add(time, intcurrent);
            // đoạn chương trình thực hiện vẽ đồ thị
            Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
            if (time > xScale.Max - xScale.MajorStep)
            {
                //xScale.Max = time + xScale.MajorStep;
                // xScale.Min = xScale.Max - 30.0; // Timer chạy qua 30 sẽ tự động dịch chuyển tịnh tiến sang trái
                // Nếu ko muốn dịch chuyển mà chạy bắt đầu từ 0 thì : xScale.Min = 0;
                if (button2.Text == "COMPACT")
                {
                    xScale.Max = time + xScale.MajorStep;
                    xScale.Min = xScale.Max - 30.0;
                }
                else
                {
                    xScale.Max = time + xScale.MajorStep;
                    xScale.Min = 0;
                }
            }
 
            // Vẽ đồ thị
            zedGraphControl1.AxisChange();
            // Force a redraw
            zedGraphControl1.Invalidate();
        }
 
        private void frmMain_Shown(object sender, EventArgs e)
        {
 
        }
 
        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    {
                        Close();//dong chuong trinh
                        break;
                    }
            }
        }
 
        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
 
        }
 
        int check = 0;
        private void bntKetNoi_Click(object sender, EventArgs e)
        {
            if (_SerialPort.IsOpen == false)
            {
                _SerialPort.PortName = cbxCom.Text.Trim();//cai dat chon ten cong com
                _SerialPort.Open();//mo cong com
                timer1.Enabled = true;
                if (check == 0) { tickStart = Environment.TickCount; check = 1; }
 
                if (_SerialPort.IsOpen == true)
                {
                    MessageBox.Show("KẾT NỐI THÀNH CÔNG", "THÔNG BÁO");
                }
                else
                {
                    MessageBox.Show("KẾT NỐI THẤT BẠI", "THÔNG BÁO");
                }
            }
            else
            {
                MessageBox.Show("ĐÃ MỞ CỔNG COM RỒI", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void btnNgatKetNoi_Click(object sender, EventArgs e)
        {
 
            if (_SerialPort.IsOpen == false)
            {
                MessageBox.Show("CHƯA KẾT NỐI CỔNG COM", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                _SerialPort.Close();//dong cong com lai
                MessageBox.Show("ĐÃ NGẮT KẾT NỐI", "THÔNG BÁO");
            }
        }
 
        private void btnGui_Click(object sender, EventArgs e)
        {
            if (_SerialPort.IsOpen == false)
            {
                MessageBox.Show("VUI LÒNG KẾT NỐI CỔNG COM TRƯỚC", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                 _SerialPort.Write(txbSend.Text+"/");
           }}
 private void timer1_Tick(object sender, EventArgs e)
        {
            draw(txbSend.Text ,txbReceive.Text.TrimEnd());
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "COMPACT") button2.Text = "SROLL";
            else button2.Text = "COMPACT";
        }
 
       
    }
}
 
Top