title: C# synchronising received datas via serial port
body: <p>Please have a look at my code below, which is a part of application which purpose is drawing an online chart  based on datas received via serial port. And that part of code is responsible for reciving and drawing:</p>
  
  <pre><code>   private void serialPort1_DataReceived(object sender,  SerialDataReceivedEventArgs e)
      {
  
                  rx_str = serialPort1.ReadTo("\r\n");
                  //rx_str = serialPort1.ReadExisting();
                  serialPort1.DiscardInBuffer();
  
              this.Invoke(new EventHandler(rx_parse));      
      }
  
       private void rx_parse(object sender, EventArgs e)
      {
  
              Match mch = Regex.Match(rx_str, ex_reg);
              data.Add(Convert.ToInt16(mch.Value.ToString());
         chart1.Series["Series1"].Points.AddY(Convert.ToDouble(data[tab_size]));
  
          tab_size++;
  
      }
  </code></pre>
  
  <p>used variables has created earlier: </p>
  
  <p>rx_str - string</p>
  
  <p>data - List of doubles</p>
  
  <p>tab_size - int</p>
  
  <p>ex_reg - used pattern of regular expression</p>
  
  <p>In this case I have real problem with synchronising received datas from the buffer.
  To testing it, I measure something via ADC in mirocontroller and send it as string to PC.</p>
  
  <p>Lets take such example - received datas can look like this: 373\r\n</p>
  
  <p>I'm using regular expression to taking only digits from received string. Very often datas get to the string rx_str from the buffer,  for that example, as: 73\r\n3, 3, \n37, \n3 etc instead of 373\r\n in every cycle. So, then I processing datas that are in wrong order and it causes wrong results in chart.</p>
  
  <p>Please give me an advise how can I synchronise it to receive the same order of signs in every time when I get datas from buffer. I mean to get the same form of received datas.
   I add that I 've checked it via terminal and everything is ok, in every time was the same form of datas. For this example - 373\r\n</p>