GSMComm phoneConnected / phoneDisconnected处理程序

时间:2013-07-16 10:48:50

标签: c# gsm

尝试使用GsmComm Library开发小型应用程序。 目前,检测手机是否连接有问题。

它会检测手机何时断开连接,但是当再次连接时不想检测手机......

知道为什么吗?

我的代码:

        GsmCommMain gsm = new GsmCommMain(4, 115200, 200);



private void Form1_Load(object sender, EventArgs e)
        {
            gsm.PhoneConnected += new EventHandler(gsmPhoneConnected);
                gsm.PhoneDisconnected += new EventHandler(gsmPhoneDisconnected);
                gsm.Open();
        }

private delegate void ConnctedHandler(bool connected);

    private void onPhoneConnectedChange(bool connected)
    {
        try
        {
            if (connected)
            {

                phoneStatus.Text = "OK";
            }
            else
            {
                phoneStatus.Text = "NG";
            }
        }
        catch (Exception exce)
        {
            logBox.Text += "\n\r" + exce.ToString();
        }
    }

    public void gsmPhoneConnected(object sender, EventArgs e)
    {
        this.Invoke(new ConnctedHandler(onPhoneConnectedChange), new object[] { true });
    }

    private void gsmPhoneDisconnected(object sender, EventArgs e)
    {
        this.Invoke(new ConnctedHandler(onPhoneConnectedChange), new object[] { false });
    }

1 个答案:

答案 0 :(得分:0)

对不起,迟到了。刚刚注意到你的问题。 无需使用EventHandler进行连接。如果要在连接手机/ gsm调制解调器后调用某些功能,则应在打开端口后调用它们,并且(!)检查是否使用 GsmCommMain <中的 IsConnected()成员函数建立连接/ em> class。

var gsm = new GsmCommMain(4, 115200, 200);

private void Form1_Load(object sender, EventArgs e)
{
    //gsm.PhoneConnected += new EventHandler(gsmPhoneConnected);   // not needed..
    gsm.PhoneDisconnected += new EventHandler(gsmPhoneDisconnected);
    gsm.Open();
    if(gsm.IsConnected()){
        this.onPhoneConnectedChange(true);
    }
}

private delegate void ConnctedHandler(bool connected);
private void onPhoneConnectedChange(bool connected)
{
    try
    {
        if (connected)
        {

            phoneStatus.Text = "OK";
        }
        else
        {
            phoneStatus.Text = "NG";
        }
    }
    catch (Exception exce)
    {
        logBox.Text += "\n\r" + exce.ToString();
    }
}

/*public void gsmPhoneConnected(object sender, EventArgs e)
{
    this.Invoke(new ConnctedHandler(onPhoneConnectedChange), new object[] { true });
}*/

private void gsmPhoneDisconnected(object sender, EventArgs e)
{
    this.Invoke(new ConnctedHandler(onPhoneConnectedChange), new object[] { false });
}