Advertisement
Guest User

Arduino interfacing cSharp

a guest
Jan 9th, 2015
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. add using System.IO.Ports;
  2. add SerialPort port = new SerialPort();
  3.  
  4. public Form1()
  5. {
  6.     button1.Enable = true; // Connect Button
  7.     button2.Enable = flase; // ON button
  8.     button3.Enable = false; // OFF Button
  9.    
  10.     InitializeComponent();
  11. }
  12.  
  13. private void button1_Click(object sender, EventArgs e)
  14. {
  15.     port.BaudRate = 9600;
  16.     port.PortName = textbox1.text;
  17.    
  18.     try
  19.     {
  20.         port.Open();
  21.         // if connected
  22.         button1.Enable = false; // Connect button disable.
  23.         button2.Enable = true; // On Button Enable.
  24.     } catch (Exception ex)
  25.     {
  26.         MessageBox.Show(ex.Message, "ERROR OCCURRED");
  27.     }
  28. }
  29.  
  30. private void button2_Click(object sender, EventArgs e)
  31. {
  32.     port.WriteLine("ON");
  33.     button2.Enable = false;
  34.     button3.Enable = true;
  35. }
  36.  
  37. private void button3_Click(object sender, EventArgs e)
  38. {
  39.     port.WriteLine("OFF");
  40.     button2.Enable = true;
  41.     button3.Enable = false;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement