26.2.3. Example
Demonstrating How to Use the +CMGR AT Command to Read SMS Text
Messages from a Message Storage Area (SMS Text Mode)
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 text 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 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 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 would like to read the text
message at index 1. You should enter the following command line in
the 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 SMS text message successfully, the result returned should
look something like this:
AT+CMGR=1 +CMGR:
"REC UNREAD","+85291234567",,"07/02/18,00:05:10+32" It
is easy to read text messages via AT commands.
OK
Here
is a brief description of the various field values in the information
response above:
The
value of the first field, "REC UNREAD", indicates the
status of the text message is "received unread", which
means it is a text message received from the SMSC and it has not
been read.
The
value of the second field, "+85291234567", is the sender's
phone number.
The
value of the third 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 fourth 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 fifth field, "It is easy to read text messages via
AT commands.", is the body of the SMS message.
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:
"REC READ","+85291234567",,"07/02/18,00:05:10+32" It
is easy to read text messages via AT commands.
OK
The
SMS text message "It is easy to read text messages via AT
commands." is an incoming text message. If the SMS text message
retrieved is an outgoing text message, the format of the +CMGR
information response will be different. To see the differences, first
write a new outgoing text message to the message storage area using
the +CMGW AT command (command
name in text: Write Message to Memory), like this:
AT+CMGW="+85291234567" >
A simple example of SMS text messaging. +CMGW: 2
OK
(Note:
Make sure the same message storage area is selected for writing and
reading text messages. See "Selecting
the Message Storage Areas to be Used for SMS Reading, Writing,
Deleting, Sending or Receiving" if you do not know how this
can be done.)
Then
read both text messages using the +CMGR AT command:
AT+CMGR=1;+CMGR=2 +CMGR:
"REC READ","+85291234567",,"07/02/18,00:05:10+32" It
is easy to read text messages via AT commands.
+CMGR: "STO
UNSENT","+85291234567", A simple example of SMS
text messaging.
OK
As
you can see, the +CMGR information response of the incoming text
message contains the service center timestamp field, while that of
the outgoing text message does not.
As
mentioned in the earlier section "Format
of the Information Response of the +CMGR AT Command in SMS Text Mode"
of this SMS tutorial, eight optional fields are
not shown in the information
response of the +CMGR AT command by default. They are
address_type,
TPDU_first_octet,
protocol_identifier,
data_coding_scheme,
validity_period,
service_center_address,
service_center_address_type
and sms_message_body_length.
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),
like this:
AT+CSDH=1 OK
Now
send the command line "AT+CMGR=1;+CMGR=2" to the GSM/GPRS
modem or mobile phone again and you should obtain the following
result:
AT+CMGR=1;+CMGR=2 +CMGR:
"REC
READ","+85291234567",,"07/02/18,00:05:10+32",145,4,0,0,"+85290000000",145,49 It
is easy to read text messages via AT commands.
+CMGR: "STO
UNSENT","+85291234567",,145,17,0,0,167,"+85290000000",145,39 A
simple example of SMS text messaging.
OK
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
Feedback Form (ExpandCollapse)
|
|