title: Multi serialPort connection
body: <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.IO.Ports;
  using System.IO;
  using System.Threading;
  
  namespace wangxl{
  public partial class Smart_meter : Form
  {
      public int begin_year;
      public int begin_month;
      public int begin_day;
      public int finish_year;
      public int finish_month;
      public int finish_day;
      string from_bs_2;
      string from_EM;
      double[,] power = new double[4, 23];
      int chushihua = 0;
      int chuqi_changshu = 0;
      int n_1 = 0;
      int n_2 = 0;
      double[,] power_display_f = new double[4, 23];
      string path =   "C:\\Users\\Public\\data\\data_logging.txt";
      string path_m = "C:\\Users\\Public\\data\\energy_monitoring.txt";
      //  string path = "‪C:\\energy_moniotring\\data_logging.txt";   
  
      public Smart_meter()
      {
          InitializeComponent();
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
          String[] input = SerialPort.GetPortNames();
          comboBox1.Items.AddRange(input);
          comboBox16.Items.AddRange(input);
      }
  
  void button1(object sender, EventArgs e)
      {
          try
          {
              serialPort1.PortName = comboBox1.Text;
              serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
              serialPort1.DataBits = Convert.ToInt32(comboBox3.Text);
              serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits),     comboBox4.Text);
              serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox5.Text);
              serialPort1.Open();
              progressBar1.Value = 100;
          }
  
          catch (Exception err)
          {
              MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          if (serialPort1.IsOpen)
          {
              serialPort1.Close();
              progressBar1.Value = 0;
          }
      }
  
      private void button3_Click(object sender, EventArgs e)
      {
          if (serialPort1.IsOpen)
          {
              string output;
              output = textBox2.Text;
              serialPort1.Write(output);
              textBox2.Text = "";
              from_bs_2 = "";
              serialPort1.DiscardOutBuffer();
              //     serialPort1.DiscardOutBuffer();
          }
      }
  
      void button7_Click(object sender, EventArgs e)
      {
          try
          {  
              serialPort2.PortName = comboBox16.Text;
              serialPort2.BaudRate = Convert.ToInt32(comboBox15.Text);
              serialPort2.DataBits = Convert.ToInt32(comboBox13.Text);
              serialPort2.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox14.Text);
              serialPort2.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox12.Text);
              serialPort2.Open();
              progressBar2.Value = 100;
          }
          catch (Exception err)
          {
              MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
      }
      private void button8_Click(object sender, EventArgs e)
      {
          if (serialPort2.IsOpen)
          {
          serialPort2.Close();
          progressBar2.Value = 0;
          }
      }
  
      private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
      {
          Thread.Sleep(1000);  // important
          from_bs_2 += serialPort1.ReadExisting();
          Thread.Sleep(1000);
          this.Invoke(new EventHandler(showdata));
          Thread.Sleep(1000);
      }
  
      private void serialPort2_DataReceived(object sender, SerialDataReceivedEventArgs e)
      {
          Thread.Sleep(1000);  // important
          from_EM += serialPort2.ReadExisting();
          Thread.Sleep(1000);
          this.Invoke(new EventHandler(showdata_2));
          Thread.Sleep(1000);
          //  textBox1.Text += "laishujule";
      }
  </code></pre>
  
  <p>I want to design a program which can connect 2 or 3 Arduinos at same time. So, I created a serialPort 1, it works well, I can receive the data from serial 1, but when I use the same way to create serialPort 2, I find the serialPort doesn't work, it can be connected, but it can't get any data from Arduino. I found that the <code>serialPort2_DataReceived</code> function doesn't work, I had add the serialPort 2 into my program from tools. Can you help me?</p>