title: Display data on the listbox from COM Port
body: <p>I've 2 buttons. Button Connect(open com port) &amp; Disconnect(close port). When I click on the connect button, the timer will trigger &amp; display the data on Listbox from the com port every few seconds. When I click disconnect and click connect button again, the data will not be displayed on the Listbox and the timer is not triggered. Why is that so?</p>
  
  <p>Here's my codes:</p>
  
  <pre><code>    List&lt;byte&gt; buffer = new List&lt;byte&gt;(125);
      private void btnConnect_Click(object sender, EventArgs e)
      {
  
          if (serialPortN.IsOpen)
              serialPortN.Close();
          try
          {
              {
  
                  serialPortN.PortName = "COM8";
                  serialPortN.BaudRate = 9600;
                  serialPortN.Parity = Parity.None;
                  serialPortN.DataBits = 8;
                  serialPortN.StopBits = StopBits.One;
                  serialPortN.Encoding = System.Text.Encoding.ASCII;
                  serialPortN.ReadTimeout = 500;
              }
  
              serialPortN.Open();
              label1.Text = "COM8" + " is opened";
  
  
              System.Timers.Timer timer = new System.Timers.Timer();
              timer.Interval = 5000;
              timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
              timer.Start();
  
              serialPortN.DataReceived += new SerialDataReceivedEventHandler(datareceived);
  
  
              btnDisconnect.Enabled = true;
              btnConnect.Enabled = false;
  
          }
  
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message.ToString());
          }
      }
       void datareceived(object sender, SerialDataReceivedEventArgs e)
      {
          myDelegate d = new myDelegate(update);
          listBox1.Invoke(d, new object[] { });
  
      }
  
      public void update()
      {
  
          while (serialPortN.BytesToRead &gt; 0)
              buffer.Add((byte)serialPortN.ReadByte());
          // Call a routine to process the data.          
          ProcessBuffer(buffer);
       }
  
    private void ProcessBuffer(List&lt;byte&gt; buffer)
      {
         int numberOfBytesToRead = 125;
          if (buffer.Count &gt;= numberOfBytesToRead)
          {
              this.Invoke(new Action(() =&gt;
               Console.WriteLine(string.Format("SPO = {0}, PulseRate = {1}, Time = {2}",
                                                      buffer[43].ToString(),
                                                      buffer[103].ToString(),
                                                      DateTime.Now.ToString()
              ))));
  
              buffer.RemoveRange(0, numberOfBytesToRead);
          }
  
      }
  
     void TimerElapsed(object sender, ElapsedEventArgs e)
      {
  
          int numberOfBytesToRead = 125;
          if (buffer.Count &gt;= numberOfBytesToRead)
          {
              this.Invoke(new Action(() =&gt;
              listBox1.Items.Add(string.Format("SPO = {0}, PulseRate = {1}, Time = {2}",
                                                      buffer[43].ToString(),
                                                      buffer[103].ToString(),
                                                      DateTime.Now.ToString()
              ))));
              buffer.RemoveRange(0, numberOfBytesToRead);
  
          }
  </code></pre>
  
  <p>Thank all!</p>