title: Serial port ReadFile receives repeated data
body: <p>I am working on a multi threading program to read and write to a serial port. If I test my application with Putty everything is fine. But when I test it with the created .exe-file, it doesn't work. (I start the program in VS2017 and then the .exe file)</p>
  
  <p>E.g.: My input: "Test", the output in the other window: "Teeeeeeeeeeeessssssssssttttttt".</p>
  
  <p>My code to send the data:</p>
  
  <pre><code>void SendDataToPort()
  {
  for (size_t i = 0; i &lt;= strlen(line); i++) // loop through the array until 
   every letter has been sent
  {
      try
      {
          if (i == strlen(line))  // If ever letter has been sent                 
          {                       // end it with a new line
              c = '\n';           // a new line
          }
          else
          {
              c = line[i];
          }
          WriteFile(serialHandle, &amp;c, 1, &amp;dwBytesWrite, NULL); // Send letter to serial port
      }
      catch (const exception&amp;)
      {
          cout &lt;&lt; "Error while writing.";
      }
  }
  cout &lt;&lt; endl &lt;&lt; "Sent." &lt;&lt; endl;
  }
  </code></pre>
  
  <p>In the array "line" I have the input from the user.</p>
  
  <p>My code to read the data:</p>
  
  <pre><code>int newLineCounter = 0;
  unsigned char tmp;
  while (!endCurrentRead)
  {
      ReadFile(hSerial, &amp;tmp, 1, &amp;bytesRead, NULL); // Get new letter
  
      if (tmp != '\n')
      {
          newLineCounter = 0;
          if (tmp &gt;= 32 &amp;&amp; tmp &lt;= 126)
          {
              output += tmp; // Add current letter to the output
          }
      }
      else
      {
          newLineCounter++;
          if (newLineCounter &lt; 2) // If there are more than 2 '\n' it doesn't write it down
          {
              output += tmp;
          }
          else if (newLineCounter == 2)
          {
              Print(output);
              output = receiverName;
              endCurrentRead = true;
          }
      }
  }
  </code></pre>
  
  <p>After that I write the data down with the Print(output) function:</p>
  
  <pre><code>cout &lt;&lt; output &lt;&lt; endl;
  </code></pre>
  
  <p>How I create the Handle File:</p>
  
  <pre><code>serialHandle = CreateFile(LcomPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  </code></pre>
  
  <p>Why is this happening and why is this only happening when I test it with the .exe-file and not with Putty?</p>