how to check succeeded and failed sending a message in gsmcomm library

1.2k views Asked by At

i build a simple application for sending message using gsmcomm library and C#. when i sent message through a modem i store it to oracle database SENTMESSAGE table. I provide a table when sending message failed namely FAILEDMESSAGE table (ID, DATE, TIME, PHONENUMBER, MESSAGE), but i still didn't understand how implemented it.

Anyone can suggest me how to differentiate between sending a message succeeded and failed in gsmcomm library?

this is my code for sending a message :

private void btnSentSMS_Click(object sender, EventArgs e)
{
    var msg = txtMessage.Text;
    var phoneNumber = txtNumber.Text;
    var pdu = new SmsSubmitPdu(msg, phoneNumber, string.Empty);
    comm.SendMessage(pdu);
    MessageBox.Show("sms sent");

    //STORE SEND SMS TO DATABASE
    OracleCommand cmd = new OracleCommand();
    cmd.CommandText = @"INSERT INTO SENTMESSAGE (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES 
                       (SQ_SENTMESSAGE.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
                        + phoneNumber + "', '" + msg + "')";
    cmd.Connection = koneksi_manual.con;
    koneksi_manual.con.Open();// <= Open connection before executing the command.
    cmd.ExecuteNonQuery();
    koneksi_manual.con.Close(); //closing connection
}
2

There are 2 answers

1
Aks On BEST ANSWER

SemdMessage Method like:

public bool SendMessage(SerialPort port, string phoneNo, string message)
    {
        bool isSend = false;
        try
        {                
            string recievedData = SendATCommand(port,"AT", 300, "No phone connected");
            string command = "AT+CMGF=1" + char.ConvertFromUtf32(13);
            recievedData = SendATCommand(port,command, 300, "Failed to set message format.");

            // AT Command Syntax - http://www.smssolutions.net/tutorials/gsm/sendsmsat/
            command = "AT+CMGS=\"" + phoneNo + "\"" + char.ConvertFromUtf32(13);
            recievedData = SendATCommand(port, command, 300,
                "Failed to accept phoneNo");

            command = message + char.ConvertFromUtf32(26);
            recievedData = SendATCommand(port, command, 3000,
                "Failed to send message"); //3 seconds

            if (recievedData.EndsWith("\r\nOK\r\n"))
                isSend = true;
            else if (recievedData.Contains("ERROR"))
                isSend = false;

            return isSend;
        }
        catch (Exception ex)
        {
            throw ex; 
        }          
    }
1
Aks On

please try to this code :

private void btnSentSMS_Click(object sender, EventArgs e)
{
    var msg = txtMessage.Text;
    var phoneNumber = txtNumber.Text;
    var pdu = new SmsSubmitPdu(msg, phoneNumber, string.Empty);
    if(comm.SendMessage(pdu))
    {
        MessageBox.Show("sms sent");

        //STORE SEND SMS TO DATABASE
        OracleCommand cmd = new OracleCommand();
        cmd.CommandText = @"INSERT INTO SENTMESSAGE (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES 
                   (SQ_SENTMESSAGE.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
                    + phoneNumber + "', '" + msg + "')";
cmd.Connection = koneksi_manual.con;
koneksi_manual.con.Open();// <= Open connection before executing the command.
cmd.ExecuteNonQuery();
koneksi_manual.con.Close(); //closing connection
}
else
{
MessageBox.Show("sms not sent");
OracleCommand cmd = new OracleCommand();
cmd.CommandText = @"INSERT INTO FAILEDMESSAGE (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES 
                   (SQ_SENTMESSAGE.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
                    + phoneNumber + "', '" + msg + "')";
            cmd.Connection = koneksi_manual.con;
        koneksi_manual.con.Open();// <= Open connection before executing the command.
cmd.ExecuteNonQuery();
koneksi_manual.con.Close(); //closing connection
}
}