Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ZedGraph;
  9. using System.IO.Ports;
  10. using System.Threading;
  11.  
  12. namespace DynamicData
  13. {
  14. public partial class Form1 : Form
  15. {
  16. private SerialPort port;
  17. private string buffer = "";
  18.  
  19. private void connect()
  20. {
  21. port = new SerialPort("COM8", 115200, Parity.None, 8, StopBits.One);
  22. port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
  23. if (!port.IsOpen) port.Open();
  24. }
  25.  
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. }
  30.  
  31.  
  32. private void Form1_Load( object sender, EventArgs e )
  33. {
  34. connect();
  35. GraphPane myPane = zedGraphControl1.GraphPane;
  36. RollingPointPairList list = new RollingPointPairList(500);
  37. LineItem curve = myPane.AddCurve( "Sensor", list, Color.Blue, SymbolType.None );
  38.  
  39. myPane.XAxis.Scale.Min = 0;
  40. myPane.XAxis.Scale.Max = 10;
  41. myPane.YAxis.Scale.Min = 0;
  42. myPane.YAxis.Scale.Max = 300;
  43. myPane.XAxis.Scale.MinorStep = 0.5;
  44. myPane.XAxis.Scale.MajorStep = 1;
  45.  
  46. zedGraphControl1.AxisChange();
  47.  
  48. }
  49.  
  50.  
  51. private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  52. {
  53. //sample data: ;100*100000:
  54. //sampling rate ~100Hz
  55. buffer += port.ReadExisting();
  56.  
  57. //flush incomplete package
  58. while (buffer[0] != ';')
  59. {
  60. buffer = buffer.Remove(0, 1);
  61. if (buffer.Length < 1) break;
  62. }
  63.  
  64. //got a complete package, go to data handling
  65. while (buffer.Contains(":"))
  66. {
  67. DataHandling();
  68. }
  69. }
  70.  
  71.  
  72. private void DataHandling()
  73. {
  74. string[] nameArray = buffer.Split(new[] { ";", ":", "*" }, StringSplitOptions.RemoveEmptyEntries);
  75.  
  76. //plot sensor data vs. time
  77. draw(Convert.ToInt32(nameArray[0]), Convert.ToInt32(nameArray[1]));
  78.  
  79. //remove handled package in buffer
  80. var index = buffer.IndexOf(":");
  81. buffer = buffer.Remove(0, index + 1);
  82.  
  83. }
  84.  
  85. double time = 0;
  86. private void draw(int sensor, int t)
  87. {
  88. //convert tick to sec (uP clock rate = 16MHZ)
  89. time = time + (t / 16000000.0);
  90.  
  91. // Get the first CurveItem in the graph
  92. LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
  93.  
  94. // Get the PointPairList
  95. IPointListEdit list = curve.Points as IPointListEdit;
  96. list.Add(time, sensor);
  97.  
  98.  
  99. //Keep the X scale at a rolling 10 second interval, with one
  100. //major step between the max X value and the end of the axis
  101. Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
  102. if (time > xScale.Max - xScale.MajorStep)
  103. {
  104. xScale.Max = time + xScale.MajorStep;
  105. xScale.Min = xScale.Max - 10.0;
  106. }
  107.  
  108. //Display sensor data
  109. this.Invoke(new Action(() => { textBox1.Text = byte1.ToString(); }));
  110.  
  111. axisChangeZedGraph(zedGraphControl1);
  112.  
  113. }
  114.  
  115. delegate void axisChangeZedGraphCallBack(ZedGraphControl zg);
  116. private void axisChangeZedGraph(ZedGraphControl zg)
  117. {
  118. if (zg.InvokeRequired)
  119. {
  120. axisChangeZedGraphCallBack ad = new axisChangeZedGraphCallBack(axisChangeZedGraph);
  121. zg.Invoke(ad, new object[] { zg });
  122. }
  123. else
  124. {
  125. // zg.AxisChange();
  126. zg.Invalidate();
  127. zg.Refresh();
  128. }
  129. }
  130.  
  131. }
  132. }
  133.  
  134. //this is just to add a point to the plot, the curve object should have already been created
  135. private void draw(int sensor, int t)
  136. {
  137. //convert tick to sec (uP clock rate = 16MHZ)
  138. time = time + (t / 16000000.0);
  139.  
  140. //curve should be a module-level variable already set up with proper formatting,
  141. //just no points yet
  142. curve.AddPoint(time, sensor);
  143.  
  144. //Display sensor data
  145. this.Invoke(new Action(() => { textBox1.Text = byte1.ToString(); }));
  146.  
  147. zg.AxisChange();
  148. zg.Refresh();
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement