Welcome to Developer's Home Friday, April 18, 2014
Home - DevelopersHome.com

26.3.4. Example Demonstrating How to Use the +CMGL AT Command to List SMS Text Messages from a Message Storage Area (SMS Text Mode)


Preferences - Do not show ads

Now let's see a more detailed example that demonstrates how to use the +CMGL AT command to list SMS text messages from a message storage area when SMS text mode is used, and how the +CMGL AT command should be used together with other AT commands.


Instructing the GSM/GPRS Modem or Mobile Phone to Operate in SMS Text Mode

First, enter the command line "AT+CMGF=1" in a terminal program (for example, HyperTerminal in Microsoft Windows) to instruct the GSM/GPRS modem or mobile phone to operate in SMS text mode. This step is necessary because the default mode is SMS PDU mode. Below shows the response returned from Nokia 6021 to HyperTerminal after the execution of the command line "AT+CMGF=1":


AT+CMGF=1
OK


The final result code OK indicates the +CMGF AT command was executed successfully. If the final result code ERROR is returned, it is likely that the GSM/GPRS modem or mobile phone does not support SMS text mode. To check whether the GSM/GPRS modem or mobile phone supports SMS text mode, enter the command line "AT+CMGF=?" in the terminal program. Here is the response returned from Nokia 6021 to HyperTerminal:


AT+CMGF=?
+CMGF: (0,1)

OK


The values in the parentheses indicate the modes that the GSM/GPRS modem or mobile phone can operate in. The values 0 and 1 represent SMS PDU mode and text mode respectively. From the above response, we know that Nokia 6021 can operate in either SMS PDU mode or SMS text mode. See the section titled "Selecting the Operating Mode (AT+CMGF)" of this SMS tutorial if you want to learn further details about the +CMGF AT command.


Selecting the Message Storage Area to Read SMS Text Messages from

Second, use the AT command +CPMS (command name in text: Preferred Message Storage) to select the message storage area to read SMS text messages from. For example, to instruct the GSM/GPRS modem or mobile phone to use the message storage area in the SIM card for the AT command +CMGL, enter the command line 'AT+CPMS="SM"' in the terminal program. The result should be like this:


AT+CPMS="SM"
+CPMS: 2,10,2,10,5,160

OK


Detailed information about the +CPMS AT command is available in the "Preferred Message Storage (AT+CPMS)" section of this SMS tutorial.


Listing SMS Text Messages

Third, you can now use the +CMGL AT command to list SMS text messages from the message storage area. Let's say you want to retrieve all new SMS messages received. As the status of new inbound SMS messages is "received unread", you should specify "REC UNREAD" to the +CMGL AT command, as shown below:


AT+CMGL="REC UNREAD"


Since "REC UNREAD" is the default parameter value, you can simply omit it, like this:


AT+CMGL


Now press the Enter key on the keyboard. The GSM/GPRS modem or mobile phone will then execute the command line "AT+CMGL" and return all text messages that have the "REC UNREAD" status in the message storage area. In a terminal program such as HyperTerminal of Microsoft Windows, the result should look something like this:


AT+CMGL
+CMGL: 2,"REC UNREAD","+85291234567",,"07/02/18,00:05:10+32"
A simple demo of SMS text messaging.
+CMGL: 3,"REC UNREAD","+85291234567",,"07/02/18,00:07:22+32"
It is easy to list SMS text messages.

OK


From above, we know that the message storage area contains two new inbound SMS text messages. The first one is "A simple demo of SMS text messaging." and the second one is "It is easy to list SMS text messages.". Let's take the first SMS text message as an example to illustrate the meaning of different field values in the information response:

  • The value of the first field, 2, tells us the text message is stored in the memory location at index 2.

  • The value of the second field, "REC UNREAD", indicates the status of the text message is "received unread".

  • The value of the third field, "+85291234567", is the sender's phone number.

  • The value of the fourth field should be a string that contains the text associated to the sender's phone number "+85291234567" in the phonebook. In this example, this field is empty. One possible reason is that the GSM/GPRS modem or mobile phone (for example, Nokia 6021 and many other Nokia products) does not support this field.

  • The value of the fifth field, "07/02/18,00:05:10+32", tells us that the SMSC received the text message at 00:05:10 (GMT+8) on 18 Feb 2007. (Don't forget the unit of time zone is a quarter of an hour. Since 32 quarters of an hour = 8 hours, "+32" means GMT+8 hours.)

  • The value of the sixth field, "A simple demo of SMS text messaging.", is the body of the SMS text message.

The final result code OK indicates the SMS text messages were read from the message storage area successfully. If the execution of the +CMGL AT command fails, the GSM/GPRS modem or mobile phone will return either the final result code ERROR or +CMS ERROR.

Now if the command line "AT+CMGL" is executed again, no text message will be listed. Here shows the response returned from Philips 598:


AT+CMGL
OK


The response returned from Nokia 6021 is slightly different, as shown below. There seems to be a minor bug in Nokia's implementation. Two extra characters <CR><LF> are outputted and this does not follow the general syntax of extended AT commands.


AT+CMGL

OK


No SMS text message is listed because the status of the SMS text messages "A simple demo of SMS text messaging." and "It is easy to list SMS text messages." has been changed from "received unread" to "received read" after being read by the +CMGL AT command. To see these two SMS text messages again, specify either "REC READ" or "ALL" to the +CMGL AT command, like this:


AT+CMGL="ALL"
+CMGL: 1,"STO SENT","+85291234567",,
Hello, welcome to our SMS tutorial.
+CMGL: 2,"REC READ","+85291234567",,"07/02/18,00:05:10+32"
A simple demo of SMS text messaging.
+CMGL: 3,"REC READ","+85291234567",,"07/02/18,00:07:22+32"
It is easy to list SMS text messages.

OK


As mentioned in the earlier section "Format of the Information Response of the +CMGL AT Command in SMS Text Mode", two optional fields address_type and sms_message_body_length are not shown in the information response of the +CMGL AT command by default. To show these optional fields, you have to specify the value 1 to the +CSDH AT command (command name in text: Show Text Mode Parameters):


AT+CSDH=1
OK


Execute the command line 'AT+CMGL="ALL"' again and the result should be:


AT+CMGL="ALL"
+CMGL: 1,"STO SENT","+85291234567",,,145,35
Hello, welcome to our SMS tutorial.
+CMGL: 2,"REC READ","+85291234567",,"07/02/18,00:05:10+32",145,36
A simple demo of SMS text messaging.
+CMGL: 3,"REC READ","+85291234567",,"07/02/18,00:07:22+32",145,37
It is easy to list SMS text messages.

OK


The value of the address_type fields (the sixth field from the left) of all three SMS text messages is 145, which indicates the phone numbers stored with these three SMS text messages are typical phone numbers in international format. The values of the sms_message_body_length fields (the seventh field from the left) of the three SMS text messages are 35, 36 and 37. They indicate the SMS text messages contain 35 characters, 36 characters and 37 characters respectively.

Preferences - Do not show ads


Page 62 of 65 Previous Page | Next Page
Contents at a Glance
Print this Web Page | | Back to Top
Custom Search

Feedback Form (Expand)

What do you think about this web page?






SMS Tutorial Table of Contents SMS Tutorial Contents at a Glance Preferences - Change Color Scheme Preferences - Do Not Show Ads Previous Page Next Page

Send Global SMS - Clickatell Gateway