日期:2014-05-16  浏览次数:20943 次

C# 打开多个 SerialPort[ ] 的问题
 C# 打开 SerialPort[ ] 数组的问题
我想连续打开多个SerialPort  count =9       
 for (int l = 0; l < count; l++)
            {
                
                comPort[l] = new SerialPort();
                comPort[l].PortName = dt.Rows[l]["Com"].ToString();
                comPort[l].BaudRate = Convert.ToInt32(dt.Rows[l]["BaudRate"]);
                comPort[l].DataBits =Convert.ToInt32( dt.Rows[l]["DataBit"]);
                comPort[l].Parity = Parity.None;//(dt.Rows[l]["Checksum"].ToString());
                comPort[l].StopBits = StopBits.One;// Convert.ToInt32((dt.Rows[l]["StopBit"]));
                comPort[l].ReadTimeout = 100;// (int)dt.Rows[l]["Delay"];
                try
                {
                  
                    if (l > 0)
                    {
                       int  l1 = l - 1;
                        if (comPort[l1].IsOpen)
                        {
                            comPort[l1].Close();
                          
                        }
                    }
                    Thread.Sleep(2000);//延迟2000ms(这里延迟不加就无法连续打开串口
                    richTextBox1.Invoke
          (
              new MethodInvoker