如何在Com Port中断开呼叫时获取通知 [英] How to get the notification when call is disconnected comming in the Com Port

查看:63
本文介绍了如何在Com Port中断开呼叫时获取通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调制解调器,它的工作正常。我收到来自串口的数据说COM4。



我的DataReceived事件正在给我数据。

说以下是事件它显示了控件中的数据。



private void port_DataReceived(object sender,SerialDataReceivedEventArgs e)

{

if(!comport.IsOpen)返回;



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;})); //这里plz不考虑time.i的正确格式我在这里使用的只是想法

if(data.Contains(RING))txtCID.Invoke(new EventHandler(delegate {txtCID.ForeColor =(txtCID.ForeColor == Color.Yellow?Color。红色:Color.Yellow);}) );



}



我现在希望在断开呼叫时收到通知。在那我想清除控件或其他东西。

请指导我。

先谢谢你。

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.

推荐答案

对于异步数据我通常会创建一个计时器来测试是否还有数据正在进入。



下面的方法使用两个计数器,当一个计数器停止递增形式通信事件并调用计时器,第二个计数器开始递增。当下面的例子中第二个计数器达到10或1秒时,我设置一个布尔值让应用程序知道没有数据进入。



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.

//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.


这篇关于如何在Com Port中断开呼叫时获取通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆