title: SerialPort data loss - C#
body: <p>I'm working on a SerialPort app and one very simple part of it is giving me issues. I simply want to read a constant stream of data from the port and write it out to a binary file as it comes in. The problem seems to be speed: my code has worked fine on my 9600 baud test device, but when carried over to the 115200bps live device, I seem to be losing data. What happens is after a variable period of time, I miss 1 byte which throws off the rest of the data. I've tried a few things:</p>
  
  <pre><code>private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  {
      bwLogger.Write((byte)serialPort1.ReadByte());
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  {
      byte[] inc = new byte[serialPort1.BytesToRead];
      serialPort1.Read(inc, 0, inc.Length);
  
      bwLogger.Write(inc);
  }
  </code></pre>
  
  <p>and a few variations. I can't use ReadLine() as I am working with a constant stream of data (right?). I've tried fiddling with the buffer size (both serialPort1.ReadBufferSize and the hardware FIFO buffer). Ideally, for usability purposes, I'd handle this on the software side and not make the user have to change Windows driver settings.</p>
  
  <p>Any ideas?</p>