日期:2014-05-17  浏览次数:20889 次

C# SetialPort类读取串口数据时使用委托出错。
public void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string strTemp = "";
            double iSecond = 0.5;

            DateTime dtOld = System.DateTime.Now;
            DateTime dtNow = System.DateTime.Now;
            TimeSpan dtInter;
            dtInter = dtNow - dtOld;

            int i = Sp.BytesToRead;
            if (i > 0)
            {
                try
                {
                    strTemp = Sp.ReadExisting();
                }
                catch
                { }
                if (strTemp.ToLower().IndexOf("\r") < 0)
                {
                    i = 0;
                }
                else
                {
                    this.Invoke(interfaceUpdataHandle, strTemp);
                }
            }
            while (dtInter.TotalSeconds < iSecond && i <= 0)
            {
                dtNow = System.DateTime.Now;
                dtInter = dtNow - dtOld;
                i = Sp.BytesToRead;
                if (i > 0)
                {
      &n