jahanzaib  kk
jahanzaib kk

Reputation: 688

Sending SMS Through GSMCOMM library of C#

I have develpoed a c# application for sending sms using GSMCOMM library of C# .But the problem which i am facing for three days is that when i tried to send the message using the gsmcomm objects.send message methode .Sometimes it gives exception that phone is not connected and sometimes it gives exception port not open. I am sharing my code below: Code for connecting pc to phone gsm modem.And sometime it send the message without giving any exception.
Code for connecting phone to 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;
    }


And code for sending SMS

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

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

 catch (Exception ex)
                {

                    throw ex;
                }

Upvotes: 1

Views: 17828

Answers (2)

vahid basirat
vahid basirat

Reputation: 455

when you using gsmcomm .. the first of all , list your comPorts in a comboBox i expert in vb.net .. you can read this code and translate it to C# 1) create a combobox in your form and in form_load , write this code

 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

at the Global scope of your from , write this code

            Public Property mymodem As GsmCommMain

add a sub to your project like below

   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

after that put a textbox for mobile number .. name it txttel also put a textbox for textMessage .. name it txtMSG put a button to send you message .. name it btnsend the remained code will be like this ..

  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

at the end be sure to close your port .. like this

  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

Upvotes: 5

soalflow
soalflow

Reputation: 266

Try these guides (it was helpful for me): http://www.codeproject.com/Articles/325731/Bulk-SMS-Sender http://www.codeproject.com/Articles/20420/How-To-Send-and-Receive-SMS-using-GSM-Modem

But it seems that your problem with com-port opening isn't in your code. Try to test your port using something like Teraterm app. And be sure the port isn't open when you start to run your app (it may be still open after previous launching).

Upvotes: 1

Related Questions