title: C# SerialPort: ReadLine not working
body: <p>i'm having issues reading input from a serial port in C#. The device, that is connected via usb, is a ELM327-Chip for reading the OBD-port of the car. </p>
  
  <p>My Code consist of two threads: A Write- and a Read-Thread. It looks like the following:</p>
  
  <pre><code>static volatile SerialPort moPort;
  
  ...
  
  moPort = new SerialPort("COM3");
  
  moPort.BaudRate = 38400;
  moPort.Parity = Parity.None;
  moPort.DataBits = 8;
  moPort.StopBits = StopBits.One;
  moPort.NewLine = "\r";
  moPort.ReadTimeout = 5000;
  
  moPort.Open();
  </code></pre>
  
  <p>Write-Thread:</p>
  
  <pre><code>for (; ; )
  {
      foreach (string sPID in Car.getAvailablePIDs())
      {
          moPort.WriteLine("01 " + sPID);
          Thread.Sleep(50);
      }
  }
  </code></pre>
  
  <p>Read-Thread:</p>
  
  <pre><code>for (; ; )
  {
      string sResult;
      try
      {
      sResult = moPort.ReadLine();
      }
      catch (TimeoutException)
      {
      return;
      }
  }
  </code></pre>
  
  <p>This way my program is working properly. The Input I get from the Device looks like the following:</p>
  
  <pre><code>41 0C 00 1C\r
  41 1E 00\r
  &gt;NO DATA 
  </code></pre>
  
  <p>The problem just occurs when I dont use the sleep function in the main-thread. The response I receive from the device then looks like the following: </p>
  
  <pre><code>41 0C
  00 1C
  \r41 1E
  00\r
  &gt;NO
  DATA
  </code></pre>
  
  <p>It doesn't seperate the strings anymore by '\r' but just sends ... chaos.</p>
  
  <p>I have no idea what I should do or if there is anything wrong with my code. Has anyone suggestions?</p>
  
  <p>MfG Kyle</p>