0

I have created winform application to send SMS using USB modem, it working properly but I want to get the delivery message and confirm the message has been sent correctly.

Here is my program

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        SerialPort sp = new SerialPort();
        sp.PortName = textBox1.Text;
        sp.Open();
        sp.WriteLine("AT" + Environment.NewLine);
        Thread.Sleep(100);
        sp.WriteLine("AT+CMGF=1" + Environment.NewLine);
        Thread.Sleep(100);
        sp.WriteLine("AT+CSCS=\"GSM\"" + Environment.NewLine);
        Thread.Sleep(100);
        sp.WriteLine("AT+CMGS=\"" + mobile + "\"" + Environment.NewLine);
        Thread.Sleep(100);
        sp.Write(message);
        Thread.Sleep(100);
        sp.Write(new byte[] { 26 }, 0, 1);
        Thread.Sleep(100);

        var response = sp.ReadExisting();
        if (response.Contains("ERROR: 500"))
        {
            MessageBox.Show("Please check credits");
        }
        sp.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

Please help me how to read the delivery status with above code

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
Malshan
  • 15
  • 7

2 Answers2

1

This question is not C# specific; rather it's an AT commands question.

After sending a SMS, you'll get a response from modem, something likee this:

+CMGS: {sms id, 0 to 255}
OK

In this case, if Service Center has delivered the SMS successfully, modem will return this response:

+cds: {some id which does not matter} {PDU status report}

You need just decode this PDU to obtain status report, ID of original SMS and other useful data. If the ID of the sent SMS and the one from status report are equal, you have status report exactly for your message.

Note: if you remove message from modem's storage before receiving of delivery report, you'll get report which will be containing all usual information, but status of delivery will be most likely 71 instead of 0.

I used this approach myself, based on this answer, and it works.

Edit 1 : you are handling the RS232 reading synchronously, which i dont really recomand,the reading function should be fired automatically when the data are available in the port,something like that :

private string SerialDataReceived = string.Empty
private void button1_Click(object sender, EventArgs e)
{
// new instance of the COM port
port = new SerialPort(ComPort, 115200, Parity.None, 8, StopBits.One);
// start port lessener
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
// Begin communications and wait for nad to reboot completly
port.Open();

//send your AT Commands
 port.Write("ATE0\r\n");
// check the response 'if Command is successfull it reply with something +Ok'
if(SerialDataReceived.ToLower().Contains("ok"))
}

//event will be fired each time a new data are available in the port
     private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
            // Show all the incoming data in the port's buffer
            SerialDataReceived += port.ReadExisting();
        }

now in your send function you should check if you have in the end a response containing +CMGS:,

Erwin Draconis
  • 764
  • 8
  • 20
0
var response = sp.ReadExisting();

while (!response.Contains("OK") && !response.Contains("ERROR"))          

{
 
 // check if message has been sent before exiting      
 response = sp.ReadExisting();  

}
David Buck
  • 3,752
  • 35
  • 31
  • 35