title: C# Serial communication with arduino
body: <p>I'm trying to communicate with my arduino from my pc. I'm sending 1,0,0,0 in a loop and the arduino sends the received data back. However, instead of getting 1,0,0,0 back, this is what I get:</p>
  
  <pre><code>1000
  1000
  1000
  1000
  1000
  1000
  1000
  0011
  0000
  0010
  0100
  1001
  0000
  0011
  0000
  </code></pre>
  
  <p>As you can see, it works at the beginning, but starts to get weird after a few messages.
  Instead of sending 3 0's it sent 5. Why is that?</p>
  
  <p>Here is my code:</p>
  
  <p>C# Application:</p>
  
  <pre><code>class Program
  {
      SerialPort p;
  
      static void Main(string[] args)
      {
          Program p = new Program();
          p.initialize();
      }
      private void initialize()
      {
          p = new SerialPort("COM3", 115200);
          p.Open();
  
          byte[] data = { 1, 0, 0, 0 };
  
          Thread t = new Thread(reading);
          t.Start();
  
          while(true)
          { 
              p.Write(data, 0, data.Count());
          }
          Console.ReadLine();
      }
      private void reading()
      {
          while (true)
          {
              Console.WriteLine(p.ReadLine());
          }
      }
  }
  </code></pre>
  
  <p>Arduino:</p>
  
  <pre><code>void setup()
  {
      Serial.begin(115200);
      delay(5000);
      Serial.println("Program started....");
  }
  
  void loop()
  {
      for (int i = 0;i&lt;4;i++)
      {
          Serial.print(Serial.read());
      } 
      Serial.println(); 
      delay(500);
  }
  </code></pre>