1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| 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.Windows.Forms.DataVisualization.Charting;
using System.Threading;
using System.IO.Ports;
using System.IO;
namespace source_reel
{
public partial class Form1 : Form
{
private delegate void CanlJust();
private List<float> _valueList;
private List<string> _stringList;
private Thread _thread;
private CanlJust _dolt;
// private int bytesRead;
private Random _ran;
private int _interval;
private List<double> _timeList;
private List<int> _customValueList;
// private string zak=null;
// char[] zak = new char[20];
//private double a ;
private string zak=null ;
private string reader ;
// private List<string> listofIDs;
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SerialPort sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
sp.Open();
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false;
chart1.Series[0].XValueType = ChartValueType.Time;
chart1.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Seconds;
chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;
chart1.ChartAreas[0].AxisX.Interval = 0;
_valueList = new List<float>();
// _stringList = new List<string>();
_ran = new Random();
//double a = Single.Parse(zak);
_interval = 800;
// serial();
Goboy();
_timeList = new List<double>();
_customValueList = new List<int>();
}
void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string w = sp.ReadLine();
zak = w.Replace(".",",");
// _stringList.Add(w);
//_valueList.Add(int );
// MessageBox.Show(_valueList.ToString());
// List<string> listofIDs = _stringList.ToList();
if (zak != String.Empty)
{
Invoke(new Action(() => richTextBox1.AppendText(zak)));
//richTextBox1.Text.Replace('.', ',');
//Invoke(new Action(() => textBox1.AppendText(zak)));
}
//zak1 = string.Copy(zak);
}
private void Goboy()
{
_dolt += new CanlJust(AddData);
DateTime now = DateTime.Now;
chart1.ChartAreas[0].AxisX.Minimum = now.ToOADate();
chart1.ChartAreas[0].AxisX.Maximum = now.AddSeconds(5).ToOADate();
_thread = new Thread(new ThreadStart(ComeOnYouThread));
_thread.Start();
}
private void ComeOnYouThread()
{
while (true)
{
try
{
chart1.Invoke(_dolt);
Thread.Sleep(_interval);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Exception ; " + e.ToString());
}
}
}
private void AddData()
{
DateTime now = DateTime.Now;
// StreamReader zak = new StreamReader(richTextBox1.Text);
//richTextBox1.Text = zak.ReadLine();
// line
// try
//{
// if (int.TryParse(monstring, out num))
// line = Convert.ToInt32(zak);
// }
//catch {
//}
// bool success = Int32.TryParse(richTextBox1.Text, out val);
// richTextBox1.
//int number = Convert.ToInt32(richTextBox1.Text);
// zak = richTextBox1.Text.Replace('.', ',');
//string zak1;
// zak1 = textBox1.Text;
//if (string.IsNullOrEmpty(richTextBox1.Text))
//{
// richTextBox1.Clear();
//StringReader reader;
// reader = new StringReader(richTextBox1.Text);
if (textBox1.Text != String.Empty)
{
_valueList.Add(float.Parse("54"));
}
else {
_valueList.Add(float.Parse("20,45857"));
}
// MessageBox.Show(zak1);
//}
//else
//{
// _valueList.Add(float.Parse(richTextBox1.Text));
//}
// _valueList.Add(double.Parse(textBox1.Text.Replace('.',',')));
chart1.ResetAutoValues();
if (chart1.Series[0].Points.Count > 0)
{
while (chart1.Series[0].Points[0].XValue < now.AddSeconds(-5).ToOADate())
{
chart1.Series[0].Points.RemoveAt(0);
chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum = now.AddSeconds(20).ToOADate();
}
}
try
{
chart1.Series[0].Points.AddXY(now.ToOADate(), _valueList[_valueList.Count - 1]);
chart1.Invalidate();
}
catch
{
}
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
if (_thread != null)
_thread.Abort();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
} |
Partager