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.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Diagnostics;
- using System.Windows.Forms.DataVisualization.Charting;
- namespace WEEK2
- {
- public partial class Form1 : Form
- {
- #region global variables
- float readBuffer1;
- private string readBuffer = string.Empty; // buffer to store characters
- #endregion
- public Form1()
- {
- InitializeComponent(); //intialize form1
- }
- #region connect to serial port open port set parameters and run timer
- private void button1_Click(object sender, EventArgs e)
- {
- if (!serialPort1.IsOpen)
- {
- serialPort1.PortName = "COM4"; //look in your PC wich comport is connected (device manager)
- serialPort1.Open(); //open serialPort1
- richTextBox1.Text = "comport opened "; //show in textbox that the comport is opened
- serialPort1.ReceivedBytesThreshold = 20; //threshold 20 bytes received event trigger
- //serialPort1.NewLine = "\r"; //te onstabiel als ik deze laat staan. // last char to be recognised
- serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(SerialPort1_DataReceived);
- //timer1.Enabled = true;
- }
- }
- #endregion
- #region read databuffer received
- private void SerialPort1_DataReceived(System.Object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
- {
- byte[] data1 = new byte[serialPort1.BytesToRead]; //stukje code om binaire
- serialPort1.Read(data1, 0, data1.Length); //waardes te kunnen lezen
- readBuffer = System.Text.Encoding.UTF8.GetString(data1); //en gebruiken.
- readBuffer1 = serialPort1.ReadByte(); //readByte
- Invoke(new EventHandler(DoUpdate)); //update
- }
- #endregion
- #region update the text in textbox enable timer for next
- public void DoUpdate(object sender, System.EventArgs e)
- {
- byte data = (byte)serialPort1.ReadByte();
- // 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
- //readBuffer = voltage.ToString(); //convert voltage to string and put in readBuffer
- readBuffer = data.ToString();
- //richTextBox1.Text = readBuffer;//show value
- Volt.Text = readBuffer; //show value on the label
- }
- #endregion
- private void Volt_Click(object sender, EventArgs e)
- {
- }
- #region button close; close serial port timer off give messagebox
- private void button2_Click_1(object sender, EventArgs e)
- {
- {
- serialPort1.Close(); //close serialPort1
- richTextBox1.Text = "serial port closed"; //show text in richtextbox
- MessageBox.Show("Temperature meter Thiemo Hoek"); //show message box
- this.Close(); //close program
- }
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void chart1_Click(object sender, EventArgs e)
- {
- string x1 = Volt.Text;
- var chart = chart1.ChartAreas[0];
- chart.AxisX.IntervalType = DateTimeIntervalType.Number;
- chart.AxisX.LabelStyle.Format = "";
- chart.AxisY.LabelStyle.Format = "";
- chart.AxisY.LabelStyle.IsEndLabelVisible = true;
- chart.AxisX.Minimum = 0;
- chart.AxisX.Maximum = 20;
- chart.AxisY.Minimum = 0;
- chart.AxisY.Maximum = 50;
- chart.AxisX.Interval = 1;
- chart.AxisY.Interval = 5;
- chart1.Series.Add("degrees");
- chart1.Series["degrees"].ChartType = SeriesChartType.Line;
- chart1.Series["degrees"].Color = Color.Red;
- chart1.Series[0].IsVisibleInLegend = false;
- for(int x = 0; x<21; x++)
- {
- chart1.Series["degrees"].Points.AddXY(x,x1);
- }
- }
- }
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement