View difference between Paste ID: xjgWBTxB and UfMVd6q9
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Windows.Forms;
10
using System.Diagnostics;
11
using System.Windows.Forms.DataVisualization.Charting;
12
13
namespace WEEK2
14
{
15
    public partial class Form1 : Form
16
    {
17
        #region global variables
18
        float readBuffer1;
19
        
20
        private string readBuffer = string.Empty;  // buffer to store characters
21
     
22
        
23
        #endregion
24
        public Form1()
25
        {
26
            InitializeComponent();                 //intialize form1
27
        }
28
29
        #region connect to serial port open port set parameters and run timer
30
        private void button1_Click(object sender, EventArgs e)
31
32
        {
33
            if (!serialPort1.IsOpen)
34
            {
35
                serialPort1.PortName = "COM4";          //look in your PC wich comport is connected      (device manager)
36
                serialPort1.Open();                     //open serialPort1
37
                richTextBox1.Text = "comport opened ";  //show in textbox that the comport is opened
38
                serialPort1.ReceivedBytesThreshold = 20; //threshold 20 bytes received  event trigger
39
                //serialPort1.NewLine = "\r";            //te onstabiel als ik deze laat staan.   // last char to be recognised
40
                serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(SerialPort1_DataReceived);   
41
                //timer1.Enabled = true;
42
            }
43
        }
44
45
        #endregion
46
47
        #region read databuffer received
48
        private void SerialPort1_DataReceived(System.Object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
49
        {
50
51
            byte[] data1 = new byte[serialPort1.BytesToRead];           //stukje code om binaire
52
            serialPort1.Read(data1, 0, data1.Length);                   //waardes te kunnen lezen
53
            readBuffer = System.Text.Encoding.UTF8.GetString(data1);    //en gebruiken.
54
            readBuffer1 = serialPort1.ReadByte();   //readByte
55
56
            Invoke(new EventHandler(DoUpdate));            //update
57
58
        }
59
        #endregion
60
61
        #region update the text in textbox enable timer for next
62
        public void DoUpdate(object sender, System.EventArgs e)
63
        {
64
            byte data = (byte)serialPort1.ReadByte();
65
            // double voltage = Math.Round(data * (5.0 / 255)); //berekening om de spanning weer te geven, ,2 voor 2 decimalen en round rond de cijfers af 
66
            //readBuffer = voltage.ToString();  //convert voltage to string and put in readBuffer
67
            readBuffer = data.ToString();
68
            //richTextBox1.Text = readBuffer;//show value
69
            Volt.Text = readBuffer;         //show value on the label 
70
        }
71
72
        #endregion
73
74
        private void Volt_Click(object sender, EventArgs e)
75
        {
76
77
        }
78
79
        #region button close; close serial port timer off give messagebox
80
        private void button2_Click_1(object sender, EventArgs e)
81
        {
82
            {
83
                serialPort1.Close();                        //close serialPort1
84
                richTextBox1.Text = "serial port closed";   //show text in richtextbox
85
                MessageBox.Show("Temperature meter Thiemo Hoek"); //show message box 
86
                this.Close();       //close program
87
            }
88
        }
89
90
        private void label1_Click(object sender, EventArgs e)
91
        {
92
93
        }
94
95
        private void chart1_Click(object sender, EventArgs e)
96
        {
97
98
            string x1 = Volt.Text;
99
            var chart = chart1.ChartAreas[0];
100
            chart.AxisX.IntervalType = DateTimeIntervalType.Number;
101
102
            chart.AxisX.LabelStyle.Format = "";
103
            chart.AxisY.LabelStyle.Format = "";
104
            chart.AxisY.LabelStyle.IsEndLabelVisible = true;
105
            chart.AxisX.Minimum = 0;
106
            chart.AxisX.Maximum = 20;
107
            chart.AxisY.Minimum = 0;
108
            chart.AxisY.Maximum = 50;
109
            chart.AxisX.Interval = 1;
110
            chart.AxisY.Interval = 5;
111
112
            chart1.Series.Add("degrees");
113
            chart1.Series["degrees"].ChartType = SeriesChartType.Line;
114
            chart1.Series["degrees"].Color = Color.Red;
115
            chart1.Series[0].IsVisibleInLegend = false;
116
117
            for(int x = 0; x<21; x++)
118
            {
119
                chart1.Series["degrees"].Points.AddXY(x,x1);
120
121
            }
122
            
123
            
124
        
125
        }
126
    }
127
}
128
        #endregion