环境

IDE VS2015 - Visual C# 2015
下位机 STM32F429通过串口通信

包含命名空间

using System.Windows.Forms.DataVisualization.Charting;

声明变量容器

public Series RX_stream;

初始化控件和变量

  1. 在设计视图中添加chart控件添加chart控件
  2. Form1_load() 中初始化变量容器和chart
/*变量初始化*/
 Series RX_stream = new Series("RX_stream");
 
 /*数据表格初始化*/
 chart1.Series.Clear();
 RX_stream.ChartType = SeriesChartType.Column;
 RX_stream.Points.AddXY("accx", "65535");
 RX_stream.Points.AddXY("accy", "0");
 RX_stream.Points.AddXY("accz", "32768");
 RX_stream.Points.AddXY("gyrox", "225");
 RX_stream.Points.AddXY("gyroy", "8987");
 RX_stream.Points.AddXY("gyroz", "33443");
 RX_stream.Points.AddXY("T", "13221");
 chart1.Series.Add(RX_stream);
 
 /*粗暴地解决多线程安全问题*/
 System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;

在Timer中更新chart

/*清空seriers*/
 chart1.Series[0].Points.Clear();
 /*拷贝RX_stream数据缓冲区*/
 Series RX_stream = new Series("RX_stream");
 byte[] data = RX_Buffer.ToArray();

 if (data.Length!=0)//简单的数据校验
 {
 	/*数据解码*/
     int[] tmp = new int[7] ;
     for(int i = 0; i < 7; i++)
     {
         tmp[i] = (data[i*2] * 256 + data[i*2+1]);
         tmp[i] = (tmp[i] < 32768) ? (tmp[i]) : (tmp[i] - 65536);
     }
     
     /*操作chart*/
     chart1.Series.Clear();
     RX_stream.ChartType = SeriesChartType.Column;
     RX_stream.Points.AddXY("accx", tmp[0].ToString());
     RX_stream.Points.AddXY("accy", tmp[1].ToString());
     RX_stream.Points.AddXY("accz", tmp[2].ToString());
     RX_stream.Points.AddXY("gyrox", tmp[3].ToString());
     RX_stream.Points.AddXY("gyroy", tmp[4].ToString());
     RX_stream.Points.AddXY("gyroz", tmp[5].ToString());
     RX_stream.Points.AddXY("T", tmp[6].ToString());
     RX_stream.Points.AddXY("max", "32768");
     RX_stream.Points.AddXY("min", "-32768");
     chart1.Series.Add(RX_stream);
 }

感想

接触C#第三天,.NET图形效率确实比matlab高很多。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐