Welcome to Developer's Home Thursday, April 24, 2014
Home - DevelopersHome.com

26.2.7. Example Demonstrating How to Use the +CMGR AT Command to Read SMS Text Messages from a Message Storage Area (SMS PDU Mode)


Preferences - Do not show ads

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


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

First, instruct the GSM/GPRS modem or mobile phone to operate in SMS PDU mode by entering the command line "AT+CMGF=0" in a terminal program (for example, HyperTerminal in Microsoft Windows). Below shows the response returned from Nokia 6021 to HyperTerminal after the execution of the command line "AT+CMGF=0":


AT+CMGF=0
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 PDU mode. To check whether the GSM/GPRS modem or mobile phone supports SMS PDU 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 value 0 represents SMS PDU mode and the value 1 represents SMS text mode. 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 +CMGR, enter the command line 'AT+CPMS="SM"' in the terminal program. Then press the Enter key on the keyboard and you should see something similar to this:


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

OK


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


Reading Text Messages

Third, you can now use the +CMGR AT command to read a text message from the message storage area. Let's say you want to read the SMS text message at index 1. You should enter the following command line in a terminal program:


AT+CMGR=1


Then press the Enter key on the keyboard and the GSM/GPRS modem or mobile phone will execute the command line "AT+CMGR=1". If it reads the text message successfully, the result returned should look something like this:


AT+CMGR=1
+CMGR: 0,,62
07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

OK


Here is a brief description of the various field values in the information response above:

  • The value of the first field, 0, indicates the status of the SMS message is "received unread", i.e. it is an SMS text message received from the SMSC and it has not been read.

  • The value of the second 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 third field, 62, indicates the TPDU encoded in the hexadecimal sequence consists of 62 octets.

  • The details of the SMS text message are encoded in the hexadecimal sequence. Examples include the sender's phone number, the body of the SMS text message, the time and date at which the SMS text message arrived at the SMS center, etc. We have discussed how to decode the hexadecimal sequence in the section titled "Some Explanation about the Decoding of the SMSC_number_and_TPDU Field Value of the +CMGR AT Command" of this SMS tutorial. Thus, we will not talk about it again here.

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

After using the +CMGR AT command to read the SMS text message, its status was changed from "received unread" to "received read". So, if the command line "AT+CMGR=1" is executed again, you will see a slightly different result (note the part highlighted in bold type):


AT+CMGR=1
+CMGR: 1,,62
07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

OK


The SMS text message retrieved above is an incoming text message. Note that if the SMS text message retrieved is an outgoing text message, the data contained in the hexadecimal sequence will have a different format and it should be decoded according to the section "Some Explanation about the Coding of the SMSC_number_and_TPDU Parameter Value of the +CMGS AT Command" of this SMS tutorial.

Suppose there is no message located at index 3 of the message storage area. If you specify the index 3 to the +CMGR AT command, the GSM/GPRS modem or mobile phone will return +CMS error 321 (which means the index is invalid), like this:


AT+CMGR=3
+CMS ERROR: 321

Preferences - Do not show ads


Page 59 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

Bulk SMS Gateway