Deleting SMS Messages from Message Storage (AT+CMGD) (Tutorial 24)

The AT command +CMGD (command name in text: Delete Message) is used to delete SMS message(s) from message storage. The message storage area from which SMS messages are deleted is specified by the +CPMS AT command (command name in text: Preferred Message Storage). Details about the +CPMS AT command can be found in the earlier section "Preferred Message Storage (AT+CPMS)" of this SMS tutorial.


1. Syntax of the +CMGD AT Command

The syntax of the +CMGD AT command is: (Optional parameters are enclosed in square brackets.)


+CMGD=index[,flag]


In the above line, index is an integer specifying the location of the SMS message to be deleted from the message storage area by the +CMGD AT command, and flag is an integer specifying whether to delete SMS messages according to their message status. The SMS specification has defined these flag values: 0, 1, 2, 3 and 4.

  • 0. Meaning: Delete only the SMS message stored at the location index from the message storage area. This is the default value.

  • 1. Meaning: Ignore the value of index and delete all SMS messages whose status is "received read" from the message storage area.

  • 2. Meaning: Ignore the value of index and delete all SMS messages whose status is "received read" or "stored sent" from the message storage area.

  • 3. Meaning: Ignore the value of index and delete all SMS messages whose status is "received read", "stored unsent" or "stored sent" from the message storage area.

  • 4. Meaning: Ignore the value of index and delete all SMS messages from the message storage area.

To find the flag values supported by your GSM/GPRS modem or mobile phone, you can perform a test operation using the +CMGD AT command. See the next section for the details.


2. Finding the Supported Indexes and Flag Values

Performing a test operation with the +CMGD AT command allows you to find all indexes that are supported by the index parameter. The test operation may also return all supported values of the flag parameter. Here is the format of the information response returned after the execution of the test command "+CMGD=?": (Optional fields are enclosed in square brackets.)


+CMGD: (list_of_indexes)[,(list_of_flag_values)]


  • The values inside the first parentheses are the values that are supported by the index parameter of the +CMGD AT command.

  • The values inside the second parentheses are the values that are supported by the flag parameter of the +CMGD AT command. Note: A GSM/GPRS modem or mobile phone might not provide these values since the SMS specification defines them as optional.

As an example, here is the response returned from my Nokia 6021 mobile phone to Windows' HyperTerminal program after the execution of the test command "+CMGD=?":


AT+CMGD=?
+CMGD: (1-10),(0-4)

OK


From above, we know that the Nokia 6021 mobile phone allows any integer between 1 and 10 to be assigned to the index parameter of the +CMGD AT command. Also, the Nokia 6021 mobile phone supports all of the five flag values defined in the SMS specification.

Do you remember that we described the format of values inside parentheses in the section "Test Command -- Checks Whether a Certain AT Command is Supported"? If not, you may want to go back and have a look. Here are a few examples showing some of the possibilities: "(0-4)", "(0,1,2,3,4)", "(0,2-4)".


3. Example Demonstrating How to Use the +CMGD AT Command to Delete SMS Text Messages

Now let's see an example that demonstrates how to use the +CMGD AT command to delete an SMS text message from the message storage area. Suppose you have written an SMS text message to the message storage area by the +CMGW AT command (details about the +CMGW AT command can be found in the earlier section "Writing SMS Messages to Memory / Message Storage (AT+CMGW)" of this SMS tutorial), like this:


AT+CMGW="+85291234567"
> A simple demo of SMS text messaging.
+CMGW: 1

OK


The information response of the +CMGW AT command tells us that the SMS text message is stored in the memory location at index 1. To delete the SMS text message, specify 1 to the index parameter of the +CMGD AT command, like this:

(Important note: Make sure you are writing text messages to and deleting text messages from the same message storage area. If not, you may delete the wrong text message. Details about message storage areas can be found in the earlier section "Preferred Message Storage (AT+CPMS)" of this SMS tutorial.)


AT+CMGD=1
OK


The final result code OK indicates the SMS text message "A simple demo of SMS text messaging." was deleted successfully. If the operation fails, the final result code returned will either be ERROR or +CMS ERROR.

Here is another example. Suppose you want to delete all SMS messages stored in the message storage area. To do this, assign 4 to the flags parameter of the +CMGD AT command. The value assigned to the index parameter does not matter since it will be ignored by the GSM/GPRS modem or mobile phone.


AT+CMGD=1,4
OK


from developershome .com

a Snowman, fall in love, beautiful rose - Romantic SMS


If I were to describe true luv then I would describe it as what a snowman did to a snowwoman:
He gave her warm hug n they both melted in each other’s arms.
(Characters: 155)


Never waste an opportunity 2 say 'I love U' to someone u really like,
coz it is not everyday u'll meet the person
who has the magic to let u fall in love.
(Characters: 154)




I looked at a sweet, beautiful rose,
and then I looked at you,
and I kept looking at you,
for the rose isn't as beautiful as you. 2
(Characters: 131)

Writing SMS Messages to Memory / Message Storage (AT+CMGW) (Tutorial 23)

The AT command +CMGW (command name in text: Write Message to Memory) is used to write an SMS message to memory (i.e. message storage). The memory/message storage area to which SMS messages are written is specified by the +CPMS AT command (command line in text: Preferred Message Storage). Details about the +CPMS AT command can be found in the earlier section "Preferred Message Storage (AT+CPMS)" of this SMS tutorial.


Syntax of the +CMGW AT Command in SMS Text Mode

In SMS text mode, the syntax of the +CMGW AT command is: (Optional parameters are enclosed in square brackets.)


+CMGW[=address[,address_type[,message_status]]]sms_message_body


Before we discuss each of the parameters, let's see an example that gives you some idea of how an actual command line should look like:


AT+CMGW="+85291234567",145,"STO UNSENT"This is an example for illustrating the syntax of the +CMGW AT command in SMS text mode.


The address Parameter

The first parameter of the +CMGW AT command, address, specifies the destination address to send the SMS message to. Usually it is a mobile number formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163). For example, "+85291234567" and "91234567". Note that the value passed to the address parameter should be a string, i.e. it should be enclosed in double quotes.

The address parameter is optional and so it can be omitted. Later when you want to send the SMS message out, you can specify the destination address by the +CMSS AT command (command name in text: Send Message from Storage).


The address_type Parameter

The second parameter of the +CMGW AT command, address_type, specifies the type of the address assigned to the address parameter. Two values are commonly used. They are 129 and 145:

  • 129. Meaning: The value of address is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) but it is not sure whether the value of address is an international number, a national number or a number of other types. Example addresses: "85291234567", "91234567".

  • 145. Meaning: The value of address is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) and it is an international number. Example address: "+85291234567".

As address_type is an optional parameter, it can be omitted. If you do so, the GSM/GPRS modem or mobile phone will use the default value of the address_type parameter, which is:

  • 129 if the value of address does not start with a "+" character. For example, "85291234567".

  • 145 if the value of address starts with a "+" character. For example, "+85291234567".


The message_status Parameter

The third parameter of the +CMGW AT command, message_status, specifies the status of the SMS message to be written. The SMS specification has defined four status values:

  • REC UNREAD. It refers to the message status "received unread".

  • REC READ. It refers to the message status "received read".

  • STO UNSENT. It refers to the message status "stored unsent". This is the default value.

  • STO SENT. It refers to the message status "stored sent".

Note that the value assigned to the message_status parameter should be a string. Thus, it should be enclosed in double quotes.

As message_status is an optional parameter, it can be omitted. If you do so, the GSM/GPRS modem or mobile phone will use the default value of the message_status parameter, which is "STO UNSENT".


The Character

, which represents the carriage return character, follows the message_status parameter. When the GSM/GPRS modem or mobile phone receives the carriage return character, it will send back a prompt formed by these four characters: the carriage return character, the linefeed character, the ">" character and the space character. If you don't understand what this means, don't worry. This should be clear to you when you see the example in the section "Example Demonstrating How to Use the +CMGW AT Command to Write SMS Text Messages to Message Storage in SMS Text Mode".


The sms_message_body Parameter

The fourth parameter of the +CMGW AT command, sms_message_body, specifies the SMS message body to be written to the memory/message storage area. Entering the character will cancel the +CMGW AT command. If you don't understand what this means, see the example in the section "Example Demonstrating How to Use the +CMGW AT Command to Write SMS Text Messages to Message Storage in SMS Text Mode".


The Character

When you finish entering the SMS message body, you have to enter the character to mark the end of the SMS message body. The GSM/GPRS modem or mobile phone will then attempt to write the SMS message to the memory/message storage area.


SMSC Number Stored with the SMS Message (SMS Text Mode)


In SMS text mode, the +CMGW AT command does not have a parameter that allows you to specify an SMSC number. (Note that the +CMGW AT command does have such parameter when the GSM/GPRS modem or mobile phone is operating in SMS PDU mode.) However, when an SMS message is written to message storage, an SMSC number is actually stored with it. The SMSC number stored is the one specified by the +CSCA AT command (command name in text: Service Centre Address). Later if you send the SMS message, it will be transmitted through this SMSC. Once an SMS message has been written to message storage, it is not possible to change the SMSC number stored with the SMS message by using AT commands.

Note that it is possible that the command behavior on your mobile device is slightly different from what was described above.

More information about this issue is available in the "SMSC Number to be Used by the +CMSS AT Command to Send SMS Messages" section of this SMS tutorial.


Format of the Information Response of the +CMGW AT Command in SMS Text Mode

If the GSM/GPRS modem or mobile phone writes the SMS message to the message storage area successfully, it will return an information response to the computer / PC. In SMS text mode, the information response of the +CMGW AT command has the following format:


+CMGW: index


index is an integer that tells us the memory location to which the SMS message was written in the message storage area.

from developershome .com