Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a modem and its working fine. I am recieving data comming from the Serial Port say COM4.

my DataReceived event is giving me the data.
say following is the event which shows the data in controls.

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!comport.IsOpen) return;

comport.WriteLine("AT+VCID=1\n");
string data = comport.ReadLine();
if (data.Contains("NMBR")) txtCID.Invoke(new EventHandler(delegate { txtCID.Text = data; }));
if (data.Contains("TIME")) txtdate.Invoke(new EventHandler(delegate { txtdate.Text = data; }));//here plz don't consider the proper formate for the time.i am using here just the idea
if (data.Contains("RING")) txtCID.Invoke(new EventHandler(delegate { txtCID.ForeColor = (txtCID.ForeColor ==Color.Yellow?Color.Red: Color.Yellow); }));

}

I now want to be notified when the incomming call is disconnected. on that i would like to clear the controls or something else.
Please guide me about it.
Thank you in Advance.
Posted

1 solution

For asynchronous data I generally create a timer to test if there is still data comiing in.

The method below uses two counters, when one counter stops incrementing form comm events and the timer is called, a second coounter starts to increment. When the second counter hits 10 or 1 second for the example below I set a boolean to let the application know there is no data coming in.

C#
//Initialize Com Timer
private void startComTimer()
{
  tmrCOM.Interval = 100;
  tmrCOM.Enabled = true;
}


    //Port open section of code
    ...
    try
   {
      serialPort.Open();
      PortOpen = true;
      comError = false;
    }
    catch (IOException)
    {
      //Indicate that port is NOT open
      comError = true;
      PortOpen = false;
    }
    ...

void _serialComm_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    ...
    COMcnt_1++;                 //For Data Good Indicator to let
    COMcnt_2 = 0;               //user know data is coming in
    ...    
    //When the data is verfied set datGood
    datGood = true;
    ...
}

private void tmrCOM_Tick(object sender, EventArgs e)
{
   //Alert the user if there is no new data.
   //Indicates that there may be issues with
   //data format or data stream has been lost.
   if (PortOpen)
   {
     if (COMcnt_1 == lastCOMcnt)
   {
     COMcnt_2++;
   }

    lastCOMcnt = COMcnt_1;

    if (COMcnt_2 >= 10)
    {
       datGood = false;
    }
  }
  else
  {
    datGood = false;
  }
}


Good Luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900