NoviceToDotNet
NoviceToDotNet

Reputation: 10805

How to forcefully leave the serialPort_DataReceived event?

This is my program below which reads data from a port for caller id data. But i want once it gets the number it should stop listening the port and reading further data. and perform task written in exception. But even it doesn't work when i forcefully try to quit the event with exception thrown, and don't it leave when i set time out as well.. what should i do please let me know.

            bool _continue = true;
            string message = string.Empty;
            string number = string.Empty;
            private void serialPort_DataNewReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                try
                {
                    if (_continue)
                    {
                        SerialPort sp = (SerialPort)sender;
                        message = sp.ReadExisting();
                        if (message.Contains("MESG"))
                        {
                            string[] strFirstStep = Regex.Split(message, "\r\nNMBR =");
                            string[] strLastStep = Regex.Split(strFirstStep[1], "\r\n");
                            number = strLastStep[0];
                            _continue = false;

                        }
                    }
                    else throw new TimeoutException();
                }
                catch (TimeoutException t)
                {
                    bw = new BackgroundWorker();
                    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                    bw.RunWorkerAsync();
                }
            }

Upvotes: 0

Views: 759

Answers (2)

Sinatr
Sinatr

Reputation: 21979

Why do you have timeout exception in DataReceived? Perhaps you need something like:

bool _continue = true;
string message = string.Empty;
string number = string.Empty;

private void serialPort_DataNewReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    // stop listening (by ignoring call)
    if (!_continue)
        return;

    // proceed
    SerialPort sp = (SerialPort)sender;
    message = sp.ReadExisting();
    if (message.Contains("MESG"))
    {
        string[] strFirstStep = Regex.Split(message, "\r\nNMBR =");
        string[] strLastStep = Regex.Split(strFirstStep[1], "\r\n");
        number = strLastStep[0];
        _continue = false;

        // do a work if number is received
        bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        bw.RunWorkerAsync();
    }

To handle timeout (when you don't receive data for a while) you can create a timer, which will be reset as data are received. If timer is not reset in time, then its tick event will trigger, which means - timeout.

Upvotes: 1

zeshanbaig786
zeshanbaig786

Reputation: 157

you need to remove the event from serial port object

serialPort.DataReceived -= serialPort_DataNewReceived;

Upvotes: 1

Related Questions