title: C# serialPort speed
body: <p>I am developing some monitoring tool for some kind of protocol based on serial communication.</p>
  
  <p>Serial BaudRate=187,5kb
  I use System.IO.Ports.SerialPort class.</p>
  
  <p>This protocol has 4 kinds of frames. They have 1Byte,3Bytes,6Bytes, 10-255Bytes. <strong>I can work with them but I receive them too late to respond</strong>.</p>
  
  <p>For the beginning I receive first packed after ex. 96ms (too late), and it contains about 1000B. This means 20-50 frames (too much, too late).
  Later its work more stable, 3-10Bytes but it is still too late because it contains 1-2 frames. Of Course 1 frame is OK, but 2 is too late.</p>
  
  <p>Can you point me how can I deal with it more reliable? I know it is possible.</p>
  
  <p>Revision1:</p>
  
  <p>I tried straight way:</p>
  
  <pre><code>private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  {
         if (!serialPort1.IsOpen) return;
         this.BeginInvoke(new EventHandler(this.DataReceived));
  }
  </code></pre>
  
  <p>And Backgroud worker:
  And ... new Tread(Read)
  and... always the same. Too late, too slow.
  Do I have to go back to WinApi and import some kernel32.dll functions?</p>
  
  <p>Revision 2:
  this is the part of code use in the Treading way:</p>
  
  <pre><code>int c = serialPort1.BytesToRead;
  byte[] b = new byte[c];
  serialPort1.Read(b, 0, c);
  </code></pre>
  
  <p>I guess it is some problem with stream use inside SerialPort class. Or some synchronization problem.</p>
  
  <p>Revision 3:
  I do not use both at once!! I just tried different ways.</p>
  
  <p>Regards
  MarekK</p>