title: C# Serial Port Communication Arduino
body: <p>I have a project where I need to communicate with Arduino over the serial ports. The problem I face is that I cannot print continously the data I receive from the serial monitor on multiple lines on a richtextbox. When I press the button "Reveice" I do receive only one value and after this, pressing again the Receive button will overwrite the line.</p>
  
  <p>I'm tring to fix this for few days, but it's my first time programming in c# so I'm asking for your help.</p>
  
  <p>The code:</p>
  
  <pre><code>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.IO.Ports;
  
  namespace aplicatie_comanda_v1
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
              getAvilablePorts();
  
  
          }
  
          private void textBox1_TextChanged(object sender, EventArgs e)
          {
  
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
  
          }
  
          private void button2_Click(object sender, EventArgs e)
          {
  
              serialPort1.Close();
              progressBar1.Value = 0;
              button3.Enabled = true;
              button1.Enabled = false;
              receive.Enabled = false;
              richTextBox1.Clear();
  
          }
  
          private void label1_Click(object sender, EventArgs e)
          {
  
          }
          void getAvilablePorts()
          {
              string[] ports = SerialPort.GetPortNames();
              comboBox1.Items.AddRange(ports);
          }
  
          private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
          {
  
  
          }
  
          private void button3_Click(object sender, EventArgs e)
          {
              try
              {
                  if (comboBox1.Text == "" || comboBox2.Text == "" &amp;&amp; serialPort1 != null &amp;&amp; serialPort1.IsOpen)
                  {
                      richTextBox1.Text = "Select COM port and BAUD rate !";
                      serialPort1.Close();
                  }
                  else
                  {
                      string cmd = Convert.ToString(comboBox1.Text);
                      int baud = Convert.ToInt32(comboBox2.Text);
                      serialPort1.PortName = cmd;
                      serialPort1.BaudRate = baud;
                      serialPort1.DtrEnable = true;
                      serialPort1.RtsEnable = true;
                      serialPort1.Open();
                      progressBar1.Value = 100;
                      button1.Enabled = true;
                      button2.Enabled = true;
                      textBox1.Enabled = true;
                      button3.Enabled = false;
  
                  }
              }
              catch (UnauthorizedAccessException)
              {
                  richTextBox1.Text = "Unauthorized !";
              }
          }
  
          private void textBox2_TextChanged(object sender, EventArgs e)
          {
  
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
  
              string text = textBox1.Text;
              serialPort1.Write(text);
  
          }
  
          private void receive_Click(object sender, EventArgs e)
          {
              try
              {
                  richTextBox1.Text = serialPort1.ReadLine() + "\n";
  
              }
              catch (TimeoutException)
              {
                  richTextBox1.Text = "Timeout !";
              }
          }
  
          private void button4_Click(object sender, EventArgs e)
          {
  
              serialPort1.Write("w");
          }
  
          private void button5_Click(object sender, EventArgs e)
          {
              serialPort1.Write("s");
          }
  
          private void trackBar1_Scroll(object sender, EventArgs e)
          {
  
          }
  
          private void button6_Click(object sender, EventArgs e)
          {
              serialPort1.Write("a");
          }
  
          private void button7_Click(object sender, EventArgs e)
          {
              serialPort1.Write("d");
          }
  
          private void button12_Click(object sender, EventArgs e)
          {
              serialPort1.Write("b");
          }
  
          private void button13_Click(object sender, EventArgs e)
          {
              string cmd = Convert.ToString(trackBar1.Value);
              serialPort1.Write(cmd);
          }
  
          private void button8_Click(object sender, EventArgs e)
          {
              serialPort1.Write("q");
          }
  
          private void button11_Click(object sender, EventArgs e)
          {
              serialPort1.Write("e");
          }
  
          private void button9_Click(object sender, EventArgs e)
          {
              serialPort1.Write("z");
          }
  
          private void button10_Click(object sender, EventArgs e)
          {
              serialPort1.Write("c");
          }
  
          private void richTextBox1_TextChanged(object sender, EventArgs e)
          {
  
  
          }
  
          private void checkBox1_CheckedChanged(object sender, EventArgs e)
          {
              while (serialPort1.IsOpen)
              {
                  try
                  {
                      string date = serialPort1.ReadLine();
                      richTextBox1.Text = date + "\n";
  
                  }
                  catch (TimeoutException)
                  {
                      richTextBox1.Text = "Timeout !";
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Print screen of the final app: <a href="https://i.imgur.com/5f8EOly.png" rel="nofollow noreferrer">http://i.imgur.com/5f8EOly.png</a></p>
  
  <p>Thank you !</p>