Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. public partial class Form1 : Form
  2. {
  3. SerialPort seriPort;
  4. public Form1()
  5. {
  6. InitializeComponent();
  7. seriPort = new SerialPort();
  8. seriPort.BaudRate = 9600;
  9. }
  10.  
  11. private void button1_Click(object sender, EventArgs e)
  12. {
  13. timer1.Start();
  14. try
  15. {
  16. seriPort.PortName = textBox1.Text;
  17. if (!seriPort.IsOpen)
  18. MessageBox.Show("Bağlantı Kuruldu");
  19. }
  20. catch
  21. {
  22. MessageBox.Show("Bağlantı Kurulmadı!");
  23. }
  24. }
  25.  
  26. private void timer1_Tick(object sender, EventArgs e)
  27. {
  28. try
  29. {
  30. seriPort.Write("temperature");
  31. float sıcaklık = Convert.ToByte(seriPort.ReadExisting());
  32. textBox2.Text = sıcaklık.ToString();
  33. comboBox1.Items.Add(textBox2.Text);
  34. System.Threading.Thread.Sleep(100);
  35. }
  36. catch (Exception) {}
  37. }
  38.  
  39. private void button2_Click(object sender, EventArgs e)
  40. {
  41. timer1.Stop();
  42. seriPort.Close();
  43. }
  44. }
  45.  
  46. seriPort.PortName = textBox1.Text;
  47. // open the port
  48. seriPort.Open();
  49.  
  50. seriPort.Write("temperature");
  51. // wait for the response
  52. System.Threading.Thread.Sleep(2000);
  53. float sıcaklık = Convert.ToByte(seriPort.ReadExisting());
  54.  
  55. SerialPort port = new SerialPort();
  56. port.DataReceived += Port_DataReceived;
  57.  
  58. private static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
  59. {
  60.  
  61. float sıcaklık = Convert.ToByte(seriPort.ReadExisting());
  62. // do what ever you want with this value
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement