通过C#的GSMCOMM库发送SMS#

问题描述:

我已经开发了使用C#的GSMCOMM库发送短信的ac#应用程序。但是我面对三天的问题是,当我试图使用gsmcomm objects.send发送消息时消息方法。有时它会给出例外情况,即电话未连接,有时会导致异常端口未打开。 我在下面分享我的代码: 连接电脑到电话GSM调制解调器的代码。有时它发送消息,没有任何例外。
将手机连接到PC的代码。通过C#的GSMCOMM库发送SMS#

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

尝试这些指南(这是有帮助对我来说): 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应用程序等测试您的端口。并且确保在开始运行应用程序时该端口未打开(它可能在上次启动后仍处于打开状态)。

当你使用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 

,写这样的代码

  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 也把一个文本框的文字信息..它命名为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