CellularRadio module - delete all SMS

Hi guys,
CellularRadio sometimes stops to work and I found that the reason is due to too many SMS stored in the SIM card (if I insert the sim card in a smartphone gives me the error: “sim card is full. Delete some sms.”)

Directly inside the SmsReceived event handler function I wrote this code:


Thread.Sleep(1500);
_Cell.DeleteAllSms();
Thread.Sleep(1000);

I was convinced that DeleteAllSms() method would erase all the sms inside the SIM but, obviously, it doesn’t…

The code inside that method (reading from gadgeteer 4.3 source) is the following:

public void DeleteAllSms()
{
this.SendATCommand("AT+CMGD=0,4");
}

Simple and clear!
Referring to the AT commands listed here 系统发生错误 at page 99 I verified that the AT+CMGD=0,4 command is correct.

In your opinion what is going wrong?

Maybe I need to call DeleteAllSms method outside of the SmsReceived event handler function?

I’ve not tried the DeleteAll method before. I tend to process the received SMS and then delete.

You suggest something like this?

A snippet from my old project…


        void cellularRadio_SmsReceived(CellularRadio sender, CellularRadio.Sms message)
        {
            usbSerial.Port.WriteLine("NEW SMS");
            usbSerial.Port.WriteLine("FROM: " + message.PhoneNumber);
            usbSerial.Port.WriteLine("MSG: " + message.Message);
            usbSerial.Port.WriteLine("WHEN: " + message.Timestamp.ToString());
            usbSerial.Port.WriteLine("STATUS: " + message.Status.ToString());
            usbSerial.Port.WriteLine("POS:" + message.Index.ToString());
            xbeeLED.BlinkRepeatedly(GT.Color.Blue);

            database.Open();
            nodeDatabase.UserLevel user = database.LogSMS(message.Timestamp, message.PhoneNumber, message.Message, nodeDatabase.SMSDirection.Recieved);
            switch (user)
            {
                case nodeDatabase.UserLevel.Unknown:
                    Debug.Print("Unknown user so probably SPAM");
                    break;
                case nodeDatabase.UserLevel.User:
                    Debug.Print("SMS From a User");
                    break;
                case nodeDatabase.UserLevel.Admin:
                    Debug.Print("SMS From an Admin");
                    break;
                case nodeDatabase.UserLevel.Creator:
                    Debug.Print("SMS From The Creator Himself");
                    break;
            }
            cellularRadio.DeleteSms(message.Index);

            database.Close();
        }


You can ignore the database calls etc.

Actually, looking at this project, I see that I was calling the DeleteAll method on startup where I would obtain a list of SMS, process each and then DeleteAll.

Unfortunately also deleting one SMS at a time doesn’t work.
If I insert the sim card in a smartphone I still see the received sms…

This is a snippet of my code:

Private Sub _Cell_SmsReceived(sender As CellularRadio, message As CellularRadio.Sms) Handles _Cell.SmsReceived
        Try
            ManageSmsReceived(message)
            _Cell.DeleteSms(message.Index)
        Catch ex As Exception
            WriteFileError("_Cell_SmsReceived", ex.Message & " (" & ex.GetType.ToString & ")")
        End Try
    End Sub

I’m not doing strange stuff

Rather than using the smartphone what do you get is you request the SMS list before and after you have deleted the messages?

                    cellularRadio.RequestSmsList(CellularRadio.SmsState.All);

I noticed that if I request the SMS list when there aren’t SMS the event SmsListReceived is not raised… (to me it would be more correct raise the event with a zero-length SMS’s array)

By requesting the SMS list I always get the list with all SMSs I sent (no matter if I request the list before or after I called the Delete method).

There is an AT command that tells the device where to store the messages and you can set this so that they don’t get store on the SIM.

The AT+CMGD command should delete them but I have seen the same issue with a SIM900 where it did not if on the SIM.

I am away from my main PC so I can’t get access to the Android code that does this.

1 Like

AT+CPMS=[,[,<mem3]]

I read at the documentation but the only one possible value for SIM900 is “SM” (so the SIM memory).
Searching in the gadgeteer source code there’s already this:

// Set the sms to be stored in the SIM card
this.SendATCommand("AT+CPMS=\"SM\"");

I think I haven’t understood your suggestion. What can I do? :slight_smile:

I’m at a loss as to why it doesn’t work for you. :think:

I made some “experiment”…

I checked the supported message storage areas and this is the module response:

I tried to delete messages using all these ways, always without success:

cellularRadio.DeleteAllSms();
cellularRadio.SendATCommand("AT+CMGDA=\"DEL ALL\"");
cellularRadio.SendATCommand("AT+CMGD=1,4");

I edited the standard gadgeteer driver to show each response received by cellular radio module… this allowed me to check many things and verify the response received for each command I sent.
Example:

[quote]CellularRadio : +CREG: 2
CellularRadio : +CSQ: 18,0
CellularRadio : +CPAS: 0
[/quote]

Each command gets always a response… AT+CMGD=0,4 respond “OK” so the command is being interpreted but, I don’t know why, not correctly executed even if it doesn’t returns ERROR.

Between all my attempts, once AT+CMGD=0,4 has worked properly and the 6 SMS stored were deleted!
Seems to be a race condition, because I found a condition that delete all sms correctly:

  • I added a breakpoint between commands “SendSms” and “DeleteAllSms”
  • I waited until the SMS arrived on my smarphone before executing “DeleteAllSms” (thanks to breakpoing)
  • I executed the “DeleteAllSms” and it worked

This is a snippet of my code:

Try
     _Cell.SendSms(strNumber, strText)
     Thread.Sleep(5000)
     _Cell.DeleteAllSms()
     Thread.Sleep(1000)
Catch ex As Exception
     Debug.Print(ex.ToString)
     WriteFileError("SendSmsMessage", ex.Message)
End Try

Before there was a Sleep of 1 second, now I set the Sleep at 5 seconds.
If I give enough time to send the SMS, the cellular radio module removes all SMS correctly.

But if the module requires more than 5 seconds to send the SMS?
What do you suggest to do to check when the module has completed to send?

Reading the documentation I found that after each SMS sent the module responds with “+CMGS: ” where is the message reference (index). When I receive this string I can raise an event “SmsSent”.
To you could be a good way?

For some reason I missed the fact that you were talking about Sending SMS from the module .

It would therefore make perfect sense to wait until the module responds with a “SMS Sent” event before trying to delete it.

Sent SMS should not be stored in the SIM. There is a different AT command for sending via the SIM. The normal AT+CMGS command should only send from the MT, not the SM module.