title: C#: splitting incoming serial data and output to separate textbox
body: <p>I am reading in data from a serial port in C#. Currently I have two sensors on my micro-controller, and I am receiving data from both sensors in my application.</p>
  
  <p>The incoming looks like so:
  <code>:*123*188;</code> where,
  <code>:</code> represents start of data</p>
  
  <p><code>*</code> first occurrence indicates sensor 1 data</p>
  
  <p><code>*</code> second occurrence indicates sensor 2 data</p>
  
  <p><code>;</code> represents end of data</p>
  
  <p>To get rid of the punctuations I have used:</p>
  
  <pre><code>var DataPacket = SensorData.Split(new[]{":",";"},StringSplitOptions.RemoveEmptyEntries) 
  </code></pre>
  
  <p>Now I have left: *123*188. How do I separate the two sensors data and put them in different textbox.</p>
  
  <p>I have tried: </p>
  
  <pre><code>foreach(var Data in DataPacket)
  {
      string bits[] = Data.Split(new[] {"*"},StringSplitOptions.RemoveEmptyEntries);     
  }
  </code></pre>
  
  <p>But, <code>textbox1.Text = bits[0]</code> is reading both sensor 1 and sensor 2 values in the same box. And <code>bits[1]</code> is out of bound.</p>
  
  <p>Please guide me to solving this.</p>
  
  <p>Edit: Here is the extended code that reads data and tries to split it accordingly:</p>
  
  <p>private void OnReceive(object sender, System.IO.Ports.SerialDataReceivedEventArgs s)
          {
                  //FOR 1 SENSOR
                  string SensorData = AvailablePort.ReadLine();
                  this.BeginInvoke(new LineReceivedEvent(LineReceived), SensorData);</p>
  
  <pre><code>    }
  
      private delegate void LineReceivedEvent(string SensorData);
  
      private void LineReceived(string SensorData)
      {
  
         /*** FOR SECOND SENSOR****************/ 
          int startReading = SensorData.IndexOf(":");
          int endReading = SensorData.IndexOf(";");
          /********                   ************/
  
          //int userRange = Convert.ToInt32(Temp_Range.SelectedValue);
          //int data = Int32.Parse(SensorData);
  
  
          /*** FOR SECOND SENSOR ************************************************/
          var DataPacket = SensorData.Split(new[] { ":", ";" }, StringSplitOptions.None);
  
          foreach (var Data in DataPacket)
          {
              //textBox1.Text = Data;
              //TRYING TO READ MORE THAN 1 SENSOR HERE
              var newData = SensorData.Split(new[] { "*" }, StringSplitOptions.None);
              while(true)
              {
                  foreach (var data in newData)
                  {
                      textBox1.Text = data[0].ToString();
                  }
              } 
          }
  </code></pre>