Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 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.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Windows.Forms.DataVisualization.Charting;
  12.  
  13. namespace projekt_test4
  14. {
  15. public partial class Form1 : Form
  16. {
  17.  
  18. private List<Persons> Person = new List<Persons>();
  19. private string path = @"seznam.txt";
  20. int pocetHodnot = 0;
  21. int kresliZ = 0;
  22. int kresliDo = 0;
  23. int index = 1;
  24. int body = 10000;
  25. List<double> hodnoty = new List<double>();
  26. private Object nacitani = new Object();
  27.  
  28. public Form1()
  29.  
  30. {
  31. InitializeComponent();
  32. this.comboBox1.Items.Add("COM1");
  33. this.comboBox1.Items.Add("COM2");
  34. this.comboBox1.Items.Add("COM3");
  35. this.comboBox1.Items.Add("COM4");
  36.  
  37. this.comboBox2.Items.Add("2400");
  38. this.comboBox2.Items.Add("4800");
  39. this.comboBox2.Items.Add("9600");
  40. this.comboBox2.Items.Add("19200");
  41. this.comboBox2.Items.Add("38400");
  42.  
  43. //this.comboBox2.Text = Properties.Settings.Default.Speed;
  44. //this.comboBox1.Text = Properties.Settings.Default.Port;
  45. }
  46.  
  47. private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  48. {
  49. int length = this.serialPort1.BytesToRead;
  50. string data = this.serialPort1.ReadExisting();
  51. WriteData(data);
  52. }
  53.  
  54. private void button3_Click(object sender, EventArgs e)
  55. {
  56. try
  57. {
  58. this.serialPort1.PortName = this.comboBox1.Text;
  59. this.serialPort1.BaudRate = int.Parse(this.comboBox2.Text);
  60. this.serialPort1.Open();
  61. label2.Text = "Pripojeno";
  62. this.serialPort1.DataReceived += serialPort1_DataReceived;
  63. }
  64. catch (System.ArgumentException)
  65. {
  66. MessageBox.Show("Nezadali jste port");
  67. }
  68. catch (System.FormatException)
  69. {
  70. MessageBox.Show("Nezadali jste cteci rychlost");
  71. }
  72. catch (IOException)
  73. {
  74. MessageBox.Show("Port neexistuje");
  75. }
  76. catch (UnauthorizedAccessException)
  77. {
  78. MessageBox.Show("Port je vyuzivan");
  79. }
  80.  
  81.  
  82. }
  83.  
  84. private void button5_Click(object sender, EventArgs e)
  85. {
  86. label3.Text = "Probíhá Měření";
  87. }
  88.  
  89. private void exit_btn_Click(object sender, EventArgs e)
  90. {
  91. try
  92. {
  93. Application.Exit();
  94. }
  95. catch (Exception ex)
  96. {
  97. MessageBox.Show(ex.Message);
  98. }
  99. }
  100.  
  101. private void add_btn_Click(object sender, EventArgs e)
  102. {
  103. try
  104. {
  105. Persons per = PersonValue();
  106. this.Person.Add(per);
  107. FillListBox();
  108. Save();
  109.  
  110.  
  111. }
  112. catch (Exception ex)
  113. {
  114. MessageBox.Show(ex.Message);
  115. }
  116. }
  117.  
  118.  
  119. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  120. {
  121. if (this.listBox1.SelectedIndex > -1)
  122. {
  123. Persons pers = this.Person[this.listBox1.SelectedIndex];
  124. this.textBox1.Text = pers.Jmeno;
  125. this.textBox2.Text = pers.Prijmeni;
  126. this.textBox3.Text = pers.Rocnik;
  127. if (pers.Pohlavi == "Muz") { checkBox3.Checked = true; }
  128. if (pers.Pohlavi == "Zena") { checkBox4.Checked = true; }
  129. if (pers.Vysetreni == "EMG") { checkBox2.Checked = true; }
  130. if (pers.Vysetreni == "EKG") { checkBox1.Checked = true; }
  131.  
  132. }
  133. }
  134.  
  135. private void Form1_Load(object sender, EventArgs e)
  136. {
  137. //this.comboBox1.Text = Properties.Settings.Default.Speed;
  138. //this.comboBox2.Text = Properties.Settings.Default.Port;
  139. registerDelegate();
  140. string[] lines = File.ReadAllLines(path);
  141. foreach (string line in lines)
  142. {
  143. Persons pers = new Persons();
  144. pers.SetFileString(line);
  145. this.Person.Add(pers);
  146.  
  147. }
  148. FillListBox();
  149. label3.Text = "Neprobíhá měření";
  150. label2.Text = "Odpojeno";
  151. }
  152.  
  153. private void dell_btn_Click(object sender, EventArgs e)
  154. {
  155. int osoba = listBox1.SelectedIndex;
  156. var seznamOsob = new List<string>(System.IO.File.ReadAllLines(path));
  157. seznamOsob.RemoveAt(osoba);
  158. File.WriteAllLines(path, seznamOsob.ToArray());
  159. listBox1.Items.Remove(listBox1.SelectedItem);
  160. listBox1.Refresh();
  161. }
  162.  
  163. private void stop_btn_Click(object sender, EventArgs e)
  164. {
  165. label3.Text = "Neprobíhá měření";
  166. }
  167.  
  168. private void clear_btn_Click(object sender, EventArgs e)
  169. {
  170. this.chart1.Series[0].Points.Clear();
  171. }
  172.  
  173. private void export_btn_Click(object sender, EventArgs e)
  174. {
  175. chart1.Serializer.Save("Data/" + "Graf_" + DateTime.Now.ToString("dd'_'MM'_'yyyy HH'_'mm'_'ss") + ".xml"); // Uloží data do .xml
  176. this.chart1.SaveImage("Data/" + "Graf_" + DateTime.Now.ToString("dd'_'MM'_'yyyy HH'_'mm'_'ss") + ".png", ChartImageFormat.Png);
  177. }
  178. public Persons PersonValue()
  179. {
  180. Persons pers = new Persons();
  181. pers.Jmeno = this.textBox1.Text;
  182. pers.Prijmeni = this.textBox2.Text;
  183. pers.Rocnik = this.textBox3.Text;
  184. if (checkBox3.Checked) { pers.Pohlavi = this.checkBox3.Text; }
  185. else { pers.Pohlavi = this.checkBox4.Text; }
  186. if (checkBox1.Checked) { pers.Vysetreni = this.checkBox1.Text; }
  187. else { pers.Vysetreni = this.checkBox2.Text; }
  188. return pers;
  189. }
  190. private void Save()
  191. {
  192. string[] lines = new string[this.Person.Count];
  193. for (int j = 0; j < this.Person.Count; j++)
  194. {
  195. lines[j] = this.Person[j].ToFileString();
  196. }
  197. File.WriteAllLines(this.path, lines);
  198. }
  199.  
  200. private void FillListBox()
  201. {
  202. this.listBox1.Items.Clear();
  203. foreach (Persons pers in this.Person)
  204. {
  205. this.listBox1.Items.Add(pers.Jmeno + pers.Prijmeni + pers.Rocnik);
  206. }
  207. }
  208. public delegate void DlgWriteToTextbox(string text);
  209.  
  210. public DlgWriteToTextbox WriteToTextBox;
  211.  
  212. void registerDelegate()
  213. {
  214. this.WriteToTextBox += meWriteToTextbox;
  215. }
  216.  
  217. void WriteData(string text)
  218. {
  219. this.Invoke(this.WriteToTextBox, new object[] { text });
  220. }
  221.  
  222. void meWriteToTextbox(string text)
  223. {
  224. lock(nacitani)
  225. {
  226. if(text.EndsWith(";"))
  227. {
  228. text.Substring(0, text.Length - 1);
  229. }
  230. if(text.StartsWith(";"))
  231. {
  232. text.Substring(1, text.Length);
  233.  
  234. }
  235. string[] aktualniHodnoty = text.Split(';');
  236.  
  237. foreach(string value in aktualniHodnoty)
  238. {
  239. if (value == "")
  240. {
  241.  
  242. }
  243. else
  244. {
  245. hodnoty.Add(Convert.ToDouble(value));
  246. }
  247. }
  248. int pocetPrijatych = aktualniHodnoty.Length;
  249. pocetHodnot += pocetPrijatych;
  250.  
  251. textBox4.Text = text;
  252.  
  253. posuvOsy();
  254.  
  255. if(label3.Text=="Probíhá Měření")
  256. {
  257. this.chart1.Series[0].Points.Clear();
  258. this.chart1.ChartAreas[0].AxisX.Minimum = kresliZ + 1;
  259. this.chart1.ChartAreas[0].AxisX.Maximum = kresliDo + 1;
  260. this.chart1.ChartAreas[0].AxisY.Minimum = -0.66;
  261. this.chart1.ChartAreas[0].AxisY.Maximum = 2;
  262. index = 1;
  263. for (int i = kresliZ; i <= kresliDo; i++)
  264. {
  265. DataPoint p = new DataPoint(i + 1, hodnoty[i]);
  266. this.chart1.Series[0].Points.Add(p);
  267. index++;
  268. }
  269.  
  270. }
  271.  
  272.  
  273. }
  274.  
  275. }
  276. void posuvOsy()
  277. {
  278. if(pocetHodnot>=body)
  279. {
  280. kresliDo = hodnoty.Count - 1;
  281. kresliZ = kresliDo - body + 1;
  282. }
  283. else
  284. {
  285. kresliZ = 0;
  286. kresliDo = hodnoty.Count - 1;
  287. }
  288. }
  289.  
  290. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  291. {
  292. //Properties.Settings.Default.Speed = this.comboBox2.Text;
  293. //Properties.Settings.Default.Port = this.comboBox1.Text;
  294. Properties.Settings.Default.Save();
  295. }
  296.  
  297. private void disconnect_btn_Click(object sender, EventArgs e)
  298. {
  299. this.serialPort1.Close();
  300. label2.Text = "Odpojeno";
  301. }
  302.  
  303. private void button1_Click(object sender, EventArgs e)
  304. {
  305. hodnoty.Clear();
  306. pocetHodnot = 0;
  307. }
  308. }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement