title: Reading data from serial Port is loosing packages
body: <p>Hello i created a program which reads serial data from a scoreboard, then depending on the string the program separates the data into different boxes on the form and then to different txt files.The purpose of this, is to use those txt files for livestreaming purposes in basketball games.
  It's the first time i work with serial data and i am not a very experienced programmer.</p>
  <p>My problem, as the title of this post suggests is that every now and then without any reason i am loosing some packages. This is happening randomly , for example in 10 second period i could 1 package while the next one none or 4.</p>
  <pre><code>private void ReadData() 
          {
              Thread MyThread = null;
             
              {
                  try
                  {
                       ThreadStart ThreadMethod = new ThreadStart(ReadFromPort);
                       MyThread = new Thread(ThreadMethod);
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine(&quot;Failed to create thread! &quot; + e.Message);
                      return;
                  }
                  try
                  {
                      MyThread.Start();
  
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine(&quot;The thread failed to start! &quot; + e.Message);
                  }
              }
          }
  
          //Recieves data and write them on textbox (optionally on a txt)
          private void ReadFromPort()
          {
              
              while (Receiver == true)
              {
                  try
                  {
                      
                      int count = ComPort.BytesToRead;
                      System.Windows.Forms.Application.DoEvents();
                      
                     
                      byte[] data = new byte[count];
                      ComPort.Read(data, 0, data.Length);           
                      currentMessage = Combine(currentMessage, data);
                      ReceivedData = (BitConverter.ToString(data)); 
                                     
                      if (count &gt; 0)
                      {
                          if (chBoxUpdate.Checked)
                          {
                              DataType = count;
                              tempData = ReceivedData;
                              this.Invoke(new EventHandler(DisplayText));
                              if (chboxTxt.Checked)
                              {
                                  this.Invoke(new EventHandler(ExportData));
                              }
                          }
                          else if (chBoxPrevious.Checked)
                          {
                              DataType = count;
                              tempData = ReceivedData;
                              this.Invoke(new EventHandler(ClearData));
                              this.Invoke(new EventHandler(DisplayText));
                              if (chboxTxt.Checked)
                              {
                                  this.Invoke(new EventHandler(ExportData));
                              }
                          }
                          
                      }
                  }
                  catch (Exception e)
                  {
  
                  }
              }
          }
  
          //Displays Text
          private void DisplayText(object sender, EventArgs e)
          {
              string temp;
              Console.WriteLine(tempData+ &quot; (&quot; + tempData.Length.ToString() + &quot;)&quot;);
             
              try
              {
                  if (tempData.Length == 38)// &amp;&amp; ReceivedData.Substring(12, 5) == &quot;03-02&quot;)
                  {
                      if (tempData.Substring(12, 5) == &quot;03-02&quot;)
                      {
                          DataText.AppendText(tempData.Substring(24, 8));
                          DataText.AppendText(&quot;\n&quot;);
                          Blink.Text = &quot;Reading...&quot;;
                          timer1.Start();
                          timer1.Enabled = true;
                      }
                  }
                  else
                  if (tempData.Length == 35)   
                  {
                      if (tempData.Substring(12, 5) == &quot;45-02&quot;)
                      {
                          AttackTime.AppendText(tempData.Substring(24, 5));
                          Blink.Text = &quot;Reading...&quot;;
                          AttackTime.AppendText(&quot;\n&quot;);
                          timer1.Start();
                          timer1.Enabled = true;
                      }
                  }
                  else
                  if (tempData.Length == 29)  
                  {
                      if (tempData.Substring(12, 5) == &quot;03-36&quot;)  
                      {
                          HomeScore.AppendText(tempData.Substring(21, 2));
                          Blink.Text = &quot;Reading...&quot;;
                          HomeScore.AppendText(&quot;\n&quot;);
                          timer1.Start();
                          timer1.Enabled = true;
                      }
                      else
                      if (tempData.Substring(12, 5) == &quot;03-46&quot;)  
                      {
                          AwayScore.AppendText(tempData.Substring(21, 2));
                          Blink.Text = &quot;Reading...&quot;;
                          AwayScore.AppendText(&quot;\n&quot;);
                          timer1.Start();
                          timer1.Enabled = true;
                      }
                  }
              }
              catch (ArgumentOutOfRangeException)
              {
  
              }
          }
  
  
  </code></pre>
  <p>Keep in mind that  tempData and ReceivedData are the same here and as the programs appears now there's not any particular reason to set tempData=ReceivedData.This is a part of a different,older code i used in the begining which i never changed but it doesn't effect the program.</p>
  <p>At first i am using a thread to enable the ReadFromPort then if the program finds that there are available data in line it displays them with the DisplayText and it's using the ExportData to export the data to a txt.I think the problem is somewhere there but as i am not very experienced i can't tell where.</p>
  <p>Are there any suggestions on how to improve my code? If you more details or information i can provide them.</p>