Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Windows.Forms.DataVisualization.Charting;
- namespace projekt_test4
- {
- public partial class Form1 : Form
- {
- private List<Persons> Person = new List<Persons>();
- private string path = @"seznam.txt";
- int pocetHodnot = 0;
- int kresliZ = 0;
- int kresliDo = 0;
- int index = 1;
- int body = 10000;
- List<double> hodnoty = new List<double>();
- private Object nacitani = new Object();
- public Form1()
- {
- InitializeComponent();
- this.comboBox1.Items.Add("COM1");
- this.comboBox1.Items.Add("COM2");
- this.comboBox1.Items.Add("COM3");
- this.comboBox1.Items.Add("COM4");
- this.comboBox2.Items.Add("2400");
- this.comboBox2.Items.Add("4800");
- this.comboBox2.Items.Add("9600");
- this.comboBox2.Items.Add("19200");
- this.comboBox2.Items.Add("38400");
- //this.comboBox2.Text = Properties.Settings.Default.Speed;
- //this.comboBox1.Text = Properties.Settings.Default.Port;
- }
- private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
- {
- int length = this.serialPort1.BytesToRead;
- string data = this.serialPort1.ReadExisting();
- WriteData(data);
- }
- private void button3_Click(object sender, EventArgs e)
- {
- try
- {
- this.serialPort1.PortName = this.comboBox1.Text;
- this.serialPort1.BaudRate = int.Parse(this.comboBox2.Text);
- this.serialPort1.Open();
- label2.Text = "Pripojeno";
- this.serialPort1.DataReceived += serialPort1_DataReceived;
- }
- catch (System.ArgumentException)
- {
- MessageBox.Show("Nezadali jste port");
- }
- catch (System.FormatException)
- {
- MessageBox.Show("Nezadali jste cteci rychlost");
- }
- catch (IOException)
- {
- MessageBox.Show("Port neexistuje");
- }
- catch (UnauthorizedAccessException)
- {
- MessageBox.Show("Port je vyuzivan");
- }
- }
- private void button5_Click(object sender, EventArgs e)
- {
- label3.Text = "Probíhá Měření";
- }
- private void exit_btn_Click(object sender, EventArgs e)
- {
- try
- {
- Application.Exit();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void add_btn_Click(object sender, EventArgs e)
- {
- try
- {
- Persons per = PersonValue();
- this.Person.Add(per);
- FillListBox();
- Save();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (this.listBox1.SelectedIndex > -1)
- {
- Persons pers = this.Person[this.listBox1.SelectedIndex];
- this.textBox1.Text = pers.Jmeno;
- this.textBox2.Text = pers.Prijmeni;
- this.textBox3.Text = pers.Rocnik;
- if (pers.Pohlavi == "Muz") { checkBox3.Checked = true; }
- if (pers.Pohlavi == "Zena") { checkBox4.Checked = true; }
- if (pers.Vysetreni == "EMG") { checkBox2.Checked = true; }
- if (pers.Vysetreni == "EKG") { checkBox1.Checked = true; }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //this.comboBox1.Text = Properties.Settings.Default.Speed;
- //this.comboBox2.Text = Properties.Settings.Default.Port;
- registerDelegate();
- string[] lines = File.ReadAllLines(path);
- foreach (string line in lines)
- {
- Persons pers = new Persons();
- pers.SetFileString(line);
- this.Person.Add(pers);
- }
- FillListBox();
- label3.Text = "Neprobíhá měření";
- label2.Text = "Odpojeno";
- }
- private void dell_btn_Click(object sender, EventArgs e)
- {
- int osoba = listBox1.SelectedIndex;
- var seznamOsob = new List<string>(System.IO.File.ReadAllLines(path));
- seznamOsob.RemoveAt(osoba);
- File.WriteAllLines(path, seznamOsob.ToArray());
- listBox1.Items.Remove(listBox1.SelectedItem);
- listBox1.Refresh();
- }
- private void stop_btn_Click(object sender, EventArgs e)
- {
- label3.Text = "Neprobíhá měření";
- }
- private void clear_btn_Click(object sender, EventArgs e)
- {
- this.chart1.Series[0].Points.Clear();
- }
- private void export_btn_Click(object sender, EventArgs e)
- {
- chart1.Serializer.Save("Data/" + "Graf_" + DateTime.Now.ToString("dd'_'MM'_'yyyy HH'_'mm'_'ss") + ".xml"); // Uloží data do .xml
- this.chart1.SaveImage("Data/" + "Graf_" + DateTime.Now.ToString("dd'_'MM'_'yyyy HH'_'mm'_'ss") + ".png", ChartImageFormat.Png);
- }
- public Persons PersonValue()
- {
- Persons pers = new Persons();
- pers.Jmeno = this.textBox1.Text;
- pers.Prijmeni = this.textBox2.Text;
- pers.Rocnik = this.textBox3.Text;
- if (checkBox3.Checked) { pers.Pohlavi = this.checkBox3.Text; }
- else { pers.Pohlavi = this.checkBox4.Text; }
- if (checkBox1.Checked) { pers.Vysetreni = this.checkBox1.Text; }
- else { pers.Vysetreni = this.checkBox2.Text; }
- return pers;
- }
- private void Save()
- {
- string[] lines = new string[this.Person.Count];
- for (int j = 0; j < this.Person.Count; j++)
- {
- lines[j] = this.Person[j].ToFileString();
- }
- File.WriteAllLines(this.path, lines);
- }
- private void FillListBox()
- {
- this.listBox1.Items.Clear();
- foreach (Persons pers in this.Person)
- {
- this.listBox1.Items.Add(pers.Jmeno + pers.Prijmeni + pers.Rocnik);
- }
- }
- public delegate void DlgWriteToTextbox(string text);
- public DlgWriteToTextbox WriteToTextBox;
- void registerDelegate()
- {
- this.WriteToTextBox += meWriteToTextbox;
- }
- void WriteData(string text)
- {
- this.Invoke(this.WriteToTextBox, new object[] { text });
- }
- void meWriteToTextbox(string text)
- {
- lock(nacitani)
- {
- if(text.EndsWith(";"))
- {
- text.Substring(0, text.Length - 1);
- }
- if(text.StartsWith(";"))
- {
- text.Substring(1, text.Length);
- }
- string[] aktualniHodnoty = text.Split(';');
- foreach(string value in aktualniHodnoty)
- {
- if (value == "")
- {
- }
- else
- {
- hodnoty.Add(Convert.ToDouble(value));
- }
- }
- int pocetPrijatych = aktualniHodnoty.Length;
- pocetHodnot += pocetPrijatych;
- textBox4.Text = text;
- posuvOsy();
- if(label3.Text=="Probíhá Měření")
- {
- this.chart1.Series[0].Points.Clear();
- this.chart1.ChartAreas[0].AxisX.Minimum = kresliZ + 1;
- this.chart1.ChartAreas[0].AxisX.Maximum = kresliDo + 1;
- this.chart1.ChartAreas[0].AxisY.Minimum = -0.66;
- this.chart1.ChartAreas[0].AxisY.Maximum = 2;
- index = 1;
- for (int i = kresliZ; i <= kresliDo; i++)
- {
- DataPoint p = new DataPoint(i + 1, hodnoty[i]);
- this.chart1.Series[0].Points.Add(p);
- index++;
- }
- }
- }
- }
- void posuvOsy()
- {
- if(pocetHodnot>=body)
- {
- kresliDo = hodnoty.Count - 1;
- kresliZ = kresliDo - body + 1;
- }
- else
- {
- kresliZ = 0;
- kresliDo = hodnoty.Count - 1;
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- //Properties.Settings.Default.Speed = this.comboBox2.Text;
- //Properties.Settings.Default.Port = this.comboBox1.Text;
- Properties.Settings.Default.Save();
- }
- private void disconnect_btn_Click(object sender, EventArgs e)
- {
- this.serialPort1.Close();
- label2.Text = "Odpojeno";
- }
- private void button1_Click(object sender, EventArgs e)
- {
- hodnoty.Clear();
- pocetHodnot = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement