ZedGraph C#串行数据图

时间:2017-02-21 10:24:27

标签: c# serial-port zedgraph

我是C#和绘图世界的新手。我试图将来自Arduino的串行数据绘制为C#中ZedGraph上的双数据类型。我无法绘制它。图表只显示Y轴刻度设置本身的输出值,但没有别的。我将非常感谢你的帮助!!谢谢。以下是我的代码 PS:我放了一个消息框来查看输出,它显示了Arduino的正确输出。

using System;
using System.Drawing;
using System.Windows.Forms;
using ZedGraph;
using System.IO.Ports;


namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {

        static string indata;
        double x;
        double d;
        public Form1()
        {
            InitializeComponent();

            PlotSerial();
           // DrawGraph();

        }

        private void PlotSerial()

        {
            SerialPort sprt;


            sprt = new SerialPort("COM4");

            sprt.BaudRate = 115200;
            try
            {
                sprt.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Check port");
            }

            // initializing zed graph
            GraphPane pane = zedGraph.GraphPane;

            PointPairList list2 = new PointPairList();
            pane.YAxisList.Clear();

            //X-Axis Settings
            pane.XAxis.Scale.MinorStep = 1;
            pane.XAxis.Scale.MajorStep = 5;
                        XDate time = new XDate(DateTime.Now.ToOADate());
                        pane.XAxis.Type = AxisType.Date;
                        pane.XAxis.Scale.Format = "ss";
                        pane.XAxis.Scale.Min = new XDate(DateTime.Now);
                        pane.XAxis.Scale.Max = new XDate(DateTime.Now.AddSeconds(25));
                        pane.XAxis.Scale.MinorUnit = DateUnit.Second;
                        pane.XAxis.Scale.MajorUnit = DateUnit.Second;

                       indata = sprt.ReadLine();
                       MessageBox.Show(indata);
                       bool result = Double.TryParse(indata, out d);

                       // double xmin = -50;
                       //double xmax = 50;

                       //for x = 0 To 36
                       //for (double x = xmin; x <= xmax; x += 1)
                       //for (int i = 1; i < 20; i++)
                       {              
                           list2.Add(x, d);
                       }


            Scale xScale1 = zedGraph.MasterPane.PaneList[0].XAxis.Scale;
                         if (time.XLDate > xScale1.Max)
                         {
                             xScale1.Max = (XDate)(DateTime.Now.AddSeconds(1));
                             xScale1.Min = (XDate)(DateTime.Now.AddSeconds(-40));
                         }
            /*  
                                    // byte[] buffer = new byte[20];
                                    indata = (byte)sprt.ReadByte();
                                    //MessageBox.Show(indata);
                                    //double xmin = -2;
                                    //double xmax =  2;
                       // for (= xmin; x <= xmax; x +=1)


                                     list2.Add(x, indata);
                            */
              pane.XAxis.Title.Text = "X Axis";
              int axis2 = pane.AddYAxis("Y Axis");
              LineItem myCurve2 = pane.AddCurve("Serialport_Curve", list2, Color.Blue, SymbolType.Diamond);
              myCurve2.YAxisIndex = axis2;
              pane.YAxisList[axis2].Title.FontSpec.FontColor = Color.Black;
             zedGraph.AxisChange();
            zedGraph.Invalidate();

        }

        private double f1(double x)
          {
              if (x == 0)
              {
                  return 1;
              }

              return Math.Sin(x) / x;
          }


          private double f2(double x)
          {
              if (x == 0)
              {
                  return 1;
              }

              return 10 * Math.Sin(x * 0.5) / x;
          }

          private double f3(double x)
          {
              if (x == 0)
              {
                  return 1;
              }

              return 0.1 * Math.Sin(x * 2) / x;
        }          
    }
}

0 个答案:

没有答案