SHARE
TWEET

Untitled

a guest Apr 21st, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.IO.Ports;
  10. using System.Windows.Forms;
  11.  
  12. namespace assignment2
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         string[] ports = SerialPort.GetPortNames();
  17.         string readBuffer = "nebi";
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.             comboBox1.DataSource = ports;
  22.         }
  23.        
  24.  
  25.         private void connectButton_Click(object sender, EventArgs e)
  26.         {
  27.             if (comboBox1.SelectedIndex > -1)
  28.             {
  29.                 connect(comboBox1.SelectedItem.ToString());
  30.             }
  31.             else
  32.             {
  33.                 MessageBox.Show("Please select a port first");
  34.             }
  35.         }
  36.  
  37.         public void connect(string portName)
  38.         {
  39.            // var port = new SerialPort(portName);
  40.             if (!serialPort1.IsOpen)
  41.             {
  42.                 serialPort1.PortName = portName;
  43.                 serialPort1.Open();
  44.                 textBox1.Text = "serial connections";
  45.                // serialPort1.ReceivedBytesThreshold = 20;
  46.                 serialPort1.NewLine = "\r";
  47.                 serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
  48.                 //Continue here....
  49.             }
  50.             else
  51.             {
  52.                 System.Console.WriteLine("we zitten er niet in jonge");
  53.             }
  54.         }
  55.         private void serialPort1_DataReceived(System.Object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  56.         {
  57.            
  58.  
  59.             readBuffer = serialPort1.ReadLine();          //read the received buffer into readbuffer
  60.            
  61.             Invoke(new EventHandler(DoUpdate)); // invoke new event to update the textbox
  62.          
  63.         }
  64.  
  65.         public void DoUpdate(object sender, System.EventArgs e)
  66.         {
  67.             textBox1.Text = readBuffer;//show value
  68.            
  69.             // this.chart1.Series["Temp"].Points.AddXY("16.37", readBuffer);
  70.         }
  71.  
  72.         private void closeButton_Click(object sender, EventArgs e)
  73.         {
  74.             serialPort1.Close();
  75.             textBox1.Text = "serial port closed";
  76.             this.Close();
  77.         }
  78.     }
  79. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top