【问题标题】:Sending SMS Through GSMCOMM library of C#通过 C# 的 GSMCOMM 库发送短信
【发布时间】:2013-05-13 12:47:00
【问题描述】:

我已经开发了 ac# 应用程序,用于使用 C# 的 GSMCOMM 库发送短信。但是我面临三天的问题是,当我尝试使用 gsmcomm objects.send message methode 发送消息时。有时它会给出异常该电话未连接,有时它会给出未打开的异常端口。 我在下面分享我的代码: 用于将 pc 连接到手机 gsm 调制解调器的代码。有时它会毫无例外地发送消息。
手机连接电脑代码。

private bool ConnectPhone() 
    {
        string conectionStr = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
        clsFileLogger.VerifyLogFileDirectory();
        clsFileLogger.WriteToLog("DB Connection: " + conectionStr);
        conn = new SqlConnection(@conectionStr);
        int port = Convert.ToInt32(ConfigurationSettings.AppSettings["port"]);
        int baudRate = Convert.ToInt32(ConfigurationSettings.AppSettings["baudRate"]);
        int timeout = Convert.ToInt32(ConfigurationSettings.AppSettings["timeout"]);
        gsmComm = new GsmCommMain(port, baudRate, timeout);
        try
        {
            Isconnected = false;
            if (gsmComm.IsConnected() == false)
            {
                gsmComm.Open();
            }

            Isconnected = gsmComm.IsConnected();

            clsFileLogger.WriteToLog("\nConnected with GSM Modam");
        }
        catch (Exception)
        {
            clsFileLogger.WriteToLog("\nUnable to open the port.");
        }
        return Isconnected;
    }


以及发送短信的代码

  if (gsmComm.IsConnected() == false)
                    {
                        this.ConnectPhone();
                    }

                    pdu = new SmsSubmitPdu(strSMS, cellNO, "");
                    gsmComm.SendMessage(pdu);

 catch (Exception ex)
                {

                    throw ex;
                }

【问题讨论】:

    标签: c# gsm


    【解决方案1】:

    当您使用 gsmcomm .. 首先,在组合框中列出您的 comPorts 我是 vb.net 的专家 .. 你可以阅读这段代码并将其翻译成 C# 1)在您的表单和 form_load 中创建一个组合框,编写此代码

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each prt In My.Computer.Ports.SerialPortNames
                comboBox1.Items.Add(prt)  
            Next
     End Sub
    

    在 from 的全局范围内,编写此代码

                Public Property mymodem As GsmCommMain
    

    为您的项目添加一个子项目,如下所示

       Private Sub connect()
        Try
            Cursor.Current = Cursors.WaitCursor
            If comboBox1.Text = "" Then Return
            If IsNothing(mymodem) Then mymodem = New GsmCommMain(comboBox1.Text)
            If Not mymodem.IsOpen Then mymodem.Open()
            Cursor.Current = Cursors.Default
        Catch ex As Exception
            richTextBox1.AppendText(ex.Message & vbCrLf) 'i add a richtextbox to my form for show exceptions and my produced declaration
        End Try
    End Sub
    

    然后为手机号码添加一个文本框.. 将其命名为 txttel 还为 textMessage 放置一个文本框 .. 将其命名为 txtMSG 放一个按钮向您发送消息..将其命名为 btnsend 剩下的代码会是这样..

      Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
                If String.IsNullOrEmpty(txtMSG.Text.Trim) Then Return
                   SendSMS()
    
       End Sub
    
    
      Private Sub SendSMS()
         Try
              If Not mymodem.IsOpen Then connect()
              Dim pdu As New SmsSubmitPdu(txtMSG.Text.Trim & vbCr, txtTel.Text)
              mymodem.SendMessage(pdu)
              richTextBox1.AppendText("your message sent successfully")
          Catch ex As Exception
              richTextBox1.AppendText(ex.Message)
         End Try
      End Sub
    

    最后一定要关闭你的端口..像这样

      Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If Not IsNothing(mymodem) AndAlso mymodem.IsOpen Then
            mymodem.Close()
        End If
      End Sub
    

    【讨论】:

      【解决方案2】:

      试试这些指南(对我很有帮助): http://www.codeproject.com/Articles/325731/Bulk-SMS-Sender http://www.codeproject.com/Articles/20420/How-To-Send-and-Receive-SMS-using-GSM-Modem

      但您的 com 端口打开问题似乎不在您的代码中。尝试使用诸如 Teraterm 应用程序之类的东西来测试您的端口。并确保在您开始运行应用程序时端口未打开(在上次启动后它可能仍处于打开状态)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多