通过c#的GSMCOMM库发送短信

本文关键字:GSMCOMM 通过 | 更新日期: 2023-09-27 18:13:13

我使用c#的GSMCOMM库开发了一个发送短信的c#应用程序,但是我面临的问题是,当我试图使用GSMCOMM对象发送消息时,我面临了三天的问题。发送消息方法。有时它给出异常,电话没有连接,有时它给出异常端口未打开。我在下面分享我的代码:代码连接pc到手机gsm调制解调器。有时它发送消息而不给出任何异常。
连接电话到pc的代码。

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#的GSMCOMM库发送短信

当你使用gsmcomm ..首先,在组合框中列出您的端口我精通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

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

但是似乎你的comport打开问题不在你的代码中。试着使用terterm app之类的东西来测试你的端口。并确保端口在你开始运行应用时没有打开(它可能在之前启动后仍然打开)。