1

尝试使用 GsmComm 库开发小型应用程序。目前,在检测电话是否已连接时存在一些问题。

它检测到手机何时断开连接,但不想在再次连接时检测到手机......

知道为什么吗?

我的代码:

        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 });
    }
4

1 回答 1

0

对不起,迟到的答案。刚刚注意到你的问题。无需使用 EventHandler 进行连接。如果您想在电话/gsm 调制解调器连接后调用某些函数,您应该在打开端口并(!)检查是否使用GsmCommMain类中的IsConnected()成员函数建立连接后调用它们。

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 });
}
于 2014-12-09T07:53:35.710 回答