通过C#的GSMCOMM库发送短信

jah*_* kk 1 c# gsm

我已经开发了使用C#的GSMCOMM库发送短信的ac#应用程序,但是我面临的问题是三天是当我尝试使用gsmcomm objects.send message methode发送消息时,有时会给出电话异常的信息。未连接,有时会给异常端口未打开。我在下面共享我的代码:用于将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;
    }
Run Code Online (Sandbox Code Playgroud)


以及发送短信的代码

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

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

 catch (Exception ex)
                {

                    throw ex;
                }
Run Code Online (Sandbox Code Playgroud)

vah*_*rat 5

当您首先使用gsmcomm ..时,请在vb.net的comboBox i expert中列出您的comPort。您可以阅读此代码并将其转换为C#1)在您的表单和form_load中创建一个combobox,编写此代码

 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
Run Code Online (Sandbox Code Playgroud)

在您的全球范围内,编写此代码

            Public Property mymodem As GsmCommMain
Run Code Online (Sandbox Code Playgroud)

将子项目添加到您的项目中,如下所示

   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
Run Code Online (Sandbox Code Playgroud)

之后,放置一个用于手机号码的文本框..命名为txttel,还放置一个文本框用于textMessage ..命名为txtMSG放置一个按钮以向您发送消息。

  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
Run Code Online (Sandbox Code Playgroud)

最后确保关闭端口..像这样

  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
Run Code Online (Sandbox Code Playgroud)