title: High speed serial communication over serial port in C#
body: <p>I am working with visual studio 2019. I am sending data from microcontroller over uart/serial port to the PC and I want to read it on the PC side. </p>
  
  <p>The baud rate is 9600. I am using the following code. However, its very slow. I need to be able to read the code with very high speed (comparable to the baud rate I use).</p>
  
  <p>At present, I am getting 2-3 packets per second. The timer interval is set at 10ms but even if I change it to 1ms, there is no difference. I cant figure out what am I doing wrong. Help will be greatly appreciated.</p>
  
  <p>Regards,
  Salman</p>
  
  <pre><code>    public void  readCapsule(SerialPort sp)
      {
          timer1.Enabled = false;
  
          string headerStart = "";
          string headerEnd = "";
          List&lt;Int32&gt; newCoordinates = new List&lt;Int32&gt;();
  
          headerStart = sp.ReadLine();
  
          if (headerStart == "START")
          {               
              for (int i = 0; i &lt; 3; i++)
              {
                  Int32 coords = sp.ReadByte();
                  newCoordinates.Add(coords);
              }
  
              tbRead.AppendText("X: " + newCoordinates[0].ToString() + " ");
              tbRead.AppendText("Y: " + newCoordinates[1].ToString() + " ");
              tbRead.AppendText("Z: " + newCoordinates[2].ToString() + Environment.NewLine);
  
              headerEnd = sp.ReadLine();
              newCoordinates.Clear();                
          }
      }
  
      private void timer1_Tick(object sender, EventArgs e)
      {
          readCapsule(spCapsule);            
          Application.DoEvents();
          spCapsule.DiscardInBuffer();
          timer1.Enabled = true;
      }
  </code></pre>
  
  <p>My Microcontroller code is basically incrementing 3 variables and sending them in between the START and END header. I know that there are other lines being executed so the throughput wont be 9600 but the delay I experience is WAY too long to be contributed from the microcontroller side I think. The microcontroller code is below. I am using Atmega328p. I have verified on hyper terminal that the incoming data is way faster than the rate at which I read with my C# code:</p>
  
  <pre><code>while (1) {
  
      counter++; val1 = val1 + 3; val2 = val2 + 3; val3 = val3 + 3; 
  
      if(counter &gt;= 100) {
  
          counter = 0;
          val1 = 1; val2 = 2; val3 = 3;
  
          }
  
      transmitUart0('S'); transmitUart0('T'); transmitUart0('A'); transmitUart0('R'); transmitUart0('T');transmitUart0(0x0A);
      transmitUart0(val1);
      transmitUart0(val2);
      transmitUart0(val3);
      transmitUart0('E'); transmitUart0('N'); transmitUart0('D'); transmitUart0(0x0A);
  
      _delay_ms(10);
  
  }
  </code></pre>