为什么serialPort_DataReceivedevent会提升两次?

时间:2014-01-29 11:36:43

标签: c#

我在串口使用此事件,但是当此事件从端口侦听数据时,它会再次出现,因此它会破坏数字。我是否需要在某个地方保存数据并防止它再次被解雇..因为响了很长时间,所以我应该如何阅读并停止前方听?

  private void serialPortNEW_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {

                SerialPort sp = (SerialPort)sender;
                string indata = sp.ReadExisting();
                string[] strFirstStep = Regex.Split(indata, "\r\nNMBR =");
                string[] strLastStep = Regex.Split(strFirstStep[1], "\r\n");
                string FinalValue = strLastStep[0];

                custNUmber = int.Parse(FinalValue);

                bw = new BackgroundWorker();
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                bw.RunWorkerAsync();
            }

0 个答案:

没有答案