sms_tutorial_wonderful_rk

221
Downloaded from http://www.developershome.com/sms/smsIntro.asp 1. Introduction to SMS Messaging...............................5 1.1. What is SMS (Short Message Service)?.....................5 1.2. Concatenated SMS Messages / Long SMS Messages............5 1.3. EMS (Enhanced Messaging Service).........................6 2. What Makes SMS Messaging So Successful Worldwide?...........6 2.1. SMS Messages can be Sent and Read at Any Time............6 2.2. SMS Messages can be Sent to an Offline Mobile Phone......6 2.3. SMS Messaging is Less Disturbing While You can Still Stay in Touch......................................................7 2.4. SMS Messages are Supported by 100% GSM Mobile Phones and They can be Exchanged between Different Wireless Carriers.....7 2.5. SMS is a Suitable Technology for Wireless Applications to Build on......................................................7 3. Example Applications of SMS Messaging.......................7 3.1. Person-to-Person Text Messaging..........................8 3.2. Provision of Information.................................8 3.3. Downloading..............................................8 3.4. Alerts and Notifications.................................9 3.4.1. Email, Fax and Voice Message Notifications...........9 3.4.2. E-commerce and Credit Card Transaction Alerts........9 3.4.3. Stock Market Alerts..................................9 3.4.4. Remote System Monitoring............................10 3.5. Two-way Interactive Text Messaging Applications.........10 3.6. SMS Marketing...........................................11 4. What is an SMS Center / SMSC?..............................11 5. Basic Concepts of SMS Technology...........................11 5.1. Validity Period of an SMS Message.......................11 5.2. Message Status Reports..................................12 5.3. Message Submission Reports..............................12 5.4. Message Delivery Reports................................12 6. Intra-operator SMS Messages................................13 6.1. Transmission Process of Intra-operator SMS Messages.....13 7. Inter-operator SMS Messages................................14 7.1. Transmission Process of Inter-operator SMS Messages.....14 8. International SMS Messages.................................16 9. What is an SMS Gateway?....................................16 9.1. Open Source and Free SMS Gateway Software...............19 10. How to Send SMS Messages from a Computer / PC?............19 10.1. The 1st Way: Sending SMS Messages from a Computer Using a Mobile Phone or GSM/GPRS Modem...............................20

Upload: api-3714617

Post on 16-Nov-2014

445 views

Category:

Documents


2 download

TRANSCRIPT

Downloaded from http://www.developershome.com/sms/smsIntro.asp

1. Introduction to SMS Messaging..................................................................................................51.1. What is SMS (Short Message Service)?...............................................................................51.2. Concatenated SMS Messages / Long SMS Messages..........................................................51.3. EMS (Enhanced Messaging Service)...................................................................................6

2. What Makes SMS Messaging So Successful Worldwide?..........................................................62.1. SMS Messages can be Sent and Read at Any Time.............................................................62.2. SMS Messages can be Sent to an Offline Mobile Phone.....................................................62.3. SMS Messaging is Less Disturbing While You can Still Stay in Touch..............................72.4. SMS Messages are Supported by 100% GSM Mobile Phones and They can be Exchanged between Different Wireless Carriers...........................................................................................72.5. SMS is a Suitable Technology for Wireless Applications to Build on.................................7

3. Example Applications of SMS Messaging..................................................................................73.1. Person-to-Person Text Messaging........................................................................................83.2. Provision of Information.......................................................................................................83.3. Downloading.........................................................................................................................83.4. Alerts and Notifications........................................................................................................9

3.4.1. Email, Fax and Voice Message Notifications................................................................93.4.2. E-commerce and Credit Card Transaction Alerts..........................................................93.4.3. Stock Market Alerts.......................................................................................................93.4.4. Remote System Monitoring.........................................................................................10

3.5. Two-way Interactive Text Messaging Applications...........................................................103.6. SMS Marketing...................................................................................................................11

4. What is an SMS Center / SMSC?..............................................................................................115. Basic Concepts of SMS Technology.........................................................................................11

5.1. Validity Period of an SMS Message...................................................................................115.2. Message Status Reports......................................................................................................125.3. Message Submission Reports.............................................................................................125.4. Message Delivery Reports..................................................................................................12

6. Intra-operator SMS Messages....................................................................................................136.1. Transmission Process of Intra-operator SMS Messages....................................................13

7. Inter-operator SMS Messages....................................................................................................147.1. Transmission Process of Inter-operator SMS Messages....................................................14

8. International SMS Messages.....................................................................................................169. What is an SMS Gateway?........................................................................................................16

9.1. Open Source and Free SMS Gateway Software.................................................................1910. How to Send SMS Messages from a Computer / PC?.............................................................19

10.1. The 1st Way: Sending SMS Messages from a Computer Using a Mobile Phone or GSM/GPRS Modem..................................................................................................................20

10.1.1. Major Drawback of Sending SMS Messages through a Mobile Phone or GSM/GPRS Modem -- Low SMS Sending Rate...................................................................23

10.2. The 2nd Way: Sending SMS Messages from a Computer through a Connection to the SMSC or SMS Gateway of a Wireless Carrier or SMS Service Provider.................................23

10.2.1. Difficulties in Getting a Direct Connection to the SMSC or SMS Gateway of a Wireless Carrier.....................................................................................................................2310.2.2. SMS Service Providers (SMS Gateway Providers, SMS Resellers, SMS Brokers). 2410.2.3. Sending SMS Messages Using a Protocol / Interface Supported by an SMSC or SMS Gateway........................................................................................................................25

11. How to Receive SMS Messages Using a Computer / PC?......................................................27

11.1. The 1st Way: Using a Computer to Receive SMS Messages through a Mobile Phone or GSM/GPRS Modem..................................................................................................................2811.2. The 2nd Way: Using a Computer to Receive SMS Messages through a Connection to the SMSC or SMS Gateway of a Wireless Carrier..........................................................................3011.3. The 3rd Way: Using a Computer to Receive SMS Messages through a Connection to the SMS Gateway of an SMS Service Provider..............................................................................30

11.3.1. SIM Hosting...............................................................................................................3011.3.2. Shared Phone Number...............................................................................................31

11.4. Receiving SMS Messages Using a Protocol / Interface Supported by an SMSC or SMS Gateway.....................................................................................................................................33

12. Introduction to GSM / GPRS Wireless Modems.....................................................................3512.1. What is a GSM Modem?..................................................................................................3512.2. What is a GPRS Modem?.................................................................................................3612.3. Which is Better: Mobile Phone or GSM / GPRS Modem?..............................................36

13. How to Use Microsoft HyperTerminal to Send AT Commands to a Mobile Phone or GSM/GPRS Modem?....................................................................................................................37

13.1. What is Microsoft HyperTerminal?..................................................................................3713.2. The Procedure for Sending AT Commands to a Mobile Phone or GSM/GPRS Modem Using MS HyperTerminal.........................................................................................................38

14. Introduction to AT Commands................................................................................................4114.1. Basic Commands and Extended Commands....................................................................43

15. General Syntax of Extended AT Commands...........................................................................4315.1. Information Response and Final Result Code..................................................................4515.2. Case Sensitivity of AT Commands...................................................................................45

16. Result Codes of AT Commands..............................................................................................4516.1. Final Result Codes of AT Commands..............................................................................45

16.1.1. The OK Final Result Code........................................................................................4616.1.2. The ERROR Final Result Code.................................................................................46

16.2. Final Result Code Specific to SMS AT Commands.........................................................4816.2.1. The +CMS ERROR Final Result Code -- Notifies the Occurrences and Causes of Message Service Failures......................................................................................................48

16.3. Unsolicited Result Codes of AT Commands....................................................................5117. AT Command Operations: Test, Set, Read and Execution.....................................................52

17.1. Test Command -- Checks Whether a Certain AT Command is Supported......................5217.2. Set Command -- Changes the Settings Used for Certain Tasks.......................................5417.3. Read Command -- Retrieves the Current Settings Used for Certain Tasks......................5617.4. Execution Command -- Performs an Action or Retrieve Information/Status about the GSM/GPRS Modem or Mobile Phone......................................................................................56

18. Testing the Communication between the PC and GSM/GPRS Modem or Mobile Phone......5819. Checking if the GSM/GPRS Modem or Mobile Phone Supports the Use of AT Commands to Send, Receive and Read SMS Messages.......................................................................................58

19.1. Using the AT Command +CSMS to Check if Mobile-originated SMS Messages and Mobile-terminated SMS Messages are Supported....................................................................5919.2. Checking if the AT Commands Required for Sending, Receiving and Reading SMS Messages are Supported............................................................................................................60

19.2.1. Overview of the AT Commands Required................................................................6119.2.2. Beginning the Check..................................................................................................61

20. Operating Mode: SMS Text Mode and SMS PDU Mode.......................................................6220.1. Comparison of SMS Text Mode and SMS PDU Mode....................................................6320.2. Selecting the Operating Mode (AT+CMGF)....................................................................65

21. Setting or Reading the Service Center Address / SMSC Address (AT+CSCA).....................6621.1. Syntax of the +CSCA AT Command...............................................................................67

21.1.1. Setting the Service Center Address...........................................................................6721.1.2. Reading the Service Center Address.........................................................................68

21.2. Example Demonstrating How to Use the +CSCA AT Command to Set and Read the Service Center Address..............................................................................................................68

22. Preferred Message Storage (AT+CPMS)................................................................................6922.1. Selecting the Message Storage Areas to be Used for SMS Reading, Writing, Deleting, Sending or Receiving.................................................................................................................6922.2. Format of the Information Response of the +CPMS AT Command................................7122.3. Finding the Message Storage Areas Supported by a GSM/GPRS Modem or Mobile Phone.........................................................................................................................................7122.4. Example Demonstrating How to Use the +CPMS AT Command to Select the Message Storage Areas to be Used...........................................................................................................7222.5. Finding the Message Storage Areas Currently Used, Number of Messages Currently Stored and Maximum Number of Messages Allowed...............................................................74

22.5.1. Example.....................................................................................................................7623. Writing SMS Messages to Memory / Message Storage (AT+CMGW)..................................77

23.1. Syntax of the +CMGW AT Command in SMS Text Mode.............................................7723.2. SMSC Number Stored with the SMS Message (SMS Text Mode)..................................7923.3. Format of the Information Response of the +CMGW AT Command in SMS Text Mode...................................................................................................................................................7923.4. Example Demonstrating How to Use the +CMGW AT Command to Write SMS Text Messages to Message Storage in SMS Text Mode....................................................................8023.5. Syntax of the +CMGW AT Command in SMS PDU Mode............................................8423.6. SMSC Number Stored with the SMS Message (SMS PDU Mode).................................8623.7. Format of the Information Response of the +CMGW AT Command in SMS PDU Mode...................................................................................................................................................8623.8. Example Demonstrating How to Use the +CMGW AT Command to Write SMS Text Messages to Message Storage in SMS PDU Mode...................................................................87

24. Deleting SMS Messages from Message Storage (AT+CMGD)..............................................9124.1. Syntax of the +CMGD AT Command..............................................................................9124.2. Finding the Supported Indexes and Flag Values..............................................................9224.3. Example Demonstrating How to Use the +CMGD AT Command to Delete SMS Text Messages....................................................................................................................................93

25. Sending SMS Messages from a Computer / PC Using AT Commands (AT+CMGS, AT+CMSS)....................................................................................................................................94

25.1. Comparison Table of the AT Commands +CMGS and +CMSS......................................9525.2. Syntax of the +CMGS AT Command in SMS Text Mode...............................................9625.3. Format of the Information Response of the +CMGS AT Command in SMS Text Mode9725.4. Example Demonstrating How to Use the +CMGS AT Command to Send SMS Text Messages in SMS Text Mode....................................................................................................9825.5. Syntax of the +CMGS AT Command in SMS PDU Mode............................................101

25.5.1. Some Explanation about the Coding of the SMSC_number_and_TPDU Parameter Value of the +CMGS AT Command...................................................................................103

25.6. Format of the Information Response of the +CMGS AT Command in SMS PDU Mode.................................................................................................................................................10925.7. Example Demonstrating How to Use the +CMGS AT Command to Send SMS Text Messages in SMS PDU Mode.................................................................................................10925.8. Syntax of the +CMSS AT Command in SMS Text Mode and PDU Mode...................113

25.9. Format of the Information Response of the +CMSS AT Command in SMS Text Mode and PDU Mode........................................................................................................................11425.10. SMSC Number to be Used by the +CMSS AT Command to Send SMS Messages....115

25.10.1. SMS Text Mode.....................................................................................................11625.10.2. SMS PDU Mode....................................................................................................11725.10.3. Reading the SMSC Number Saved with an SMS Message...................................119

25.11. Example Demonstrating How to Use the +CMSS AT Command to Send Text Messages from a Message Storage Area (SMS Text Mode and SMS PDU Mode)................................121

26. Reading SMS Messages from a Message Storage Area Using AT Commands (AT+CMGR, AT+CMGL).................................................................................................................................124

26.1. Comparison Table of the AT Commands +CMGR and +CMGL..................................12726.2. Using the +CMGR AT Command to Read an SMS Message from a Message Storage Area..........................................................................................................................................128

26.2.1. Syntax of the +CMGR AT Command in SMS Text Mode.....................................12926.2.2. Format of the Information Response of the +CMGR AT Command in SMS Text Mode....................................................................................................................................12926.2.3. Example Demonstrating How to Use the +CMGR AT Command to Read SMS Text Messages from a Message Storage Area (SMS Text Mode)...............................................13526.2.4. Syntax of the +CMGR AT Command in SMS PDU Mode.....................................13926.2.5. Format of the Information Response of the +CMGR AT Command in SMS PDU Mode....................................................................................................................................13926.2.6. Some Explanation about the Decoding of the SMSC_number_and_TPDU Field Value of the +CMGR AT Command...................................................................................14126.2.7. Example Demonstrating How to Use the +CMGR AT Command to Read SMS Text Messages from a Message Storage Area (SMS PDU Mode)..............................................148

26.3. Using the +CMGL AT Command to List SMS Messages of a Certain Status from a Message Storage Area.............................................................................................................151

26.3.1. Syntax of the +CMGL AT Command in SMS Text Mode.....................................15226.3.2. Format of the Information Response of the +CMGL AT Command in SMS Text Mode....................................................................................................................................15326.3.3. Finding the Message Status Values Supported by the +CMGL AT Command in SMS Text Mode...................................................................................................................15626.3.4. Example Demonstrating How to Use the +CMGL AT Command to List SMS Text Messages from a Message Storage Area (SMS Text Mode)...............................................15626.3.5. Syntax of the +CMGL AT Command in SMS PDU Mode.....................................16026.3.6. Format of the Information Response of the +CMGL AT Command in SMS PDU Mode....................................................................................................................................16126.3.7. Finding the Message Status Values Supported by the +CMGL AT Command in SMS PDU Mode..................................................................................................................16426.3.8. Example Demonstrating How to Use the +CMGL AT Command to List SMS Text Messages from a Message Storage Area (SMS PDU Mode)..............................................165

1. Introduction to SMS Messaging

1.1. What is SMS (Short Message Service)?

SMS stands for Short Message Service. It is a technology that enables the sending and receiving of messages between mobile phones. SMS first appeared in Europe in 1992. It was included in the GSM (Global System for Mobile Communications) standards right at the beginning. Later it was ported to wireless technologies like CDMA and TDMA. The GSM and SMS standards were originally developed byETSI. ETSI is the abbreviation for European Telecommunications Standards Institute. Now the 3GPP(Third Generation Partnership Project) is responsible for the development and maintenance of the GSM and SMS standards.

As suggested by the name "Short Message Service", the data that can be held by an SMS message is very limited. One SMS message can contain at most 140 bytes (1120 bits) of data, so one SMS message can contain up to:

160 characters if 7-bit character encoding is used. (7-bit character encoding is suitable for encoding Latin characters like English alphabets.)

70 characters if 16-bit Unicode UCS2 character encoding is used. (SMS text messages containing non-Latin characters like Chinese characters should use 16-bit character encoding.)

SMS text messaging supports languages internationally. It works fine with all languages supported by Unicode, including Arabic, Chinese, Japanese and Korean.

Besides text, SMS messages can also carry binary data. It is possible to send ringtones, pictures, operator logos, wallpapers, animations, business cards (e.g. VCards) and WAP configurations to a mobile phone with SMS messages.

One major advantage of SMS is that it is supported by 100% GSM mobile phones. Almost all subscription plans provided by wireless carriers include inexpensive SMS messaging service. Unlike SMS, mobile technologies such as WAP and mobile Java are not supported on many old mobile phone models.

1.2. Concatenated SMS Messages / Long SMS Messages

One drawback of the SMS technology is that one SMS message can only carry a very limited amount of data. To overcome this drawback, an extension called concatenated SMS (also known as long SMS) was developed. A concatenated SMS text message can contain more than 160 English characters. Concatenated SMS works like this: The sender's mobile phone breaks down a long message into smaller parts and sends each of them as a single SMS message. When these SMS messages reach the destination, the recipient mobile phone will combine them back to one long message.

The drawback of concatenated SMS is that it is less widely supported than SMS on wireless devices.

1.3. EMS (Enhanced Messaging Service)

Besides the data size limitation, SMS has another major drawback -- an SMS message cannot include rich-media content such as pictures, animations and melodies. EMS (Enhanced Messaging Service) was developed in response to this. It is an application-level extension of SMS. An EMS message can include pictures, animations and melodies. Also, the formatting of the text inside an EMS message is changeable. For example, the message sender can specify whether the text in an EMS message should be displayed in bold or italic, with a large font or a small font.

The drawback of EMS is that it is less widely supported than SMS on wireless devices. Also, many EMS-enabled wireless devices only support a subset of the features defined in the EMS specification. A certain EMS feature may be supported on one wireless device but not on the other.

2. What Makes SMS Messaging So Successful Worldwide?SMS is a success all over the world. The number of SMS messages exchanged every day is enormous. SMS messaging is now one of the most important revenue sources of wireless carriers. What is so special about SMS that makes it so popular worldwide? Some of the reasons are discussed below.

2.1. SMS Messages can be Sent and Read at Any Time

Nowadays, almost every person has a mobile phone and carries it most of the time. With a mobile phone, you can send and read SMS messages at any time, no matter you are in your office, on a bus or at home.

2.2. SMS Messages can be Sent to an Offline Mobile Phone

Unlike a phone call, you can send an SMS message to your friend even when he/she has not switched on the mobile phone or when he/she is in a place where the wireless signal is temporarily unavailable. The SMS system of the mobile network operator will store the SMS message and later send it to your friend when his/her mobile phone is online.

2.3. SMS Messaging is Less Disturbing While You can Still Stay in Touch

Unlike a phone call, you do not need to read or reply an SMS message immediately. Besides, writing and reading SMS messages do not make any noise. While you have to run out of a theater or library to answer a phone call, you do not need to do so if SMS messaging is used.

2.4. SMS Messages are Supported by 100% GSM Mobile Phones and They can be Exchanged between Different Wireless Carriers

SMS messaging is a very mature technology. All GSM mobile phones support it. Not only that you can exchange SMS messages with mobile users of the same wireless carrier, but you can also exchange SMS messages with mobile users of many other wireless carriers worldwide.

2.5. SMS is a Suitable Technology for Wireless Applications to Build on

Here are some of the reasons that make SMS a suitable technology for wireless applications to build on:

Firstly, SMS messaging is supported by 100% GSM mobile phones. Building wireless applications on top of the SMS technology can maximize the potential user base.

Secondly, SMS messages are capable of carrying binary data besides text. They can be used to transfer ringtones, pictures, operator logos, wallpapers, animations, VCards, VCals (calendar entries), etc.

Thirdly, SMS supports reverse billing, which enables payment to be made conveniently. For example, suppose you want to develop a commercial ringtone download application that charges a fee from the user for each ringtone downloaded. One way to accept payment is to use a reverse billing phone number obtained from a wireless carrier. To buy a ringtone, the user will write an ordinary SMS text message that contains the ID of the ringtone he/she wants to buy and send it to your SMS application's reverse billing phone number. Your SMS application will then send back one or more reverse billing SMS messages that carry the ringtone. The user will be charged a fee for the reverse billing SMS messages he/she received. The fee will be included in the user's monthly mobile phone bill or be deducted from his/her prepaid card credits. Depending on the agreement between you and the wireless carrier, all or part of the money received will be given to you.

3. Example Applications of SMS MessagingThere are many different kinds of SMS applications on the market today and many others are being developed. Applications in which SMS messaging can be utilized are virtually unlimited. We will describe some common examples of SMS applications below to give you some ideas of what can be done with SMS messaging.

3.1. Person-to-Person Text Messaging

Person-to-person text messaging is the most commonly used SMS application and it is what the SMS technology was originally designed for. In these kinds of text messaging applications, a mobile user types an SMS text message using the keypad of his/her mobile phone, then he/she inputs the mobile phone number of the recipient and clicks a certain option on the screen, such as "Send" or "OK", to send the text message out. When the recipient mobile phone receives the SMS text message, it will notify the user by giving out a sound or vibrating. The user can read the SMS text message some time later or immediately and can send a text message back if he/she wants.

A chat application is another kind of person-to-person text messaging application that allows a group of people to exchange SMS text messages interactively. In a chat application, all SMS text messages sent and received are displayed on the mobile phone's screen in order of date and time. SMS text messages written by different mobile users may be displayed in different colors for better readability, like this:

Me: Let's have a meeting at 10 am tomorrow to discuss the details of our next SMS text messaging application.

Tom: OK.

Alan: I have another meeting at 10 am. I suggest having the meeting at 11:30 am tomorrow. What do you think?

3.2. Provision of Information

A popular application of the SMS technology other than person-to-person text messaging is the provision of information to mobile users. Many content providers make use of SMS text messages to send information such as news, weather report and financial data to their subscribers. Many of these information services are not free. Reverse billing SMS is a common way used by content providers to bill their users. The user is charged a certain fee for each reverse billing SMS message received. The fee will either be included in the monthly mobile phone bill or be deducted from prepaid card credits.

3.3. Downloading

SMS messages can carry binary data and so SMS can be used as the transport medium of wireless downloads. Objects such as ringtones, wallpapers, pictures and operator logos can be encoded in one or more SMS messages depending on the object's size. Like information services, wireless download services are usually not free and reverse billing SMS is a common way used by content providers to bill their customers. The object to be downloaded is encoded in one or more reverse billing SMS messages. The mobile user who requests the object will be charged a certain fee for each reverse billing SMS message received. If the mobile user is using a monthly mobile phone service plan, the download fee will be included in his/her next monthly bill; if the mobile user is using a prepaid SIM card, the download fee will be deducted from the prepaid credits.

3.4. Alerts and Notifications

SMS is a very suitable technology for delivering alerts and notifications of important events. This is because of two reasons:

1. A mobile phone is a device that is carried by its owner most of the time. Whenever an SMS text message is received, the mobile phone will notify you by giving out a sound or by vibrating. You can check what the SMS text message contains immediately.

2. SMS technology allows the "push" of information. This is different from the "pull" model where a device has to poll the server regularly in order to check whether there is any new information. The "pull" model is less suitable for alert and notification applications, since it wastes bandwidth and increases server load.

Some common examples of SMS alert and notification applications are described below.

3.4.1. Email, Fax and Voice Message Notifications

In an email notification system, a server sends a text message to the user's mobile phone whenever an email arrives at the inbox. The SMS text message can include the sender's email address, the subject and the first few lines of the email body. An email notification system may allow the user to customize various filters so that an SMS alert is sent only if the email message contains certain keywords or if the email sender is an important person. The use cases for fax or voice message are similar.

3.4.2. E-commerce and Credit Card Transaction Alerts

Whenever an e-commerce or credit card transaction is made, the server sends a text message to the user's mobile phone. The user can know immediately whether any unauthorized transactions have been made.

3.4.3. Stock Market Alerts

In a stock market alert application, a program is constantly monitoring and analyzing the stock market. If a certain condition is satisfied, the program will send a text message to the user's mobile phone to notify him/her of the situation. For example, you can configure the alert system such that if the stock price of a company is lower than a certain value or drops by a certain percentage, it will send an SMS alert to you.

3.4.4. Remote System Monitoring

In a remote system monitoring application, a program (sometimes with the help of a group of sensors) is constantly monitoring the status of a remote system. If a certain condition is satisfied, the program will send a text message to the system administrator to notify him/her of the situation. For example, a program may be written to "ping" a server regularly. If no response is received from the server, the program can send an SMS alert to the system administrator to notify him/her that the server may be hanged.

3.5. Two-way Interactive Text Messaging Applications

SMS messaging technology can be used as the underlying communication medium between wireless devices and servers in a two-way interactive text messaging application. For example, search engines are two-way interactive text messaging applications. Let's say there is a dictionary search engine that supports queries in SMS text messages. It may operate like this:

To find out the meaning of the term "SMS text messaging", you can type "find: SMS text messaging" in an SMS text message and send it to the search engine's phone number. After receiving your SMS text message, the search engine parses it and finds that it begins with the command "find" and follows by the words "SMS text messaging". The search engine then knows you want to find out the meaning of the term "SMS text messaging". So, it sends a text message, which contains the meaning of the term "SMS text messaging", back to your mobile phone.

If the search result is very long and it cannot contain within a single SMS text message, the search engine adds "Page 1 of 2", "Page 1 of 3", etc, at the end of the reply SMS text message. The search engine also creates a session using your mobile phone number as the session ID and stores the term that you searched for (i.e. "SMS text messaging") in the session object.

To request the second page, you can send a text message with the content "page: 2" to the search engine's phone number. After receiving your SMS text message, the search engine parses it and finds that it begins with the command "page" and follows by "2". The search engine then knows you want the second page of the search result. It retrieves the term that you searched for last time from the session object and finds that it is "SMS text messaging". The search engine then sends a text message that contains the second page of the search result for the term "SMS text messaging" back to your mobile phone.

Many other two-way interactive text messaging applications can be built using a similar way. For example, a company may want to build an SMS messaging application to enable its employees to query the corporate database while they are working outdoors.

3.6. SMS Marketing

SMS messaging can be used as a marketing tool. An example is an SMS newsletter system. After signing up, the user will receive SMS text messages about the latest discounts and products of the company. If the user has any questions or comments, he/she can send a text message back with the questions or comments in it. The company may include its phone number in the SMS newsletter so that the user can talk to the customer service staff directly if he/she wants to do so.

4. What is an SMS Center / SMSC?An SMS center (SMSC) is responsible for handling the SMS operations of a wireless network. When an SMS message is sent from a mobile phone, it will reach an SMS center first. The SMS center then forwards the SMS message towards the destination. An SMS message may need to pass through more than one network entity (e.g. SMSC and SMS gateway) before reaching the destination. The main duty of an SMSC is to route SMS messages and regulate the process. If the recipient is unavailable (for example, when the mobile phone is switched off), the SMSC will store the SMS message. It will forward the SMS message when the recipient is available.

Very often an SMSC is dedicated to handle the SMS traffic of one wireless network. A network operator usually manages its own SMSC(s) and locates them inside its wireless network system. However, it is possible for a network operator to use a third-party SMSC that is located outside the wireless network system.

You must know the address of the wireless network operator's SMSC in order to use SMS messaging with your mobile phone. Typically an SMSC address is an ordinary phone number in the international format. A mobile phone should have a menu option that can be used to configure the SMSC address. Normally, the SMSC address is pre-set in the SIM card by the wireless network operator, which means you do not need to make any changes to it.

5. Basic Concepts of SMS TechnologyIn the following sections, we will introduce to you some basic concepts of the SMS technology.

5.1. Validity Period of an SMS Message

An SMS message is stored temporarily in the SMS center if the recipient mobile phone is offline. It is possible to specify the period after which the SMS message will be deleted from the SMS center so that the SMS message will not be forwarded to the recipient mobile phone when it becomes online. This period is called the validity period.

Here is an example that illustrates how validity period can be used. Suppose you find a very interesting program that is showing on TV now. You think your friend may want to watch it. So, you send a text message to tell your friend about the TV program. Let's say the TV program lasts for an hour. Your SMS text message will not be useful to your friend if his mobile phone is not available before the program finishes. In this situation, you may want to send a text message with a validity period of one hour to your friend. The wireless carrier will not deliver the SMS text message to your friend if his mobile phone is not available before the validity period ends.

A mobile phone should have a menu option that can be used to set the validity period. After setting it, the mobile phone will include the validity period in the outbound SMS messages automatically.

5.2. Message Status Reports

Sometimes you may want to know whether an SMS message has reached the recipient mobile phone successfully. To get this information, you need to set a flag in the SMS message to notify the SMS center that you want a status report about the delivery of this SMS message. The status report is sent to you in the form of an SMS message.

A mobile phone should have a menu option that can be used to set whether the status report feature is on or off. After setting it, the mobile phone will set the corresponding flag in the outbound SMS messages for you automatically. The status report feature is turned off by default on most mobile phones and GSM / GPRS modems.

5.3. Message Submission Reports

After leaving the mobile phone, an SMS message goes to the SMS center. When it reaches the SMS center, the SMS center will send back a message submission report to the mobile phone to inform whether there are any errors or failures (e.g. incorrect SMS message format, busy SMS center, etc). If there is no error or failure, the SMS center sends back a positive submission report to the mobile phone. Otherwise it sends back a negative submission report to the mobile phone. The mobile phone may then notify the user that the message submission was failed and what caused the failure.

If the mobile phone does not receive the message submission report after a period of time, it concludes that the message submission report has been lost. The mobile phone may then re-send the SMS message to the SMS center. A flag will be set in the new SMS message to inform the SMS center that this SMS message has been sent before. If the previous message submission was successful, the SMS center will ignore the new SMS message but send back a message submission report to the mobile phone. This mechanism prevents the sending of the same SMS message to the recipient multiple times.

Sometimes the message submission report mechanism is not used and the acknowledgement of message submission is done in a lower layer.

5.4. Message Delivery Reports

After receiving an SMS message, the recipient mobile phone will send back a message delivery report to the SMS center to inform whether there are any errors or failures (example causes: unsupported SMS message format, not enough storage space, etc). This process is transparent to the mobile user. If there is no error or failure, the recipient mobile phone sends back a positive delivery report to the SMS center. Otherwise it sends back a negative delivery report to the SMS center.

If the sender requested a status report earlier, the SMS center sends a status report to the sender when it receives the message delivery report from the recipient.

If the SMS center does not receive the message delivery report after a period of time, it concludes that the message delivery report has been lost. The SMS center then re-sends the SMS message to the recipient.

Sometimes the message delivery report mechanism is not used and the acknowledgement of message delivery is done in a lower layer.

6. Intra-operator SMS MessagesIf both you and your friend are using the mobile phone service of the same wireless network operator, the transmission of an SMS message from you to your friend will involve only one wireless network operator. This SMS message is called an intra-operator SMS message.

Typically, the cost for sending an intra-operator SMS message from a mobile phone is lower than that for sending other kinds of SMS messages such as inter-operator SMS messages. Some wireless network operators allow their subscribers to send unlimited intra-operator SMS messages free of charge.

6.1. Transmission Process of Intra-operator SMS Messages

The transmission of an intra-operator SMS message involves only one SMS center. After leaving the sender, the intra-operator SMS message reaches the SMS center. The SMS center then delivers the SMS message to the recipient mobile phone. If the recipient mobile phone is offline, the SMS center stores the SMS message. It will deliver the SMS message when the recipient mobile phone is online. If the SMS message's validity period expires and the recipient mobile phone is still offline, the SMS center will remove the SMS message.

When the SMS center receives the message delivery report from the recipient mobile phone or removes the SMS message (for example, when the validity period expires), it sends a status report to the sender if the sender requested one earlier.

The following figure illustrates the transmission process of an intra-operator SMS message:

7. Inter-operator SMS MessagesSuppose you and your friend are using the mobile phone service of wireless network operator A and wireless network operator B respectively. The transmission of an SMS message from you to your friend involves two wireless networks. This SMS message is called an inter-operator SMS message.

Typically, the cost for sending an inter-operator SMS message from a mobile phone is higher than that for sending an intra-operator SMS message.

7.1. Transmission Process of Inter-operator SMS Messages

The transmission of an inter-operator SMS message involves one or more SMS centers. Generally, there are two different ways for the transmission of inter-operator SMS messages. In the first way, signaling interconnections are set up between two wireless networks. When the originator SMS center receives an inter-operator SMS message, it gets the routing information from the recipient wireless network and delivers the SMS message to the recipient mobile phone directly. The following figure illustrates the transmission process:

The first way can be used if the two wireless networks involved in the transmission of the inter-operator SMS message are based on similar technologies. However, if this is not true, the second way has to be used. For example, when an SMS message is sent from a GSM network to a CDMA network. In the second way, the originator SMS center and the recipient SMS center are interconnected through an SMS gateway or with a communication protocol that is supported by both SMS centers. The SMS message first reaches the originator SMS center, which will then forward the SMS message towards the recipient SMS center. The recipient SMS center will be responsible for sending the SMS message to the recipient mobile phone and storing the SMS message if the recipient mobile phone is offline. The following figure illustrates the transmission process:

8. International SMS MessagesInter-operator SMS messages can be further divided into two categories -- local inter-operator SMS messages and international inter-operator SMS messages (international SMS messages). A local inter-operator SMS message is an SMS message that is sent from one wireless network operator to another wireless network operator in the same country, while an international SMS message is an SMS message that is sent from a wireless network operator in one country to a wireless network operator in another country.

Usually the cost for sending an international SMS message from a mobile phone is higher than that for sending a local inter-operator SMS message. Hence, the cost for sending an intra-operator SMS message <= the cost for sending a local inter-operator SMS message <= the cost for sending an international SMS message.

The interoperability of SMS messaging between two wireless networks locally and even internationally is undoubtedly a main factor that contributes to the success of SMS worldwide.

9. What is an SMS Gateway?One problem of SMS messaging is that SMSCs developed by different companies use their own communication protocol and most of these protocols are proprietary. For example, Nokia has an SMSC protocol called CIMD whereas another SMSC vendor, CMG, has an SMSC protocol called EMI. We cannot connect two SMSCs if they do not support a common SMSC protocol. To deal with this problem, an SMS gateway is placed between two SMSCs. This is illustrated in the following figure. The SMS gateway acts as a relay between the two SMSCs. It translates one SMSC protocol to another one. This way can be used by two different wireless carriers to interconnect their SMSCs for purposes such as enabling the exchange of inter-operator SMS messages.

Figure 1. An SMS gateway acts as a relay between two SMS centers.

Besides wireless carriers, content providers and SMS application developers may also find an SMS gateway useful. Let's consider the following situation. Suppose you are the developer of an SMS text messaging application. To send and receive SMS text messages on your server,

one way is to connect to the SMSCs of the wireless carriers. Different wireless carriers may use SMSCs from different vendors, which means your SMS text messaging application may need to support multiple SMSC-specific protocols. (This is illustrated in the following figure). As a result, the SMS text messaging application's complexity and development time increases.

Figure 2. An SMS text messaging application connects to SMSCs without an SMS gateway.

To deal with the above problem, an SMS gateway can be set up to handle the connections to the SMSCs. Now the SMS text messaging application only needs to know how to connect to the SMS gateway. To support more SMSCs, you just need to modify the settings of the SMS gateway. No change to the source code of the SMS text messaging application is required. The use of an SMS gateway can greatly shorten the SMS text messaging application's development time.

To connect to an SMS gateway, you can use an SMSC protocol such as SMPP and CIMD. Some SMS gateways support an HTTP / HTTPS interface. HTTP / HTTPS is easier to use than SMSC protocols. The drawback is that there may be fewer SMS features to use. For example, an SMS gateway may not support the sending of picture messages through the HTTP / HTTPS interface.

Figure 3. An SMS text messaging application connects to SMSCs through an SMS gateway.

Besides using a direct connection to the SMSC of a wireless carrier, another way to send and receive SMS text messages on a computer is to use a mobile phone or GSM/GPRS modem. To do this, your SMS text messaging application has to know how to communicate with the mobile phone or GSM/GPRS modem using AT commands.

Some SMS gateways are capable of handling the connections to mobile phones and GSM/GPRS modems. To send and receive SMS text messages with a mobile phone or GSM/GPRS modem, the SMS text messaging application only needs to know how to talk to the SMS gateway and does not need to know anything about AT commands. More details about GSM/GPRS modems and AT commands will be provided in later sections of this SMS tutorial.

Figure 4. An SMS text messaging application connects to a pool of mobile phones or GSM/GPRS modems through an SMS gateway.

9.1. Open Source and Free SMS Gateway Software

As you can see in the above sections, an SMS gateway has a lot of responsibilities in an SMS messaging system. So, SMS gateway software can be very complex and complex software is usually expensive. Fortunately, there are open source SMS gateway software packages that can be downloaded free of charge over the web. One high-quality free SMS gateway software package is Kannel, which is written in the C programming language. Kannel can handle connections to SMSCs, mobile phones and GSM/GPRS modems. It has an HTTP / HTTPS interface for the sending and receiving of SMS messages. More information about Kannel can be found on its web site http://www.kannel.org/.

10. How to Send SMS Messages from a Computer / PC?In general, there are two ways to send SMS messages from a computer / PC to a mobile phone:

1. Connect a mobile phone or GSM/GPRS modem to a computer / PC. Then use the computer / PC and AT commands to instruct the mobile phone or GSM/GPRS modem to send SMS messages.

2. Connect the computer / PC to the SMS center (SMSC) or SMS gateway of a wireless carrier or SMS service provider. Then send SMS messages using a protocol / interface supported by the SMSC or SMS gateway.

If you do not want to develop SMS software or applications but just want to use your computer / PC to send text messages, you may want to read our Quick Guide for Non-Developers.

10.1. The 1st Way: Sending SMS Messages from a Computer Using a Mobile Phone or GSM/GPRS Modem

The SMS specification has defined a way for a computer to send SMS messages through a mobile phone or GSM/GPRS modem. A GSM/GPRS modem is a wireless modem that works with GSM/GPRS wireless networks. A wireless modem is similar to a dial-up modem. The main difference is that a wireless modem transmits data through a wireless network whereas a dial-up modem transmits data through a copper telephone line. More information about GSM/GPRS modems will be provided in the section "Introduction to GSM / GPRS Wireless Modems". Most mobile phones can be used as a wireless modem. However, some mobile phones have certain limitations comparing to GSM/GPRS modems. This will be discussed in the section "Which is Better: Mobile Phone or GSM / GPRS Modem" later.

To send SMS messages, first place a valid SIM card from a wireless carrier into a mobile phone or GSM/GPRS modem, which is then connected to a computer. There are several ways to connect a mobile phone or GSM/GPRS modem to a computer. For example, they can be connected through a serial cable, a USB cable, a Bluetooth link or an infrared link. The actual way to use depends on the capability of the mobile phone or GSM/GPRS modem. For example, if a mobile phone does not support Bluetooth, it cannot connect to the computer through a Bluetooth link.

After connecting a mobile phone or GSM/GPRS modem to a computer, you can control the mobile phone or GSM/GPRS modem by sending instructions to it. The instructions used for controlling the mobile phone or GSM/GPRS modem are called AT commands. (AT commands are also used to control dial-up modems for wired telephone system.) Dial-up modems, mobile phones and GSM/GPRS modems support a common set of standard AT commands. In addition to this common set of standard AT commands, mobile phones and GSM/GPRS modems support an extended set of AT commands. One use of the extended AT commands is to control the sending and receiving of SMS messages.

The following table lists the AT commands that are related to the writing and sending of SMS messages:

AT command Meaning

+CMGS Send message

+CMSS Send message from storage

+CMGW Write message to memory

+CMGD Delete message

+CMGC Send command

+CMMS More messages to send

One way to send AT commands to a mobile phone or GSM/GPRS modem is to use a terminal program. A terminal program's function is like this: It sends the characters you typed to the mobile phone or GSM/GPRS modem. It then displays the response it receives from the mobile phone or GSM/GPRS modem on the screen. The terminal program on Microsoft Windows is called HyperTerminal. More details about the use of Microsoft HyperTerminal can be found in the "How to Use Microsoft HyperTerminal to Send AT Commands to a Mobile Phone or GSM/GPRS Modem" section of this SMS tutorial.

Below shows a simple example that demonstrates how to use AT commands and the HyperTerminal program of Microsoft Windows to send an SMS text message. The lines in bold type are the command lines that should be entered in HyperTerminal. The other lines are responses returned from the GSM / GPRS modem or mobile phone.

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

OKAT+CMSS=1+CMSS: 20

OK

Here is a description of what is done in the above example:

Line 1: "AT" is sent to the GSM / GPRS modem to test the connection. The GSM / GPRS modem sends back the result code "OK" (line 2), which means the connection between the HyperTerminal program and the GSM / GPRS modem works fine.

Line 3: The AT command +CMGF is used to instruct the GSM / GPRS modem to operate in SMS text mode. The result code "OK" is returned (line 4), which indicates the command line "AT+CMGF=1" has been executed successfully. If the result code "ERROR" is returned, it is likely that the GSM / GPRS modem does not support the SMS text mode. To confirm, type "AT+CMGF=?" in the HyperTerminal program. If the response is "+CMGF: (0,1)" (0=PDU mode and 1=text mode), then SMS text mode is supported. If the response is "+CMGF: (0)", then SMS text mode is not supported.

Line 5 and 6: The AT command +CMGW is used to write an SMS text message to the message storage of the GSM / GPRS modem. "+85291234567" is the recipient mobile phone number. After typing the recipient mobile phone number, you should press the Enter button of the keyboard. The GSM / GPRS modem will then return a prompt "> " and you can start typing the SMS text message "A simple demo of SMS text messaging.". When finished, press Ctrl+z of the keyboard.

Line 7: "+CMGW: 1" tells us that the index assigned to the SMS text message is 1. It indicates the location of the SMS text message in the message storage.

Line 9: The result code "OK" indicates the execution of the AT command +CMGW is successful.

Line 10: The AT command +CMSS is used to send the SMS text message from the message storage of the GSM / GPRS modem. "1" is the index of the SMS text message obtained from line 7.

Line 11: "+CMSS: 20" tells us that the reference number assigned to the SMS text message is 20.

Line 13: The result code "OK" indicates the execution of the AT command +CMSS is successful.

To send SMS messages from an application, you have to write the source code for connecting to and sending AT commands to the mobile phone or GSM/GPRS modem, just like what a terminal program does. You can write the source code in C, C++, Java, Visual Basic, Delphi or other programming languages you like. However, writing your own code has a few disadvantages:

You have to learn how to use AT commands. You have to learn how to compose the bits and bytes of an SMS message. For example,

to specify the character encoding (e.g. 7-bit encoding and 16-bit Unicode encoding) of an SMS message, you need to know which bits in the message header should be modified and what value should be assigned.

Sending SMS messages with a mobile phone or GSM/GPRS modem has a drawback -- the SMS transmission speed is low. As your SMS messaging application becomes more popular, it has to handle a larger amount of SMS traffic and finally the mobile phone or GSM/GPRS modem will not be able to take the load. To obtain a high SMS transmission speed, a direct connection to an SMSC or SMS gateway of a wireless carrier or SMS service provider is needed. However, AT commands are not used for communicating with an SMS center or SMS gateway. This means your have to make a big change to your SMS messaging application in order to move from a wireless-modem-based solution to a SMSC-based solution.

In most cases, instead of writing your own code for interacting with the mobile phone or GSM/GPRS modem via AT commands, a better solution is to use a high-level SMS messaging API (Application programming interface) / SDK (Software development kit) / library. The API / SDK / library encapsulates the low-level details. So, an SMS application developer does not need to know AT commands and the composition of SMS messages in the bit-level. Some SMS messaging APIs / SDKs / libraries support SMSC protocols in addition to AT commands. To move from a wireless-modem-based SMS solution to a SMSC-based SMS solution, usually you just need to modify a configuration file / property file or make a few changes to your SMS messaging application's source code.

The links to some open source and free SMS messaging libraries can be found in the article "Free Libraries/Tools for Sending/Receiving SMS with a Computer".

Another way to hide the low-level AT command layer is to place an SMS gateway between the SMS messaging application and the mobile phone or GSM/GPRS modem. (This has been described in the section "What is an SMS Gateway?" earlier.) Simple protocols such as HTTP / HTTPS can then be used for sending SMS messages in the application. If an SMSC protocol (e.g. SMPP, CIMD, etc) is used for communicating with the SMS gateway instead of HTTP / HTTPS, an SMS messaging API / SDK / library can be very helpful to you since it encapsulates the SMSC protocol's details.

Usually a list of supported / unsupported mobile phones or wireless modems is provided on the web site of an SMS messaging API / SDK / library or an SMS gateway software package. Remember to check the list if you are going to use an SMS messaging API / SDK / library or an SMS gateway software package.

10.1.1. Major Drawback of Sending SMS Messages through a Mobile Phone or GSM/GPRS Modem -- Low SMS Sending Rate

Using a mobile phone or GSM/GPRS modem to send SMS messages has a major drawback, that is the SMS sending rate is too low. Only 6-10 SMS messages can be sent per minute (when the "SMS over GSM" mode is used). The performance is not affected by the connection between the computer and the mobile phone or GSM/GPRS modem (i.e. the SMS sending rate is about the same no matter the mobile phone or GSM/GPRS modem is connected to the computer through a serial cable, USB cable, Bluetooth link or infrared link) and does not depend on whether a mobile phone or GSM/GPRS modem is used (i.e. the SMS sending rate is about the same no matter a mobile phone or a GSM/GPRS modem is used). The determining factor for the SMS sending rate is the wireless network.

10.2. The 2nd Way: Sending SMS Messages from a Computer through a Connection to the SMSC or SMS Gateway of a Wireless Carrier or SMS Service Provider

The way for sending SMS messages from a computer through a mobile phone or GSM/GPRS modem has a major limitation, that is the SMS sending rate is too low. If you need a high SMS sending rate, obtaining a direct connection to the SMS center (SMSC) or SMS gateway of a wireless carrier is necessary. The connection may be made through the Internet, X.25 or dial-up. If you cannot get a direct connection to the SMSC or SMS gateway of a wireless carrier, another choice is to get a connection to the SMS gateway of an SMS service provider, which will forward SMS messages towards a suitable SMSC.

10.2.1. Difficulties in Getting a Direct Connection to the SMSC or SMS Gateway of a Wireless Carrier

It can be difficult for small businesses or individual application developers to obtain a direct connection to the SMSC or SMS gateway of a wireless carrier since a wireless carrier may only provide such service to those who have huge SMS traffic. Buying SMS messages in bulk means the total fee will be very high (although the fee per SMS message will be low).

Besides, the information about the service (for example, cost of the service, protocols supported, network coverage) is usually not stated clearly on a wireless carrier's web site. This is because the wireless carrier staff wants to know more about your SMS messaging application, such as its nature and traffic requirement, before offering a price and providing further information to you. To decide which wireless carrier's service plan is the best, you have to discuss with the sales staff of each wireless carriers. This is troublesome if you just want to send a small number of SMS messages. (Of course if you need to send a large amount of SMS messages, say one million SMS messages per month, negotiating with the wireless carrier staff for a more favorable agreement is a necessary step.)

A more convenient way to send SMS messages is to use the SMS connectivity service of an SMS service provider, which is described in the next section.

10.2.2. SMS Service Providers (SMS Gateway Providers, SMS Resellers, SMS Brokers)

There is a demand for SMS connectivity from applications that does not require the sending or receiving of large amount of SMS messages. One example is a remote monitoring system. If the remote monitoring system finds that a certain server is not responding, it will send an SMS alert to the system administrator's mobile phone. This remote monitoring system will have a very small amount of SMS traffic per month since the servers being monitored should be working fine most of the time.

Since a wireless carrier usually does not provide direct SMSC or SMS gateway access to users without a large amount of SMS traffic, some companies come out to fill the gap. These companies are called SMS service providers. There is no minimal purchase requirement or monthly minimum usage requirement for many SMS service providers.

SMS service providers are also known as SMS gateway providers, SMS resellers and SMS brokers because of the following reasons:

SMS gateway providers -- An SMS service provider provides an SMS gateway for its users to send SMS messages to. This SMS gateway will then route the SMS messages to another SMS gateway or SMSC.

SMS resellers and SMS brokers -- SMS service providers buy a large amount of SMS messages from a lot of wireless carriers at a low price per SMS message. They then sell the SMS messages at a price higher than the cost.

Unlike wireless carriers, many SMS service providers provide detail information about their SMS connectivity service on their web site. For example, you may find the cost of the service, network coverage, protocols supported, developers' guide, etc, on the web site. Thus, the service of different SMS service providers can be compared easily. If you are not happy with, say the price or network coverage of an SMS service provider, you can simply leave its web site and find another SMS service provider.

Another advantage of using the SMS connectivity services of SMS service providers is that their network coverage is very good. They work hard to cover as many wireless networks as possible so as to make their services attractive. Some SMS service providers can send SMS messages not only to GSM wireless networks, but also to CDMA and TDMA wireless networks.

It is easy to send SMS messages with an SMS service provider. Here are the typical steps:

1. Register for an account on the SMS service provider's web site. (An SMS service provider may allow newly registered users to send a few free SMS messages for testing its service quality.)

2. Log into the account.

3. Buy a number of credits or SMS text messages online. Many SMS service providers support credit card payment and some also support PayPal.

4. Send SMS messages using a protocol / interface (e.g. HTTP, email, FTP) supported by the SMS service provider.

SMS service providers can be divided into two categories depending on how they require you to pay for their SMS messaging service:

Credit-based -- You purchase a number of credits from the SMS service provider's web site. Sending one SMS message will cost you one or more credits, depending on the country you send the SMS message to. For example, sending an SMS text message to India might cost you one credit while sending an SMS text message to the US might cost you two credits.

SMS-based -- You purchase a number of SMS messages from the SMS service provider's web site. The cost per SMS message is the same for all destinations. For example, if you purchase ten SMS messages, you can send at most ten SMS messages no matter the destination is India or the US.

The cost per SMS message sent depends on which SMS service provider you choose and how many SMS messages you purchase. It starts at around US $0.06 to US $0.07 per SMS message. The more SMS messages you purchased, the lower the cost of sending one SMS message.

A comparison of some SMS service providers can be found in "SMS Gateway Providers Comparison Table". You may also want to have a look at the article "How to Choose an SMS Gateway Provider" to learn some of the things that you should consider when choosing an SMS gateway provider for your SMS application.

10.2.3. Sending SMS Messages Using a Protocol / Interface Supported by an SMSC or SMS Gateway

After setting up an account with a wireless carrier or an SMS service provider, you can start sending SMS messages using a protocol / interface supported by the SMSC or SMS gateway. To communicate with an SMSC, an SMSC protocol is required. Most of these SMSC protocols are proprietary to the company that developed the SMSC. One widely used SMSC protocol is SMPP (Short Message Peer to Peer). It was originally a proprietary SMSC protocol created by Logica (an SMSC vendor). Now SMPP is an open SMSC protocol whose development is controlled by a non-profit organization SMS Forum. The following table lists some of the SMSC protocols and the SMSC vendors who develop the protocols:

SMSC vendor SMSC protocol

CMG (CMG and Logica have merged intoLogicaCMG.)

EMI (External Machine Interface)

UCP (Universal Computer Protocol)

Logica (CMG and Logica have merged intoLogicaCMG.)

(Now the SMS Forum is responsible for the development of SMPP.)

SMPP (Short Message Peer to Peer)

Nokia CIMD (Computer Interface to Message Distribution)

SEMA Group (Now Airwide Solutions)

OIS (Open Interface Specification)

SMS2000

SMS gateways of SMS service providers and wireless carriers very often support one or more of the following protocols / interfaces: HTTP, HTTPS (HTTP + SSL encryption), XML over HTTP / HTTPS, SMTP (email to SMS), FTP. Some also support the SMPP protocol, which is usually used by advanced users. SMSC protocols other than SMPP are not commonly supported.

It is very easy to send SMS messages if you use a simple protocol like HTTP / HTTPS. Here is an example. To send an SMS message "It is easy to send text messages" to the mobile phone number 61234567, you submit an HTTP GET to the SMS gateway using an URL that looks something like this:

http://smsgateway.company123.com/cgi-bin/sendsms?account=account123&password=password123&destination=61234567&textmessage=It+is+easy+to+send+text+messages

In the above URL:

"smsgateway.company123.com" is the host name of the SMS gateway that the SMS text message should be sent to.

"sendsms" is the file name of the CGI program that will be executed on the SMS gateway.

"account=account123" is used to assign the value account123 to the account parameter. Theaccount parameter specifies which account that the cost of the SMS text message should be deducted from.

"password=password123" is used to assign the value password123 to the password parameter. The password parameter specifies the password of the account.

"destination=61234567" is used to assign the value 61234567 to the destination parameter. Thedestination parameter specifies the recipient's mobile phone number.

"textmessage=It+is+easy+to+send+text+messages" is used to assign the value It is easy to send text messages to the textmessage parameter. The textmessage parameter specifies the SMS text message to be sent. There exists some "+" characters because the space character is a special character and it should be escaped if it appears in an URL. The escaped character of the space character is the "+" character.

After receiving your HTTP request, the SMS gateway will forward your SMS text message towards the recipient. The SMS gateway will then send back an HTTP response that contains a return value. The return value indicates whether there are any errors. For example, a certain SMS gateway may use the return value 0 to indicate that there is no error, the return value 1 to indicate the error "The account does not have enough credits", the return value 2 to indicate the error "The SMS message is too long", etc.

Note that the parameters that can be passed in an HTTP request and the return values varies between SMS gateways of different SMS service providers and wireless carriers. To send the same SMS message "It is easy to send text messages" to the same mobile phone number 61234567 but with a different SMS service provider, the URL to be used may become:

http://api.abccompany.com/cgi-bin/send?user=account123&passwd=password123&to=61234567&msg=It+is+easy+to+send+text+messages

Also, note that data transmitted over the Internet using the HTTP protocol is not secure. This means other people can read your account name and password you included in the above URL. To send data securely, you should use the HTTPS protocol instead of HTTP (i.e. change "http" at the beginning of the above URL to "https"), like this:

https://api.abccompany.com/cgi-bin/send?user=account123&passwd=password123&to=61234567&msg=It+is+easy+to+send+text+messages

11. How to Receive SMS Messages Using a Computer / PC?In general, there are three ways to receive SMS messages using your computer / PC:

1. Connect a mobile phone or GSM/GPRS modem to a computer / PC. Then use the computer / PC and AT commands to get the received SMS messages from the mobile phone or GSM/GPRS modem.

2. Get access to the SMS center (SMSC) or SMS gateway of a wireless carrier. Any SMS messages received will be forwarded to your computer / PC using a protocol / interface supported by the SMSC or SMS gateway.

3. Get access to the SMS gateway of an SMS service provider. Any SMS messages received will be forwarded to your computer / PC using a protocol / interface supported by the SMS gateway.

If you do not want to develop SMS software or applications but just want to use your computer / PC to receive text messages, you may want to read our Quick Guide for Non-Developers.

11.1. The 1st Way: Using a Computer to Receive SMS Messages through a Mobile Phone or GSM/GPRS Modem

Receiving SMS messages through a mobile phone or GSM/GPRS modem has a major advantage over the other two ways -- wireless carriers usually do not charge any fees for

receiving incoming SMS messages with their SIM cards. The disadvantage of receiving SMS messages this way is that a mobile phone or GSM/GPRS modem cannot handle a large amount of SMS traffic. One way to overcome this is to load balance the SMS traffic with a pool of mobile phones or GSM/GPRS modems. Each mobile phone or GSM/GPRS modem will have its own SIM card and mobile phone number.

In terms of programming, sending and receiving SMS messages through a mobile phone or GSM/GPRS modem are similar. What you need to do is to send instructions (in the form of AT commands) to the mobile phone or GSM/GPRS modem.

The following table lists the AT commands that are related to the receiving and reading of SMS messages:

AT command Meaning

+CNMI New message indications

+CMGL List messages

+CMGR Read messages

+CNMA New message acknowledgement

Below shows a simple example that demonstrates how to use AT commands and the HyperTerminal program of Microsoft Windows to read SMS text messages received by a GSM / GPRS modem or mobile phone. The lines in bold type are the command lines that should be entered in HyperTerminal. The other lines are responses returned from the GSM / GPRS modem or mobile phone.

ATOKAT+CMGF=1OKAT+CMGL="ALL"+CMGL: 1,"REC READ","+85291234567",,"06/11/11,00:30:29+32"Hello, welcome to our SMS tutorial.+CMGL: 2,"REC READ","+85291234567",,"06/11/11,00:32:20+32"A simple demo of SMS text messaging.

OK

Here is a description of what is done in the above example:

Line 1: "AT" is sent to the GSM / GPRS modem to test the connection. The GSM / GPRS modem sends back the result code "OK" (line 2), which means the connection between the HyperTerminal program and the GSM / GPRS modem works fine.

Line 3: The AT command +CMGF is used to instruct the GSM / GPRS modem to operate in SMS text mode. The result code "OK" is returned (line 4), which indicates the command line "AT+CMGF=1" has been executed successfully. If the result code "ERROR" is returned, it is likely that the GSM / GPRS modem does not support the SMS text mode. To confirm, type "AT+CMGF=?" in the HyperTerminal program. If the response is "+CMGF: (0,1)" (0=PDU mode and 1=text mode), then SMS text mode is supported. If the response is "+CMGF: (0)", then SMS text mode is not supported.

Line 5-9: The AT command +CMGL is used to list all SMS text messages in the message storage of the GSM / GPRS modem. There are two SMS text messages in the message storage: "Hello, welcome to our SMS tutorial." and "A simple demo of SMS text messaging.". "+85291234567" is the sender mobile phone number. "06/11/11,00:30:29+32" and "06/11/11,00:32:20+32" tell us when the SMS text messages were received by the SMSC. "+32" is the time zone. Note that the unit is a quarter of an hour. So, +32 means GMT+8 hours, since 32 quarters of an hour = 8 hours. "REC READ" indicates both of the SMS text messages have been read before.

Line 11: The result code "OK" indicates the execution of the AT command +CMGL is successful.

To enable an application to receive SMS messages, you have to write the source code for connecting to and sending AT commands to the mobile phone or GSM/GPRS modem, just like what a terminal program (such as HyperTerminal of Microsoft Windows) does. You can write the source code in C, C++, Java, Visual Basic, Delphi or other programming languages you like.

However, like what we have discussed in the earlier section "The 1st Way: Sending SMS Messages from a Computer Using a Mobile Phone or GSM/GPRS Modem", usually a better solution is to use a high-level SMS messaging API (Application programming interface) / SDK (Software development kit) / library instead of writing your own code for interacting with the mobile phone or GSM/GPRS modem via AT commands. The API / SDK / library encapsulates the low-level details. So, an SMS application developer does not need to know AT commands and the composition of SMS messages in the bit-level. Some SMS messaging APIs / SDKs / libraries support SMSC protocols in addition to AT commands. To move from a wireless-modem-based SMS solution to a SMSC-based SMS solution, usually you just need to modify a configuration file / property file or make a few changes to your SMS messaging application's source code. The links to some open source and free SMS messaging libraries can be found in the article "Free Libraries/Tools for Sending/Receiving SMS with a Computer".

Another high-level solution is to place an SMS gateway between the SMS messaging application and the mobile phone or GSM/GPRS modem. The SMS messaging application can then use simple protocols such as HTTP / HTTPS for receiving SMS messages. If an SMSC protocol (e.g. SMPP, CIMD, etc) is used for communicating with the SMS gateway instead of HTTP / HTTPS, an SMS messaging API / SDK / library can be very helpful to you since it encapsulates the SMSC protocol's details.

11.2. The 2nd Way: Using a Computer to Receive SMS Messages through a Connection to the SMSC or SMS Gateway of a Wireless Carrier

Like sending SMS messages, receiving SMS messages through a mobile phone or GSM/GPRS modem has a major limitation, that is the SMS transmission rate is too low. If you need a high SMS transmission rate, one way is to obtain a direct connection to the SMS center (SMSC) or SMS gateway of a wireless carrier. The connection may be made through the Internet, X.25 or dial-up.

To receive SMS messages through a direct connection to the SMSC or SMS gateway of a wireless carrier, usually you need to contact the wireless carrier staff to discuss the details. A wireless carrier may only provide such service to those who have huge SMS traffic and the total fee can be very high. (However, if you divide the total fee by the number of SMS messages that is allowed to receive through the SMSC or SMS gateway, you may find that the fee per incoming SMS message is very low.) The service may involve a one-time setup fee, a monthly subscription fee and/or a per-message fee. To know the exact cost of the service, protocols supported, network coverage and other information, usually you have to contact the wireless carrier staff.

11.3. The 3rd Way: Using a Computer to Receive SMS Messages through a Connection to the SMS Gateway of an SMS Service Provider

Another way to receive SMS messages using a computer is through a connection to the SMS gateway of an SMS service provider. SMS service providers generally support two kinds of solution:

1. SIM hosting -- The SMS service provider hosts a SIM card for you. All SMS messages sent to the mobile phone number of the SIM card will be forwarded to your system using a protocol / interface supported by the SMS gateway of the SMS service provider.

2. Shared phone number -- The SMS service provider provides you a phone number that is shared by its customers for receiving SMS messages. You have to specify one or more keywords to associate with your SMS application. If an SMS message is received and its content begins with a keyword specified by you, it will be forwarded to your system using a protocol / interface supported by the SMS gateway of the SMS service provider.

Some SMS service providers also provide SMS messaging solutions that are customized to your needs. For example, if you need to receive SMS messages from users in a few countries, an SMS service provider may help you open dedicated phone numbers in those countries and forward the received SMS messages to you in a unified way. To use such SMS messaging services, you have to contact the SMS service provider to discuss the details.

11.3.1. SIM Hosting

In this approach, an SMS service provider hosts a SIM card for you. The SIM card's mobile phone number will serve as a dedicated phone number of your SMS application. To use the service, you mail your SIM card to the SMS service provider. Later if you end the service, the

SMS service provider will mail the SIM card back to you. The SIM card will be put into a GSM/GPRS modem, which is connected to an SMS gateway. The SMS gateway will forward all received SMS messages to your computer using a certain protocol / interface. For example, HTTP, HTTPS, XML over HTTP/HTTPS, SMTP (SMS to email), SMPP, etc.

Like receiving SMS messages using your own mobile phone or GSM/GPRS modem, SIM hosting has a major weakness -- it cannot handle a large amount of SMS traffic. The advantage of SIM hosting is that you do not need to buy, set up and manage the necessary equipment. Also, you do not need to write any code to control the receiving of SMS messages from a GSM/GPRS modem via low-level AT commands, which means you can concentrate on the business logic of your SMS application. The SMS service provider may provide you a detail log of the incoming SMS messages.

11.3.1.1. Cost of SIM Hosting Service

SIM hosting service typically involves a one-time setup fee and a monthly hosting fee. Usually there is no per-message cost. Some SMS service providers do not state the cost of SIM hosting service on their website, which means you have to contact their staff directly.

If the SMS service provider is located overseas, the SIM card needs to support foreign network roaming. SMS roaming fees may be charged by the wireless carrier that provides you the SIM card.

11.3.2. Shared Phone Number

In this approach, the SMS service provider rents dedicated phone numbers from wireless carriers. Each phone number will then be shared by a number of the SMS service provider's customers for receiving SMS messages. Since the phone number is provided by a wireless carrier directly (not through a SIM card) and the SMS service provider has a high speed connection to the SMSC or SMS gateway of the wireless carrier, the SMS service provider is capable of handling a large amount of SMS messages with the phone number. As a result, the users of such shared phone number service can enjoy a higher SMS transmission rate than using SIM hosting services or using their own wireless modem. The cost of shared phone number service is less expensive than renting your own dedicated phone number from a wireless carrier. A detail log of the received SMS messages is sometimes provided by SMS service providers.

Like using SIM hosting service, using shared phone number service has the advantage that you do not need to buy, set up and manage any equipment (e.g. GSM / GPRS modems) and write any low-level code for receiving SMS messages. Hence, an SMS application developer can concentrate on the business logic of his/her SMS messaging application.

The weakness of shared phone number service is that the bandwidth is shared, i.e. the performance drops if the number of users sharing the phone number increases, or if the amount of incoming SMS traffic required by other users increases.

11.3.2.1. Registering Keywords

Since multiple users share a single phone number, when the SMS gateway of the SMS service provider receives an SMS message, it needs a way to determine which user to forward the SMS message to. SMS service providers' engineers come up with a solution like this: First, a user registers one or more keywords to associate with his/her SMS messaging application. Later when the SMS gateway receives an SMS message that begins with one of the keywords, it will forward the SMS message to that SMS messaging application.

Here is an example for illustration. Suppose you want to develop an SMS messaging application that provides free ringtone download service. A user will be able to perform two functions: listing the free ringtones that are available and requesting a ringtone to be sent to a mobile phone.

To receive SMS messages from users, you can subscribe to the shared phone number service of an SMS service provider and register two keywords, list and download, to be associated with your SMS messaging application.

To request the list of free ringtones that are available on your server, the user can send the following SMS text message to the shared phone number:

list

When the SMS gateway of the SMS service provider receives the above SMS text message, it parses its content and extracts the keyword list. It then searches a database to find out which SMS messaging application is associated with the keyword list. As your SMS messaging application is associated with the keyword list, the SMS text message will be forwarded to your SMS messaging application.

The keyword list indicates that the user wants the list of free ringtones that are available on your server. Hence, your SMS messaging application should return an SMS text message that contains something like this:

Free ringtones available:1. Ringtone ABC2. Ringtone CDEF3. Ringtone X4. Ringtone Y

To download the second ringtone, the user can send an SMS text message that looks like this to the shared phone number:

download 2

Since the keyword download is also associated with your SMS messaging application, the SMS gateway will forward the above SMS text message to your SMS messaging application. This time the keyword download indicates that the user wants to download a ringtone. Your SMS messaging application should encode the second ringtone in an SMS message and send it to the user.

11.3.2.2. Cost of Shared Phone Number Service

Shared phone number service typically involves a monthly subscription fee, a per-message fee and/or a per-keyword fee. A few SMS service providers also charge a one-time setup fee. Some SMS service providers do not state the cost of shared phone number service on their web site, which means you have to contact their staff directly.

11.4. Receiving SMS Messages Using a Protocol / Interface Supported by an SMSC or SMS Gateway

After setting up an account with a wireless carrier or an SMS service provider, the SMSC or SMS gateway will start forwarding inbound SMS messages to your SMS application using a certain a protocol / interface. To communicate with an SMSC, an SMSC protocol is required. As mentioned in earlier sections of this SMS tutorial, most of these SMSC protocols are proprietary to the company that developed the SMSC. One widely used SMSC protocol is SMPP (Short Message Peer to Peer). It was originally a proprietary SMSC protocol created by Logica (an SMSC vendor). Now SMPP is an open SMSC protocol whose development is controlled by a non-profit organization SMS Forum. The following table lists some of the SMSC protocols and the SMSC vendors who develop the protocols:

SMSC vendor SMSC protocol

CMG (CMG and Logica have merged intoLogicaCMG.)

EMI (External Machine Interface)

UCP (Universal Computer Protocol)

Logica (CMG and Logica have merged intoLogicaCMG.)

(Now the SMS Forum is responsible for the development of SMPP.)

SMPP (Short Message Peer to Peer)

Nokia CIMD (Computer Interface to Message Distribution)

SEMA Group (Now Airwide Solutions)

OIS (Open Interface Specification)

SMS2000

SMS gateways of SMS service providers and wireless carriers very often support one or more of the following protocols / interfaces: HTTP, HTTPS (HTTP + SSL encryption), XML over HTTP / HTTPS, SMTP (SMS to email), FTP. Some also support the SMPP protocol, which is usually used by advanced users. SMSC protocols other than SMPP are not commonly supported.

It is very easy to receive SMS messages from an SMS gateway if you use a simple protocol like HTTP / HTTPS. Let's take HTTP as an example. First, you log into your account and register a callback URL that points to a server-side script written by you. The server-side script is hosted on a web server / application server and it will be responsible for processing the received SMS messages. Commonly used server-side technologies include ASP, Java Servlet / JSP, Perl and PHP. If your server-side script is written in PHP, the callback URL should look something like this:

http://yourserver.com/receiveSMS.php

Whenever an SMS message is received, the SMS gateway will submit an HTTP GET to your web server / application server using the above callback URL. Details of the inbound SMS message such as its content, the sender's mobile phone number, the time and date that the SMS message is received, etc, will be appended to the callback URL. For example, suppose someone with the mobile phone number 61234567 sends an SMS text message "It is easy to receive text messages" to your SMS application's phone number. When the SMS gateway receives the SMS text message, it submits an HTTP GET to your web server / application server using an URL that looks like this:

http://yourserver.com/receiveSMS.php?sender=61234567&textmessage=It+is+easy+to+receive+text+messages&date_time=20060101+1830

In the above URL:

"yourserver.com" is the host name of your web server / application server that is responsible for processing the inbound SMS text message.

"receiveSMS.php" is the file that contains the PHP script for processing the inbound SMS text message.

"sender=61234567" assigns the value 61234567 to the sender parameter. The sender parameter specifies the originating mobile phone number.

"textmessage=It+is+easy+to+receive+text+messages" assigns the value It is easy to receive text messages to the textmessage parameter. The textmessage parameter specifies the content of the inbound SMS text message. The "+" character is the escaped character of the space character. The space character is a special character and it cannot appear in an URL without escaping.

"date_time=20060101+1830" assigns the value 20060101 1830 to the date_time parameter. Thedate_time parameter specifies the date and time that the SMS text message was received. In this example, 20060101 1830 means the SMS text message was received on 1 January 2006 at 6:30 pm. The date and time format may be different if you use the SMS gateway of a different SMS service provider or wireless carrier.

After processing the inbound SMS text message, you may want to send a reply SMS text message to the originator. Usually this can be done simply by including the reply SMS text message in the HTTP response you send back to the SMS gateway. The SMS gateway will then forward the included text message towards the mobile phone number 61234567. The cost for sending the SMS text message will be deducted from your account.

Note that the parameters that are included in the callback URL varies between SMS gateways of different SMS service providers or wireless carriers. For example, if a different SMS gateway is used, the previous SMS message "It is easy to receive text messages" may be forwarded to your web server / application server using a different URL, like below:

http://yourserver.com/receiveSMS.php?from=61234567&msg=It+is+easy+to+receive+text+messages&timestamp=1830+01012006

Also, note that data transmitted over the Internet using the HTTP protocol is not secure. This means other people can read the data included in the above URL. Some SMS gateways allow the use of the HTTPS protocol instead of HTTP in a callback URL (i.e. change "http" at the beginning of the above URL to "https") so that inbound SMS messages can be submitted to your web server / application server securely. (HTTPS must be enabled on your web server / application server.)

https://yourserver.com/receiveSMS.php?from=61234567&msg=It+is+easy+to+receive+text+messages&timestamp=1830+01012006

12. Introduction to GSM / GPRS Wireless Modems

12.1. What is a GSM Modem?

A GSM modem is a wireless modem that works with a GSM wireless network. A wireless modem behaves like a dial-up modem. The main difference between them is that a dial-up modem sends and receives data through a fixed telephone line while a wireless modem sends and receives data through radio waves.

A GSM modem can be an external device or a PC Card / PCMCIA Card. Typically, an external GSM modem is connected to a computer through a serial cable or a USB cable. A GSM modem in the form of a PC Card / PCMCIA Card is designed for use with a laptop computer. It should be inserted into one of the PC Card / PCMCIA Card slots of a laptop computer.

Like a GSM mobile phone, a GSM modem requires a SIM card from a wireless carrier in order to operate.

As mentioned in earlier sections of this SMS tutorial, computers use AT commands to control modems. Both GSM modems and dial-up modems support a common set of standard AT commands. You can use a GSM modem just like a dial-up modem.

In addition to the standard AT commands, GSM modems support an extended set of AT commands. These extended AT commands are defined in the GSM standards. With the extended AT commands, you can do things like:

Reading, writing and deleting SMS messages. Sending SMS messages.

Monitoring the signal strength.

Monitoring the charging status and charge level of the battery.

Reading, writing and searching phone book entries.

The number of SMS messages that can be processed by a GSM modem per minute is very low -- only about six to ten SMS messages per minute.

12.2. What is a GPRS Modem?

A GPRS modem is a GSM modem that additionally supports the GPRS technology for data transmission. GPRS stands for General Packet Radio Service. It is a packet-switched technology that is an extension of GSM. (GSM is a circuit-switched technology.) A key advantage of GPRS over GSM is that GPRS has a higher data transmission speed.

GPRS can be used as the bearer of SMS. If SMS over GPRS is used, an SMS transmission speed of about 30 SMS messages per minute may be achieved. This is much faster than using the ordinary SMS over GSM, whose SMS transmission speed is about 6 to 10 SMS messages per minute. A GPRS modem is needed to send and receive SMS over GPRS. Note that some wireless carriers do not support the sending and receiving of SMS over GPRS.

If you need to send or receive MMS messages, a GPRS modem is typically needed.

12.3. Which is Better: Mobile Phone or GSM / GPRS Modem?

In general, a GSM/GPRS modem is recommended for use with a computer to send and receive messages. This is because some mobile phones have certain limitations comparing to GSM/GPRS modems. Some of the limitations are described below:

Some mobile phone models (example: Ericsson R380) cannot be used with a computer to receive concatenated SMS messages.

What is a concatenated SMS message?A concatenated SMS message is a message that contains more than 140 bytes. (A normal SMS message can only contain at most 140 bytes.) Concatenated SMS works like this: the sender's mobile device breaks a message longer than 140 bytes into smaller parts. Each of these parts are then fitted in a single SMS message and sent to the recipient. When these SMS messages reach the destination, the recipient's mobile device will combine them back to one message.

What is the cause of the problem?When the mobile phone receives the SMS messages that are parts of a concatenated SMS message, it combines them to one message automatically. The correct behavior should be: when the mobile phone receives the SMS messages that are parts of a concatenated SMS message, it forwards them to the computer without combining them.

Many mobile phone models cannot be used with a computer to receive MMS messages. Because when they receive a MMS notification, they handle it automatically instead of forwarding it to the computer.

A mobile phone may not support some AT commands, command parameters and parameter values. For example, some mobile phones do not support the sending and receiving of SMS messages in text mode. So, the AT command "AT+CMGF=1" (it instructs the mobile phone to use text mode) will cause an error message to be returned. Usually GSM/GPRS modems support a more complete set of AT commands than mobile phones.

Most SMS messaging applications have to be available 24 hours a day. (For example, an SMS messaging application that provides ringtone downloading service should be running all the time so that a user can download ringtones any time he/she wants.) If such SMS messaging applications use mobile phones to send and receive SMS messages, the mobile phones have to be switched on all the time. However, some mobile phone models cannot operate with the battery removed even when an AC adaptor is connected, which means the battery will be charged 24 hours a day.

Besides the above issues, mobile phones and GSM/GPRS modems are more or less the same for sending and receiving SMS messages from a computer. Actually, you can consider an AT-command-enabled mobile phone as "GSM/GPRS modem + keypad + display + ...".

There is not much difference between mobile phones and GSM/GPRS modems in terms of SMS transmission rate, since the determining factor for the SMS transmission rate is the wireless network.

13. How to Use Microsoft HyperTerminal to Send AT Commands to a Mobile Phone or GSM/GPRS Modem?

13.1. What is Microsoft HyperTerminal?

Microsoft HyperTerminal is a small program that comes with Microsoft Windows. You can use it to send AT commands to your mobile phone or GSM/GPRS modem. It can be found at Start -> Programs ->Accessories -> Communications -> HyperTerminal. If you cannot find it and you are using Windows 98, then probably you have not installed it. You can go to Control Panel -> Add/Remove Programs ->Windows Setup tab -> Communications list box item -> Details button to install MS HyperTerminal.

Before you start programming your SMS application, you may want to check if your mobile phone, GSM/GPRS modem and SIM card are working properly first. The MS HyperTerminal is a handy tool when it comes to testing your GSM devices. It is a good idea to test your GSM devices beforehand. When a problem occurs, sometimes it is difficult to tell what causes the problem. The cause can be your program, the GSM device or the SIM card. If you test your GSM device and SIM card with MS HyperTerminal and they operate properly, then it is very likely that the problem is caused by your program.

For Linux users, minicom can be used instead of HyperTerminal.

13.2. The Procedure for Sending AT Commands to a Mobile Phone or GSM/GPRS Modem Using MS HyperTerminal

To use MS HyperTerminal to send AT commands to your mobile phone or GSM/GPRS modem, you can follow the procedure below:

1. Put a valid SIM card into the mobile phone or GSM/GPRS modem. You can obtain a SIM card by subscribing to the GSM service of a wireless network operator.

2. Connect your mobile phone or GSM/GPRS modem to a computer and set up the corresponding wireless modem driver. You should find the wireless modem driver in the CD or disk that was provided by the manufacturer. If the manufacturer does not provide such CD or disk with your mobile phone or GSM/GPRS modem, you can go to the manufacturer's web site and see whether the wireless modem driver can be downloaded there. If the wireless modem driver cannot be found on the web site, you can still use Windows' standard modem driver.

3. Run MS HyperTerminal by selecting Start -> Programs -> Accessories -> Communications ->HyperTerminal.

4. In the Connection Description dialog box, enter a name and choose an icon you like for the connection. Then click the OK button.

Figure 5. The screenshot of MS HyperTerminal's Connection Description dialog box in Windows 98.

Figure 5. The screenshot of MS HyperTerminal's Connection Description dialog box in Windows 98.

5. In the Connect To dialog box, choose the COM port that your mobile phone or GSM/GPRS modem is connecting to in the Connect using combo box. For example, choose COM1 if your mobile phone or GSM/GPRS modem is connecting to the COM1 port. Then click the OK button.

(Sometimes there will have more than one COM port in the Connect using combo box. To know which COM port is used by your mobile phone or GSM/GPRS modem, follow the procedure below:

In Windows 98:Go to Control Panel -> Modem. Then click the Diagnostics tab. In the list box, you can see which COM port the mobile phone or GSM/GPRS modem is connected to.

In Windows 2000 and Windows XP:Go to Control Panel -> Phone and Modem Options. Then click the Modems tab. In the list box, you can see which COM port the mobile phone or GSM/GPRS modem is connected to.)

Figure 6. The screenshot of MS HyperTerminal's Connect To dialog box in Windows 98.

6. The Properties dialog box comes out. Enter the correct port settings for your mobile phone or GSM/GPRS modem. Then click the OK button.

(To find the correct port settings that should be used with your mobile phone or GSM/GPRS modem, one way is to consult the manual of your mobile phone or GSM/GPRS modem. Another way is to check the port settings used by the wireless modem driver that you installed earlier.

To check the port settings used by the wireless modem driver on Windows 98, follow these steps:a. Go to Control Panel -> Modem.b. Select your mobile phone or GSM/GPRS modem in the list box.c. Click the Properties button.d. The Properties dialog box appears. The Maximum speeds field on the General tab corresponds to HyperTerminal's Bits per second field. Click the Connection tab and you can find the settings for data bits, parity and stop bits. Click the Advanced button and you can find the setting for flow control.

To check the port settings used by the wireless modem driver on Windows 2000 and Windows XP, follow these steps:a. Go to Control Panel -> Phone and Modem Options -> Modems tab.b. Select your mobile phone or GSM/GPRS modem in the list box.c. Click the Properties button.d. The Properties dialog box appears. Click the Advanced tab and then click the Change Default Preferences button.e. The Change Default Preferences dialog box appears. The Port speed field on the General tab corresponds to HyperTerminal's Bits per second field. You can also find the setting for flow control on the General tab. On the Advanced tab, you can find the settings for data bits, parity and stop bits.)

Figure 7. The screenshot of MS HyperTerminal's Properties dialog box in Windows 98.

7. Type "AT" in the main window. A response "OK" should be returned from the mobile phone or GSM/GPRS modem.Type "AT+CPIN?" in the main window. The AT command "AT+CPIN?" is used to query whether the mobile phone or GSM/GPRS modem is waiting for a PIN (personal identification number, i.e. password). If the response is "+CPIN: READY", it means the SIM card does not require a PIN and it is ready for use. If your SIM card requires a PIN, you need to set the PIN with the AT command "AT+CPIN=<PIN>".

Figure 8. The screenshot of MS HyperTerminal's main window in Windows 98.

If you get the responses above, your mobile phone or GSM/GPRS modem is working properly. You can start typing your own AT commands to control the mobile phone or GSM/GPRS modem.

Further details about how to use AT commands to send and receive SMS messages will be provided in the following sections.

14. Introduction to AT Commands

AT commands are instructions used to control a modem. AT is the abbreviation of ATtention. Every command line starts with "AT" or "at". That's why modem commands are called AT commands. Many of the commands that are used to control wired dial-up modems, such as ATD (Dial), ATA (Answer), ATH (Hook control) and ATO (Return to online data state), are also supported by GSM/GPRS modems and mobile phones. Besides this common AT command set, GSM/GPRS modems and mobile phones support an AT command set that is specific to the GSM technology, which includes SMS-related commands like AT+CMGS (Send SMS message), AT+CMSS (Send SMS message from storage), AT+CMGL (List SMS messages) and AT+CMGR (Read SMS messages).

Note that the starting "AT" is the prefix that informs the modem about the start of a command line. It is not part of the AT command name. For example, D is the actual AT command name in ATD and +CMGS is the actual AT command name in AT+CMGS. However, some books and web sites use them interchangeably as the name of an AT command.

Here are some of the tasks that can be done using AT commands with a GSM/GPRS modem or mobile phone:

Get basic information about the mobile phone or GSM/GPRS modem. For example, name of manufacturer (AT+CGMI), model number (AT+CGMM), IMEI number (International Mobile Equipment Identity) (AT+CGSN) and software version (AT+CGMR).

Get basic information about the subscriber. For example, MSISDN (AT+CNUM) and IMSI number (International Mobile Subscriber Identity) (AT+CIMI).

Get the current status of the mobile phone or GSM/GPRS modem. For example, mobile phone activity status (AT+CPAS), mobile network registration status (AT+CREG), radio signal strength (AT+CSQ), battery charge level and battery charging status (AT+CBC).

Establish a data connection or voice connection to a remote modem (ATD, ATA, etc).

Send and receive fax (ATD, ATA, AT+F*).

Send (AT+CMGS, AT+CMSS), read (AT+CMGR, AT+CMGL), write (AT+CMGW) or delete (AT+CMGD) SMS messages and obtain notifications of newly received SMS messages (AT+CNMI).

Read (AT+CPBR), write (AT+CPBW) or search (AT+CPBF) phonebook entries.

Perform security-related tasks, such as opening or closing facility locks (AT+CLCK), checking whether a facility is locked (AT+CLCK) and changing passwords (AT+CPWD).(Facility lock examples: SIM lock [a password must be given to the SIM card every time the mobile phone is switched on] and PH-SIM lock [a certain SIM card is associated with the mobile phone. To use other SIM cards with the mobile phone, a password must be entered.])

Control the presentation of result codes / error messages of AT commands. For example, you can control whether to enable certain error messages (AT+CMEE) and whether error messages should be displayed in numeric format or verbose format (AT+CMEE=1 or AT+CMEE=2).

Get or change the configurations of the mobile phone or GSM/GPRS modem. For example, change the GSM network (AT+COPS), bearer service type (AT+CBST), radio

link protocol parameters (AT+CRLP), SMS center address (AT+CSCA) and storage of SMS messages (AT+CPMS).

Save and restore configurations of the mobile phone or GSM/GPRS modem. For example, save (AT+CSAS) and restore (AT+CRES) settings related to SMS messaging such as the SMS center address.

Note that mobile phone manufacturers usually do not implement all AT commands, command parameters and parameter values in their mobile phones. Also, the behavior of the implemented AT commands may be different from that defined in the standard. In general, GSM/GPRS modems designed for wireless applications have better support of AT commands than ordinary mobile phones.

In addition, some AT commands require the support of mobile network operators. For example, SMS over GPRS can be enabled on some GPRS mobile phones and GPRS modems with the +CGSMS command (command name in text: Select Service for MO SMS Messages). But if the mobile network operator does not support the transmission of SMS over GPRS, you cannot use this feature.

14.1. Basic Commands and Extended Commands

There are two types of AT commands: basic commands and extended commands.

Basic commands are AT commands that do not start with "+". For example, D (Dial), A (Answer), H (Hook control) and O (Return to online data state) are basic commands.

Extended commands are AT commands that start with "+". All GSM AT commands are extended commands. For example, +CMGS (Send SMS message), +CMSS (Send SMS message from storage), +CMGL (List SMS messages) and +CMGR (Read SMS messages) are extended commands.

15. General Syntax of Extended AT CommandsThe general syntax of extended AT commands is straightforward. The syntax rules are provided below. The syntax of basic AT commands is slightly different. We will not cover the syntax of basic AT commands in this SMS tutorial since all SMS messaging commands are extended AT commands.

Syntax rule 1. All command lines must start with "AT" and end with a carriage return character. (We will use <CR> to represent a carriage return character in this SMS tutorial.) In a terminal program like HyperTerminal of Microsoft Windows, you can press the Enter key on the keyboard to output a carriage return character.

Example: To list all unread inbound SMS messages stored in the message storage area, type "AT", then the extended AT command "+CMGL", and finally a carriage return character, like this:

AT+CMGL<CR>

Syntax rule 2. A command line can contain more than one AT command. Only the first AT command should be prefixed with "AT". AT commands in the same command-line string should be separated with semicolons.

Example: To list all unread inbound SMS messages stored in the message storage area and obtain the manufacturer name of the mobile device, type "AT", then the extended AT command "+CMGL", followed by a semicolon and the next extended AT command "+CGMI":

AT+CMGL;+CGMI<CR>

An error will occur if both AT commands are prefixed with "AT", like this:

AT+CMGL;AT+CGMI<CR>

Syntax rule 3. A string is enclosed between double quotes.

Example: To read all SMS messages from message storage in SMS text mode (at this time you do not need to know what SMS text mode is. More information will be provided later in this SMS tutorial), you need to assign the string "ALL" to the extended AT command +CMGL, like this:

AT+CMGL="ALL"<CR>

Syntax rule 4. Information responses and result codes (including both final result codes and unsolicited result codes) always start and end with a carriage return character and a linefeed character.

Example: After sending the command line "AT+CGMI<CR>" to the mobile device, the mobile device should return a response similar to this:

<CR><LF>Nokia<CR><LF><CR><LF>OK<CR><LF>

The first line is the information response of the AT command +CGMI and the second line is the final result code. <CR> and <LF> represent a carriage return character and a linefeed character respectively. The final result code "OK" marks the end of the response. It indicates no more data will be sent from the mobile device to the computer / PC.

When a terminal program such as HyperTerminal of Microsoft Windows sees a carriage return character, it moves the cursor to the beginning of the current line. When it sees a linefeed character, it moves the cursor to the same position on the next line. Hence, the command line "AT+CGMI<CR>" that you entered and the corresponding response will be displayed like this in a terminal program such as HyperTerminal of Microsoft Windows:

AT+CGMINokia

OK

15.1. Information Response and Final Result Code

Don't forget the meanings of information response and final result code stated above, since you will see these two terms frequently as you go through this SMS tutorial.

AT+CGMI  <-- Command line enteredNokia  <-- Information response

OK  <-- Final result code

15.2. Case Sensitivity of AT Commands

In the SMS specification, all AT commands are in uppercase letters. However, many GSM/GPRS modems and mobile phones allow you to type AT commands in either uppercase or lowercase letters. For example, on Nokia 6021, AT commands are case-insensitive and the following two command lines are equivalent:

AT+CMGL<CR>

at+cmgl<CR>

16. Result Codes of AT CommandsResult codes are messages sent from the GSM/GPRS modem or mobile phone to provide you information about the execution of an AT command and the occurrence of an event. Two types of result codes are useful to you when dealing with AT commands for SMS messaging:

Final result codes

Unsolicited result codes

16.1. Final Result Codes of AT Commands

A final result code marks the end of an AT command response. It is an indication that the GSM/GPRS modem or mobile phone has finished the execution of a command line. Two frequently used final result codes are OK and ERROR. Only one final result code will be returned for each command line. Thus, you will not see both OK and ERROR in the response of a command line.

16.1.1. The OK Final Result Code

The OK final result code indicates that a command line has been executed successfully by the GSM/GPRS modem or mobile phone. It always starts and ends with a carriage return character and a linefeed character.

Here is an example for illustration. Let's say you send the command line "AT+CMGL;+CGMI<CR>" to your GSM/GPRS modem. The AT command "+CMGL" is used to list SMS messages stored in the message storage area and the AT command "+CGMI" is used to get the manufacturer name of the GSM/GPRS modem. If everything works properly without any errors, the command line, together with the response returned, should be something similar to this:

AT+CMGL;+CGMI<CR><CR><LF>+CMGL: 1,"REC UNREAD","+85291234567",,"06/11/11,00:30:29+32"<CR><LF>Welcome to our SMS tutorial.<CR><LF><CR><LF>Nokia<CR><LF><CR><LF>OK<CR><LF>

As mentioned earlier, when a terminal program such as HyperTerminal of Microsoft Windows sees a carriage return character, it moves the cursor to the beginning of the current line. When it sees a linefeed character, it moves the cursor to the same position on the next line. Hence, the command line you entered, together with the response returned, will be displayed like this in a terminal program such as HyperTerminal of Microsoft Windows:

AT+CMGL;+CGMI+CMGL: 1,"REC UNREAD","+85291234567",,"06/11/11,00:30:29+32"Welcome to our SMS tutorial.

Nokia

OK

16.1.2. The ERROR Final Result Code

The ERROR final result code indicates that an error occurs when the GSM/GPRS modem or mobile phone tries to execute a command line. After the occurrence of an error, the GSM/GPRS modem or mobile phone will not process the remaining AT commands in the command-line string.

Below are some common causes of error:

The syntax of the command line is incorrect. The value specified to a certain parameter is invalid.

The name of the AT command is spelt incorrectly.

The GSM/GPRS modem or mobile phone does not support one or more of the AT commands, command parameters or parameter values in the command-line string.

Like the OK final result code, the ERROR final result code always starts and ends with a carriage return character and a linefeed character.

Here is an example for illustration. Suppose you want to instruct your GSM/GPRS modem to list SMS messages from the message storage area and get the manufacturer name of the GSM/GPRS modem. You intend to type the command line "AT+CMGL;+CGMI<CR>" but make a careless mistake by typing "+CMFL" instead of "+CMGL". The GSM/GPRS modem will return the ERROR final result code, as shown below:

AT+CMFL;+CGMI<CR><CR><LF>ERROR<CR><LF>

As an error occurs when the GSM/GPRS modem processes "+CMFL", the GSM/GPRS modem stops the execution of the command line and so the second AT command "+CGMI" is not processed.

If you type the second AT command "+CGMI" incorrectly instead of the first AT command "+CMGL", the GSM/GPRS modem will output the result of the execution of the AT command "+CMGL" before outputting the ERROR final result code, like this:

AT+CMGL;+CGMU<CR><CR><LF>+CMGL: 1,"REC UNREAD","+85291234567",,"06/11/11,00:30:29+32"<CR><LF>Welcome to our SMS tutorial.<CR><LF><CR><LF>ERROR<CR><LF>

As mentioned earlier, when a terminal program such as HyperTerminal of Microsoft Windows sees a carriage return character, it moves the cursor to the beginning of the current line. When it sees a linefeed character, it moves the cursor to the same position on the next line. Hence, the command line you entered, together with the response returned, will be displayed like this in a terminal program such as HyperTerminal of Microsoft Windows:

AT+CMGL;+CGMU+CMGL: 1,"REC UNREAD","+85291234567",,"06/11/11,00:30:29+32"Welcome to our SMS tutorial.

ERROR

16.2. Final Result Code Specific to SMS AT Commands

The final result codes OK and ERROR are available to all AT commands. Unlike OK and ERROR, the +CMS ERROR final result code is only available to SMS AT commands. It notifies you about the occurrence of a message service failure.

16.2.1. The +CMS ERROR Final Result Code -- Notifies the Occurrences and Causes of Message Service Failures

The +CMS ERROR final result code is returned when a message service failure occurs. An error code is provided for programmers to check what causes the message service failure. The +CMS ERROR final result code is specific to SMS AT commands, i.e. the +CMS ERROR final result code will only be outputted by AT commands that are used to perform tasks related to SMS messaging. Below are the SMS AT commands that may output the final result code +CMS ERROR:

+CMGC (command name in text: Send Command) +CMGD (command name in text: Delete Message)

+CMGL (command name in text: List Messages)

+CMGR (command name in text: Read Message)

+CMGS (command name in text: Sending Message)

+CMGW (command name in text: Write Message to Memory)

+CMSS (command name in text: Send Message from Storage)

+CNMA (command name in text: New Message Acknowledgement to ME/TA)

+CNMI (command name in text: New Message Indications to TE)

+CPMS (command name in text: Preferred Message Storage)

+CRES (command name in text: Restore Settings)

+CSAS (command name in text: Save Settings)

+CSMS (command name in text: Select Message Service)

The syntax of the +CMS ERROR final result code is:

<CR><LF>+CMS ERROR: error_code<CR><LF>

Just as the final result codes OK and ERROR, the +CMS ERROR final result code always starts and ends with a carriage return character and a linefeed character. error_code is an integer that is associated to a certain error. A list of some error codes and their meanings can be found in "Table of +CMS Error Codes and Their Meanings".

As mentioned earlier, after the execution of a command line, only one final result code is returned. Hence, when an error occurs, you will not find both +CMS ERROR and ERROR in the command response. For errors related to SMS messaging, the +CMS ERROR final result code is returned. For other errors such as invalid command syntax and unsupported AT command, the ERROR final result code is returned as usual.

Below shows some common causes of +CMS errors:

A SIM card is not present in the GSM/GPRS modem or mobile phone. The SIM card requires a password (e.g. PIN, PIN2, PUK and PUK2) but you have not

entered it.

An invalid memory index is assigned to an AT command.

The memory of the GSM/GPRS modem, mobile phone or SIM card for storing SMS messages is full.

The SMSC address is unknown or incorrect.

Following is an example that demonstrates the usage of the +CMS ERROR result code. Let's say there is only one SMS text message stored on our Nokia 6021 and it is stored in the memory location at index 1. If we enter the command line "AT+CMGR=11" (it means "to read the SMS message at memory index 11"), Nokia 6021 will return a +CMS error:

AT+CMGR=11<CR><CR><LF>+CMS ERROR: 321<CR><LF>

As mentioned earlier, when a terminal program such as HyperTerminal of Microsoft Windows sees a carriage return character, it moves the cursor to the beginning of the current line. When it sees a linefeed character, it moves the cursor to the same position on the next line. Hence, the command line you entered, together with the response returned, will be displayed like this in a terminal program such as HyperTerminal of Microsoft Windows:

AT+CMGR=11+CMS ERROR: 321

To find out the meaning of the +CMS error code 321, go to "Table of +CMS Error Codes and Their Meanings". From there, we know that the read message operation failed because an invalid memory index was assigned to the AT command +CMGR.

Note that after the occurrence of a +CMS error, the GSM/GPRS modem or mobile phone will not process the remaining AT commands in the command line. Thus, if the command line sent to Nokia 6021 is "AT+CMGR=11;+CGMI" (+CGMI is the AT command for retrieving the manufacturer name of the GSM/GPRS modem or mobile phone), you will get the following result in Windows' HyperTerminal:

AT+CMGR=11;+CGMI+CMS ERROR: 321

But if the positions of the two AT commands in the command line are exchanged, Nokia 6021 will output the result of the execution of the AT command +CGMI before outputting the +CMS ERROR result code. Below is the result displayed in Windows' HyperTerminal:

AT+CGMI;+CMGR=11Nokia

+CMS ERROR: 321

16.2.1.1. Table of +CMS Error Codes and Their Meanings

The following table lists some of the +CMS error codes and their meanings.

+CMS error code Meaning

300 Mobile equipment (ME) failure. Mobile equipment refers to the mobile device that communicates with the wireless network. Usually it is a mobile phone or GSM/GPRS modem. The SIM card is defined as a separate entity and is not part of mobile equipment.

301 SMS service of mobile equipment (ME) is reserved. See +CMS error code 300 for the meaning of mobile equipment.

302 The operation to be done by the AT command is not allowed.

303 The operation to be done by the AT command is not supported.

304 One or more parameter values assigned to the AT command are invalid. (For PDU mode)

305 One or more parameter values assigned to the AT command are invalid. (For Text mode)

310 There is no SIM card.

311 The SIM card requires a PIN to operate. The AT command +CPIN (command name in text: Enter PIN) can be used to send the PIN to the SIM card.

312 The SIM card requires a PH-SIM PIN to operate. The AT command +CPIN (command name in text: Enter PIN) can be used to send the PH-SIM PIN to the SIM card.

313 SIM card failure.

314 The SIM card is busy.

315 The SIM card is wrong.

316 The SIM card requires a PUK to operate. The AT command +CPIN (command name in text: Enter PIN) can be used to send the PUK to the SIM card.

+CMS error code Meaning

320 Memory/message storage failure.

321 The memory/message storage index assigned to the AT command is invalid.

322 The memory/message storage is out of space.

330 The SMS center (SMSC) address is unknown.

331 No network service is available.

332 Network timeout occurred.

340 There is no need to send message acknowledgement by the AT command +CNMA (command name in text: New Message Acknowledgement to ME/TA).

500 An unknown error occurred.

16.3. Unsolicited Result Codes of AT Commands

Unsolicited result codes are messages sent from the GSM/GPRS modem or mobile phone to provide you information about the occurrence of an event. For example, you can use the +CNMI AT command (command name in text: New Message Indications to TE) to request the GSM/GPRS modem or mobile phone to send the unsolicited result code "+CMTI" to your computer / PC every time a new SMS message is received from the SMS center.

Here are the unsolicited result codes that are related to SMS messaging:

+CDS

A GSM/GPRS modem or mobile phone uses +CDS to forward a newly received SMS status report to the computer / PC.

+CDSI

A GSM/GPRS modem or mobile phone uses +CDSI to notify the computer / PC that a new SMS status report has been received and the memory location where it is stored.

+CMT

A GSM/GPRS modem or mobile phone uses +CMT to forward a newly received SMS message to the computer / PC.

+CMTI

A GSM/GPRS modem or mobile phone uses +CMTI to notify the computer / PC that a new SMS message has been received and the memory location where it is stored.

17. AT Command Operations: Test, Set, Read and ExecutionThere are four types of AT command operations:

Test operation. A test operation is used to check whether a certain AT command is supported by the GSM/GPRS modem or mobile phone.

Set operation. A set operation is used to change the settings used by the GSM/GPRS modem or mobile phone for certain tasks.

Read operation. A read operation is used to retrieve the current settings used by the GSM/GPRS modem or mobile phone for certain tasks.

Execution operation. An execution operation is used to perform an action or retrieve information/status about the GSM/GPRS modem or mobile phone.

The command syntax for performing an operation will be described in detail in the following sections.

17.1. Test Command -- Checks Whether a Certain AT Command is Supported

A test operation is used to check whether a certain AT command is supported by the GSM/GPRS modem or mobile phone. All extended AT commands support the test operation. The syntax is:

command=?

where command is an AT command. When an AT command is used in the above syntax to perform a test operation, it is called a test command.

Here is an example. The AT command +CGMI (command name in text: Request Manufacturer Identification) is used to get the manufacturer name of the GSM/GPRS modem or mobile phone. To test whether +CGMI is supported, you can make use of the test command "+CGMI=?". The complete command line that should be entered is:

AT+CGMI=?

If the GSM/GPRS modem or mobile phone supports the AT command +CGMI, the result code "OK" will be returned, like this:

AT+CGMI=?OK

If the GSM/GPRS modem or mobile phone does not support the AT command +CGMI, the result code "ERROR" will be returned, like this:

AT+CGMI=?ERROR

In the above example, the AT command +CGMI does not have any parameters. If the AT command to be tested has parameter(s), the parameter value(s) supported by the GSM/GPRS modem or mobile phone may be printed additionally. Below is an example that illustrates the format of the response. +COMMAND1 is a fictitious AT command that has four parameters.

AT+COMMAND1=?+COMMAND1: (0,1),(0-10),(0,1,5-10),("GSM","UCS2")

OK

The supported values of each of the four parameters are enclosed in parentheses. Commas are used to delimit the parentheses and the values inside parentheses. A hyphen is used to indicate a range of values. The values inside parentheses can be of the string type.

In the above example, the response of the test command "+COMMAND1=?" provides us the following information:

(0,1). The first parameter accepts either 0 or 1. (0-10). The second parameter accepts any integer between 0 and 10.

(0,1,5-10). The third parameter accepts 0, 1 or any integer between 5 and 10.

("GSM","UCS2"). The fourth parameter accepts either the string "GSM" or "UCS2".

To a few AT commands, the test operation does not return the parameter values supported. Instead, it returns the values that are allowed to appear in the information response of the AT command. An example is the +CBC AT command (command name in text: Battery Charge). The +CBC command is used to retrieve the connection status and charge level of the battery of the mobile device. Two values are returned in the information response of the +CBC AT command. The format is:

+CBC: connection_status,charge_level

For example, if the battery is placed in the mobile device with no charger connected and the charge level is 80%, the result of the execution of the +CBC AT command will be:

AT+CBC+CBC: 0,80

OK

If you run the test command "+CBC=?", all the supported values that are allowed to appear in the connection status field and charge level field will be provided. With my Nokia 6021, the result is:

AT+CBC=?+CBC: (0,1),(0-100)

OK

"(0,1)" means the connection status field in the information response of the +CBC AT command can contain either 0 or 1, while "(0-100)" means the charge level field can contain any integer between 0 and 100.

17.2. Set Command -- Changes the Settings Used for Certain Tasks

A set operation changes the settings used by the GSM/GPRS modem or mobile phone for certain tasks. The syntax is:

command=value1,value2,...valueN

where command is an AT command and value1 to valueN are the values you want to set. When an AT command is used in the above syntax to perform a set operation, it is called a set command.

Here is an example. The AT command +CSCA (command name in text: Service Centre Address) is used to set the SMSC (SMS center) address for sending SMS messages. It takes two parameters that specify the SMSC address and type of address. To set the SMSC address to +85291234567, enter the following command line in a terminal program such as MS Windows' HyperTerminal:

AT+CSCA="+85291234567",145

If the set command runs successfully, the result code "OK" will be returned:

AT+CSCA="+85291234567",145OK

Some AT commands have optional parameters. You can choose not to assign values to them. For example, the second parameter of the +CSCA AT command is optional. If no value is assigned to the second parameter, the GSM/GPRS modem or mobile phone will use the default parameter value, which is 145 if the SMSC address starts with "+" (the plus character). Hence, this command line:

AT+CSCA="+85291234567"

is equivalent to:

AT+CSCA="+85291234567",145

Typically the values you specified with set commands are placed in volatile memory. If the GSM/GPRS modem or mobile phone is switched off or rebooted, the values you specified with set commands will be gone. When the GSM/GPRS modem or mobile phone is powered on again, all settings are back to the defaults.

For some commonly used settings, there are AT commands for saving/restoring the settings to/from non-volatile memory. For example, the AT commands +CSAS (command name in text: Save Settings) and +CRES (command name in text: Restore Settings) can be used to save and restore settings related to SMS messaging such as the SMS center address.

17.3. Read Command -- Retrieves the Current Settings Used for Certain Tasks

A read operation retrieves the current settings used by the GSM/GPRS modem or mobile phone for certain tasks. The syntax is:

command?

where command is an AT command. When an AT command is used in the above syntax to perform a read operation, it is called a read command. The read operation is supported by all AT commands that are capable of the set operation.

Here is an example that illustrates how to use a read command. The AT command +CSCA (command name in text: Service Centre Address) is used to set the SMSC (SMS center) address for sending SMS messages. It takes two parameters that specify the SMSC address and type of address. Suppose you set the SMSC address to +85291234567 in Microsoft HyperTerminal, like this:

AT+CSCA="+85291234567",145OK

After that, if you enter the read command "+CSCA?", the GSM/GPRS modem or mobile phone will return the SMSC address and type of address that you set in the previous step:

AT+CSCA?+CSCA: "+85291234567",145

OK

17.4. Execution Command -- Performs an Action or Retrieve Information/Status about the GSM/GPRS Modem or Mobile Phone

An execution operation is used to perform an action (for example, send or read an SMS message) or retrieve information/status about the GSM/GPRS modem or mobile phone (for example, retrieve the current battery charge level, battery charging status or radio signal strength of the mobile network). The syntax is:

command=value1,value2,...valueN

where command is an AT command and value1 to valueN are the values to assign to the AT command. If the AT command does not have any parameters, the part "=value1,value2,...valueN" should be omitted. When an AT command is used in the above syntax to perform an execution operation, it is called an execution command.

Here is an example illustrating the use of an execution command. The AT command +CMSS (command name in text: Send Message from Storage) can be used to perform an execution operation to send an SMS message stored in message storage. It has three parameters. They specify the index of the memory location that stores the SMS message, the destination phone number and the type of the phone number respectively. To send the SMS message at index 1 to the phone number +85291234567, the following command line can be used:

AT+CMSS=1,"+85291234567",145

Some AT commands have optional parameters. You can choose not to assign values to them. For example, the third parameter of the +CMSS AT command is optional. If no value is assigned to the third parameter, the GSM/GPRS modem or mobile phone will use the default parameter value, which is 145 if the destination phone number starts with "+" (the plus character). Hence, this command line:

AT+CMSS=1,"+85291234567"

is equivalent to:

AT+CMSS=1,"+85291234567",145

Unlike set commands, execution commands do not store the parameter values assigned to them. So, no read command is available for retrieving the last parameter values assigned to an execution command. For example, if you send the command line "AT+CMSS?" to your GSM/GPRS modem or mobile phone, the ERROR result code will be returned:

AT+CMSS?ERROR

18. Testing the Communication between the PC and GSM/GPRS Modem or Mobile PhoneSuppose you have connected your GSM/GPRS modem or mobile phone to your PC / computer and started a terminal program (such as HyperTerminal on Microsoft Windows). Now you are ready to enter your first command. The first thing that is usually done is to test the communication between the PC and GSM/GPRS modem/mobile phone to confirm that everything is working properly so far. Simply enter "AT" in the terminal program to perform the test. When the GSM/GPRS modem or mobile phone receives "AT", it will send back the final result code "OK" to indicate that it has received your command successfully, like this:

ATOK

19. Checking if the GSM/GPRS Modem or Mobile Phone Supports the Use of AT Commands to Send, Receive and Read SMS MessagesAfter testing the communication between the PC and GSM/GPRS modem/mobile phone, the next thing that you may want to do is to check if the GSM/GPRS modem or mobile phone supports the use of AT commands to send, receive and read SMS messages. Most GSM/GPRS modems support all three functions. However, only some mobile phones support all of them.

Sending SMS Messages

To find out whether a GSM/GPRS modem or mobile phone supports the sending of SMS messages through AT commands, you have to:

1. Use the AT command +CSMS (command name in text: Select Message Service) to check whether mobile-originated SMS messages are supported.

2. Perform test operations to check whether +CMGS (command name in text: Send Message) and/or +CMSS (command name in text: Send Message from Storage) are supported.(You may want to check the AT commands +CMGW [command name in text: Write Message to Memory] and +CMGD [command name in text: Delete Message] in addition as they are sometimes used together with +CMSS.)

Receiving SMS Messages and Reading SMS Messages from Message Storage

To find out whether a GSM/GPRS modem or mobile phone supports the receiving and reading of SMS messages through AT commands, you have to:

1. Use the AT command +CSMS (command name in text: Select Message Service) to check whether mobile-terminated SMS messages are supported.

2. Perform test operations to check whether +CNMI (command name in text: New Message Indications to TE), +CMGL (command name in text: List Messages) and/or +CMGR (command name in text: Read Message) are supported.

If the GSM/GPRS modem or mobile phone supports the +CNMI AT command, it can send a notification or directly forward the message to the PC whenever a new SMS message arrives.

If the GSM/GPRS modem or mobile phone does not support +CNMI but supports +CMGL and/or +CMGR, the PC has to poll the GSM/GPRS modem or mobile phone repeatedly in order to know if any new SMS messages have arrived.

19.1. Using the AT Command +CSMS to Check if Mobile-originated SMS Messages and Mobile-terminated SMS Messages are Supported

One use of the AT command +CSMS (command name in text: Select Message Service) is to check the message types supported by the GSM/GPRS modem or mobile phone. There are three message types: mobile-originated SMS messages, mobile-terminated SMS messages and cell broadcast messages.

Mobile-originated SMS messages refer to SMS messages that are sent from a mobile device to an SMSC, i.e. outbound SMS messages.

Mobile-terminated SMS messages refer to SMS messages that are sent from an SMSC to a mobile device, i.e. inbound SMS messages.

Cell broadcast messages are text messages pushed to subscribers located in a certain mobile network area by the network operator. These text messages may contain news, weather information, etc.

Here is an example that demonstrates how to use the +CSMS AT command to check if mobile-originated and mobile-terminated SMS messages are supported. First, send the read command "+CSMS?" to the GSM/GPRS modem or mobile phone. The response returned from our Nokia 6021 to HyperTerminal is shown below:

AT+CSMS?+CSMS: 0,1,1,1

OK

As you can see, the information response contains four values. The second, third and fourth values indicate whether Nokia 6021 supports mobile-terminated SMS messages, mobile-originated SMS messages and cell broadcast messages respectively. If the value is 1, it means the message type is supported. If the value is 0, it means the message type is not supported.

For Nokia 6021:

The third value in the information response is 1, which indicates Nokia 6021 supports mobile-originated SMS messages. So, Nokia 6021 is capable of sending SMS messages to an SMSC.

The second value in the information response is 1, which indicates Nokia 6021 supports mobile-terminated SMS messages. So, Nokia 6021 is capable of receiving SMS messages from an SMSC.

If the final result code "ERROR" is returned (as shown below), it is likely that the +CSMS AT command is not supported by the mobile device.

AT+CSMS?ERROR

To confirm, send the test command "+CSMS=?" to the GSM/GPRS modem or mobile phone. If the final result code "ERROR" is returned (as shown below), it means the mobile device does not support the +CSMS AT command.

AT+CSMS=?ERROR

Note that +CSMS is a mandatory command in the AT command set for SMS messaging. If it is not supported, normally the whole AT command set for SMS messaging is not supported.

19.2. Checking if the AT Commands Required for Sending, Receiving and Reading SMS Messages are Supported

The next thing to do is to check if the AT commands required for sending, receiving and reading SMS messages are supported by the mobile phone or GSM/GPRS modem. As pointed out

earlier in this SMS tutorial, you can check if a certain AT command is supported by performing a test operation. Simply execute an AT command with "=?" attached at its end. For example, "AT+CMGS=?".

Before we begin the check, let's go through an overview of the AT commands that are required for sending, receiving and reading SMS messages. You will learn which AT commands should be checked and get a general idea about when these AT commands should be used.

19.2.1. Overview of the AT Commands Required

For Sending SMS Messages

To send SMS messages via AT commands, the GSM/GPRS modem or mobile phone has to support either +CMGS (command name in text: Send Message) or +CMSS (command name in text: Send Message from Storage). You may also find +CMGW (command name in text: Write Message to Memory) and +CMGD (command name in text: Delete Message) useful, since they are sometimes used together with +CMSS. +CMSS is used to send an SMS message located in the message storage area. If an SMS message does not exist in the message storage area, you must first use the AT command +CMGW to write the SMS message to the message storage area before you can use the AT command +CMSS to send the SMS message to the SMSC. After message submission, you may use the AT command +CMGD to delete the SMS message to free some space from the message storage area.

For Receiving and Reading SMS Messages

To receive and read SMS messages via AT commands, the GSM/GPRS modem or mobile phone has to support the AT command +CNMI (command name in text: New Message Indications to TE. TE stands for Terminal Equipment, which is the equipment that controls the GSM/GPRS modem or mobile phone. For example, a PC / computer), +CMGL (command name in text: List Messages), or +CMGR (command name in text: Read Messages).

The AT command +CNMI is used to specify how newly arrived SMS messages should be handled. You can tell the GSM/GPRS modem or mobile phone either to forward newly arrived SMS messages directly to the PC, or to save them in message storage and then notify the PC about their locations in message storage.

The AT command +CMGL is used to read all SMS messages that have a certain status (e.g. "received unread", "received read", etc) from the message storage area, while the AT command +CMGR is used to read the SMS message saved at a certain location of the message storage area.

19.2.2. Beginning the Check

Now that you've learned the AT commands required for outbound and inbound SMS messaging are +CMGS, +CMSS, +CNMI, +CMGL and +CMGR. You can begin the check by performing a test operation with each of the AT commands. For example, you can execute the command line "AT+CMGS=?" to check whether +CMGS is supported, like this:

AT+CMGS=?OK

The final result code "OK" indicates the AT command +CMGS is supported. If the GSM/GPRS modem or mobile phone returns the final result code "ERROR", it means the command is not supported.

A quicker way is to copy and paste the following command line to the terminal program and execute it:

AT+CMGS=?;+CMSS=?;+CNMI=?;+CMGL=?;+CMGR=?

As an example, here shows the response returned from my Nokia 6021 to HyperTerminal after the execution of above command line:

AT+CMGS=?;+CMSS=?;+CNMI=?;+CMGL=?;+CMGR=?+CNMI: (0-2),(0-3),(0,2,3),(0-2),(0,1)

+CMGL: (0-4)

OK

The return of the final result code "OK" indicates all AT commands under test are supported. If the final result code "ERROR" is returned, it means one or more AT commands are not supported.

20. Operating Mode: SMS Text Mode and SMS PDU ModeThe SMS specification has defined two modes in which a GSM/GPRS modem or mobile phone can operate. They are called SMS text mode and SMS PDU mode. (PDU stands for Protocol

Data Unit.) The mode that a GSM/GPRS modem or mobile phone is operating in determines the syntax of some SMS AT commands and the format of the responses returned after execution. Below are the SMS AT commands affected:

+CMGS (Send Message) +CMSS (Send Message from Storage)

+CMGR (Read Message)

+CMGL (List Messages)

+CMGW (Write Message to Memory)

+CNMA (New Message Acknowledgement to ME/TA)

+CMGC (Send Command)

The syntax of the unsolicited result codes below also depends on the mode in which the GSM/GPRS modem or mobile phone is operating:

+CMT (Used to forward received SMS messages to the computer / PC.) +CBM (Used to forward received cell broadcast messages to the computer / PC.)

+CDS (Used to forward received status reports to the computer / PC.)

These two AT commands are useful to you only if SMS text mode is used:

+CSMP (Set Text Mode Parameters)

+CSDH (Show Text Mode Parameters)

20.1. Comparison of SMS Text Mode and SMS PDU Mode

Below we compare SMS text mode and SMS PDU mode from various aspects. The comparison should help you learn the differences between these two modes and decide which mode should be used by your SMS messaging application.

Syntax of SMS AT Commands and Responses

When the GSM/GPRS modem or mobile phone is operating in different modes, the syntax of certain SMS AT commands and the responses returned after command execution is different. Here's an example for illustration. Let's say you would like to send the SMS message "It is easy to send text messages." to the mobile phone number +85291234567. In SMS text mode, this is the command line that you should enter:

AT+CMGS="+85291234567"<CR>It is easy to send text messages.<Ctrl+z>

However, if the GSM/GPRS modem or mobile phone is operating in SMS PDU mode, executing the above command line will cause an error to occur. This is because the syntax of the +CMGS AT command is different in SMS PDU mode. To do the same task, the following command line should be used instead:

AT+CMGS=42<CR>07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E<Ctrl+z>

Defined Values for Certain Parameters

When the GSM/GPRS modem or mobile phone is operating in different modes, the defined values for certain parameters are different. Usually string values are defined for text mode while numeric values are defined for PDU mode. For example, the +CMGL AT command is used to list SMS messages stored in message storage. It takes one parameter that specifies the status of the SMS messages to be retrieved. The following table lists the defined values for the parameter in text mode and PDU mode:

Message status Defined values in text mode Defined values in PDU mode

Received unread "REC UNREAD" 0

Received read "REC READ" 1

Stored unsent "STO UNSENT" 2

Stored sent "STO SENT" 3

All messages "ALL" 4

Suppose you would like to list all SMS messages from message storage. If the GSM/GPRS modem or mobile phone is operating in SMS text mode, you should assign the string value "ALL" to the +CMGL AT command, like this:

AT+CMGL="ALL"

In SMS PDU mode, the numeric value 4 should be assigned to the +CMGL AT command instead:

AT+CMGL=4

Input/Output Format of SMS Messages Used by SMS AT Commands

When the GSM/GPRS modem or mobile phone is operating in different modes, the input/output format of SMS messages used by SMS AT commands is different. In SMS text mode, headers and body of SMS messages are inputted/outputted as separate parameters/fields. In SMS PDU mode, TPDUs (Transport Protocol Data Units) in hexadecimal format are inputted and outputted. Headers and body of SMS messages are encoded in the TPDUs.

Here is an example for illustration. To send the SMS message "It is easy to send text messages." to the mobile phone number +85291234567, the following command line should be used in SMS text mode. As you can see below, the destination phone number header and message body are provided to the +CMGS AT command as separate parameters.

AT+CMGS="+85291234567"<CR>It is easy to send text messages.<Ctrl+z>

To send the same SMS text message in SMS PDU mode, the following command line should be used instead. The message body, destination phone number header and some other headers are encoded in the hexadecimal sequence.

AT+CMGS=42<CR>07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E<Ctrl+z>

Ease of Use

As you can see in the previous example, it is easier to use AT commands in SMS text mode. You do not have to learn about the structure of different types of TPDUs in the bit level and the encoding and decoding of the hexadecimal sequence.

Supported Features of SMS Messaging

Although it is easier to use AT commands in SMS text mode, it supports fewer features of SMS messaging than SMS PDU mode. This is because you do not have complete control over the header values and message body in SMS text mode. Some tasks, although can be done in text mode, require the programmer to have knowledge about PDU mode and TPDU. For example, to request a status report from SMSC in SMS text mode, you have to set bit 5 of the first octet of the SMS-SUBMIT TPDU to 1 by the AT command +CSMP (command name in text: Set Text Mode Parameters). Similar tasks include setting the message validity period and sending a flash SMS message that immediately pops up on the phone screen when it arrives at the destination.

Level of Support

SMS PDU mode is more commonly supported by GSM/GPRS modems and mobile phones than SMS text mode.

20.2. Selecting the Operating Mode (AT+CMGF)

The AT command +CMGF (command name in text: Message Format) is used to select the operating mode of the GSM/GPRS modem or mobile phone. It takes one parameter. The value of the parameter can either be 0 or 1. The values 0 and 1 refer to SMS PDU mode and SMS text mode respectively. SMS PDU mode is the default mode if it is implemented on the mobile device.

To find out the operating mode(s) supported by a GSM/GPRS modem or mobile phone, perform a test operation with the +CMGF AT command. Below shows the response returned from my Nokia 6021 mobile phone after the execution of the command line "AT+CMGF=?":

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

OK

The information response "+CMGF: (0,1)" indicates Nokia 6021 supports both PDU mode and text mode.

To change the operating mode of a GSM/GPRS modem or mobile phone, perform a set operation with the +CMGF AT command. The following example demonstrates how to set the operating mode to SMS text mode:

AT+CMGF=1OK

If the operating mode specified is not supported by the GSM/GPRS modem or mobile phone, the final result code "ERROR" will be returned.

To find out the operating mode currently used by a GSM/GPRS modem or mobile phone, perform a read operation with the +CMGF AT command. Here is an example:

AT+CMGF?+CMGF: 0

OK

The response above indicates the GSM/GPRS modem or mobile phone is using SMS PDU mode.

21. Setting or Reading the Service Center Address / SMSC Address (AT+CSCA)Before you send SMS messages, one important thing that should be done is to make sure the correct service center address (SMSC address) has been set properly. Typically a service center address/SMSC address is a phone number formatted using the ISDN / telephony numbering plan (ITU E.164/E.163). For example, +85291234567. The AT command +CSCA (command name in text: Service Centre Address) can be used to set or read the service center address through which SMS messages are sent. The setting will be used by the AT commands +CMGW (command name in text: Write Message to Memory) and +CMGS (command name in text: Send Message).

In SMS text mode, the only way to specify the service center address to be used by +CMGW and +CMGS is through +CSCA. However, in SMS PDU mode, it is possible to specify the service center address to the AT commands +CMGW and +CMGS directly as a parameter value.

The mobile network operator usually pre-sets the correct service center number in the default profile of settings stored in the SIM card. Most mobile phones provide some ways for the user to view or change the service center number saved in the profiles of settings. For example, on my Nokia 6021 mobile phone, the message center number saved in the default profile of settings can be viewed and changed by going to Menu -> Messages -> Message settings -> Text messages -> Sending profile -> Default profile -> Message centre number.

Some GSM/GPRS modems and mobile phones (for example, Nokia 6021, Sony Ericsson T68i, Philips 598, etc) use the service center number in the default profile of settings as the default value of the +CSCA AT command. As a result, if the service center number in the default profile of settings is correct, you do not need to use the +CSCA AT command to set the service center number.

Note

To some mobile devices such as Nokia 6021, the +CSCA AT command does not save the service center number in non-volatile memory. Hence, if you switch off the GSM/GPRS modem or mobile phone, the setting will be gone. To save the service center number set with the AT command +CSCA, you have to make use of the AT command +CSAS (command name in text: Save Settings). To restore the saved service center number, the AT command +CRES (command name in text: Restore Settings) is required.

The command behavior is different on some mobile devices such as Sony Ericsson T68i and Philips 598. Every time the AT command +CSCA is used to change the SMSC address, the new SMSC address is saved to non-volatile memory.

21.1. Syntax of the +CSCA AT Command

21.1.1. Setting the Service Center Address

To set the service center address, perform a set operation with the +CSCA AT command using the following syntax: (Optional parameters are enclosed in square brackets.)

+CSCA=address[,address_type]

The address Parameter

The first parameter of the +CSCA AT command, address, specifies the SMSC address to be set. Usually it is a phone number formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163). For example, "+85291234567". Note that the value assigned to the address parameter should be a string, which means you should use double quotes to enclose the phone number.

The address_type Parameter

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

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

145. Meaning: The SMSC 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 SMSC address does not start with a "+" character. For example, "85291234567".

145 if the SMSC address starts with a "+" character. For example, "+85291234567".

21.1.2. Reading the Service Center Address

To read the service center address, you can perform a read operation using the +CSCA read command "+CSCA?". The information response returned has the following format:

+CSCA: address,address_type

The definition of address and address_type is the same as above.

21.2. Example Demonstrating How to Use the +CSCA AT Command to Set and Read the Service Center Address

Now let's see an example that demonstrates how to use the +CSCA AT command to set and read the service center address. As said earlier, sometimes a proper service center address is in use by default and there is no need to make any changes. So first let's type the read command "+CSCA?" in a terminal program (for example, HyperTerminal in Microsoft Windows) to check if a proper service center address is in use currently. The response returned from a GSM/GPRS modem or mobile phone should be something like this:

AT+CSCA?+CSCA: "+85291111111",145

OK

If you find that the service center address is not correct, use the +CSCA AT command to change it, like this:

AT+CSCA="+85290000000",145OK

The final result code OK indicates the service center address was set successfully. If the execution of the command line fails, the final result code will be ERROR.

22. Preferred Message Storage (AT+CPMS)The AT command +CPMS (command name in text: Preferred Message Storage) serves several purposes. It can be used to:

1. Select the message storage area that will be used when sending, receiving, reading, writing or deleting SMS messages.

2. Find the number of messages that are currently stored in the message storage area.

3. Find the maximum number of messages that can be stored in the message storage area.

22.1. Selecting the Message Storage Areas to be Used for SMS Reading, Writing, Deleting, Sending or Receiving

To select the message storage areas to be used for SMS reading, writing, deleting, sending or receiving, you should perform a set operation with the +CPMS AT command. The syntax is: (Optional parameters are enclosed in square brackets.)

+CPMS=message_storage1[,message_storage2[,message_storage3]]

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+CPMS="ME","SM","MT"

The message_storage1 Parameter

The first parameter of the +CPMS AT command, message_storage1, specifies the message storage area that will be used when reading or deleting SMS messages. (For details about reading SMS messages, see AT commands +CMGR and +CMGL. For details about deleting SMS messages, see the AT command +CMGD.)

The message_storage2 Parameter

The second parameter of the +CPMS AT command, message_storage2, specifies the message storage area that will be used when sending SMS messages from message storage or writing SMS messages. (For details about sending SMS messages from message storage, see the AT command +CMSS. For details about writing SMS messages, see the AT command +CMGW.)

The message_storage3 Parameter

The third parameter of the +CPMS AT command, message_storage3, specifies the preferred message storage area for storing newly received SMS messages. If you use the +CNMI AT command (command name in text: New Message Indications to TE) to tell the GSM/GPRS modem or mobile phone to forward newly received SMS messages directly to the PC instead of storing them in the message storage area, you do not need to care about the message_storage3 parameter.

Values that May be Assigned to the Parameters of the +CPMS AT Command

Here are the values defined in the SMS specification that may be assigned to the parametersmessage_storage1, message_storage2 and message_storage3:

SM. It refers to the message storage area on the SIM card. ME. It refers to the message storage area on the GSM/GPRS modem or mobile phone.

Usually its storage space is larger than that of the message storage area on the SIM card.

MT. It refers to all message storage areas associated with the GSM/GPRS modem or mobile phone. For example, suppose a mobile phone can access two message storage areas: "SM" and "ME". The "MT" message storage area refers to the "SM" message storage area and the "ME" message storage area combined together.

BM. It refers to the broadcast message storage area. It is used to store cell broadcast messages.

SR. It refers to the status report message storage area. It is used to store status reports.

TA. It refers to the terminal adaptor message storage area.

Note that a GSM/GPRS modem or mobile phone may not support all the above values and it may support other manufacturer specific values. To learn how to find the values supported by your GSM/GPRS modem or mobile phone, please refer to the section "Finding the Message Storage Areas Supported by a GSM/GPRS Modem or Mobile Phone".

22.2. Format of the Information Response of the +CPMS AT Command

If the GSM/GPRS modem or mobile phone executes the +CPMS set command successfully, it will return an information response to the computer / PC. The information response of the +CPMS AT command has the following format:

+CPMS: used_space1,max_space1,used_space2,max_space2,used_space3,max_space3

used_space1 is the number of messages currently stored in the message storage area specified by the message_storage1 parameter.

max_space1 is the maximum number of messages that can be stored in the message storage area specified by the message_storage1 parameter.

used_space2 is the number of messages currently stored in the message storage area specified by the message_storage2 parameter.

max_space2 is the maximum number of messages that can be stored in the message storage area specified by the message_storage2 parameter.

used_space3 is the number of messages currently stored in the message storage area specified by the message_storage3 parameter.

max_space3 is the maximum number of messages that can be stored in the message storage area specified by the message_storage3 parameter.

22.3. Finding the Message Storage Areas Supported by a GSM/GPRS Modem or Mobile Phone

Before we can use the +CPMS AT command to select the message storage areas to be used for various SMS operations, we have to know what message storage areas are supported by the GSM/GPRS modem or mobile phone. Among the six types of message storage area mentioned in the last section, "SM", "ME" and "MT" are commonly supported by GSM/GPRS modems and mobile phones. To find out the message storage areas that are supported by your GSM/GPRS modem or mobile phone, you can perform a test operation with the +CPMS AT command using the command line below:

AT+CPMS=?

If the GSM/GPRS modem or mobile phone executes the above command line successfully, it will return an information response to the computer / PC. The information response of the +CPMS AT command has the following format:

+CPMS: (storage1a,storage1b,...),(storage2a,storage2b,...),(storage3a,storage3b,...)

The first, second and third parentheses contain the values supported by the message_storage1parameter, the message_storage2 parameter and the message_storage3 parameter respectively.

22.4. Example Demonstrating How to Use the +CPMS AT Command to Select the Message Storage Areas to be Used

Now let's see an example that demonstrates how to use the +CPMS AT command to select the message storage areas to be used for various SMS operations.

Finding the Message Storage Areas Supported by the GSM/GPRS Modem or Mobile Phone

First, enter the command line "AT+CPMS=?" in a terminal program (for example, Windows' HyperTerminal program) to find the message storage areas supported by the GSM/GPRS modem or mobile phone. Below shows the response returned from my Nokia 6021 to Windows' HyperTerminal program after the execution of the command line "AT+CPMS=?":

AT+CPMS=?+CPMS: ("ME","SM"),("ME","SM"),("MT")

OK

The information response above indicates Nokia 6021 supports the use of "ME" or "SM" for SMS reading, deleting, sending and writing, while it only supports the use of "MT" for storing inbound SMS messages.

Selecting Message Storage Areas

After knowing what message storage areas are selectable, we can now perform a set operation to select the message storage areas. For example, let's say you want to instruct the GSM/GPRS modem or mobile phone to use:

the phone message storage area for SMS reading and deleting, the SIM message storage area for SMS writing and sending, and

both the phone message storage area and SIM message storage area for storing inbound SMS messages.

You should assign the string values "ME", "SM" and "MT" to the +CPMS AT command. Following shows the response returned from my Nokia 6021 mobile phone to HyperTerminal:

AT+CPMS="ME","SM","MT"+CPMS: 2,150,2,10,4,160

OK

As you can see, there are six values in the information response of the +CPMS AT command. Following is a description of them.

The First and Second Fields: 2,150

The value 2 indicates there are currently two messages stored in the ME message storage area. 150 is the maximum number of messages that can be stored in the ME message storage area.

The Third and Fourth Fields: 2,10

The value 2 indicates there are currently two messages stored in the SM message storage area. 10 is the maximum number of messages that can be stored in the SM message storage area.

The Fifth and Sixth Fields: 4,160

The value of the fifth field of the +CPMS information response, 4, indicates there are currently four messages stored in the MT message storage area. The same result can also be obtained by calculating manually like this:

Number of messages currently stored in the ME message storage area + Number of messages currently stored in the SM message storage area= 2 + 2= 4

The value of the sixth field of the +CPMS information response, 160, is the maximum number of messages that can be stored in the MT message storage area. The same result can also be obtained by calculating manually like this:

Maximum number of messages that can be stored in the ME message storage area + Maximum number of messages that can be stored in the SM message storage area= 150 + 10= 160

22.5. Finding the Message Storage Areas Currently Used, Number of Messages Currently Stored and Maximum Number of Messages Allowed

To find out the message storage areas currently used, the number of messages currently stored in a certain message storage area, and the maximum number of messages that are allowed to be stored in a certain message storage area, you can perform a read operation with the +CPMS AT command using the command line below:

AT+CPMS?

If the GSM/GPRS modem or mobile phone executes the above command line successfully, it will return an information response to the computer / PC. The +CPMS information response has the following format:

+CPMS: message_storage1,used_space1,max_space1,message_storage2,used_space2,max_space2,message_storage3,used_space3,max_space3

The message_storage1 Field

The first field of the information response of the +CPMS AT command, message_storage1, contains a string that indicates the message storage area to be used when reading or deleting SMS messages. (For details about reading SMS messages, see AT commands +CMGR and +CMGL. For details about deleting SMS messages, see the AT command +CMGD.)

The used_space1 Field

The second field of the information response of the +CPMS AT command, used_space1, contains an integer that indicates the number of messages currently stored in the message storage areamessage_storage1.

The max_space1 Field

The third field of the information response of the +CPMS AT command, max_space1, contains an integer that indicates the maximum number of messages that can be stored in the message storage areamessage_storage1.

The message_storage2 Field

The fourth field of the information response of the +CPMS AT command, message_storage2, contains a string that indicates the message storage area to be used when sending SMS messages from message storage or writing SMS messages. (For details about sending SMS messages from message storage, see the AT command +CMSS. For details about writing SMS messages, see the AT command+CMGW.)

The used_space2 Field

The fifth field of the information response of the +CPMS AT command, used_space2, contains an integer that indicates the number of messages currently stored in the message storage areamessage_storage2.

The max_space2 Field

The sixth field of the information response of the +CPMS AT command, max_space2, contains an integer that indicates the maximum number of messages that can be stored in the message storage area message_storage2.

The message_storage3 Field

The seventh field of the information response of the +CPMS AT command, message_storage3, contains a string that indicates the preferred message storage area to be used for storing newly received SMS messages.

The used_space3 Field

The eighth field of the information response of the +CPMS AT command, used_space3, contains an integer that indicates the number of messages currently stored in the message storage areamessage_storage3.

The max_space3 Field

The ninth field of the information response of the +CPMS AT command, max_space3, contains an integer that indicates the maximum number of messages that can be stored in the message storage area message_storage3.

22.5.1. Example

The example below shows the response returned by my Nokia 6021 mobile phone to Windows' HyperTerminal program after the read command "+CPMS?" has been executed:

AT+CPMS?+CPMS: "ME",2,150,"SM",2,10,"MT",4,160

OK

As you can see, there are nine values in the information response of the +CPMS AT command. Following is a description of them.

The First, Second and Third Fields: "ME",2,150

The string value "ME" indicates that the phone message storage area will be used for SMS reading and deleting. The value 2 indicates that there are currently two messages stored in the phone message storage area. 150 is the maximum number of messages that can be stored in the phone message storage area.

The Fourth, Fifth and Sixth Fields: "SM",2,10

The string value "SM" indicates the SIM message storage area will be used for SMS writing and sending. The value 2 tells us that there are currently two messages stored in the SIM message storage area. 10 is the maximum number of messages that can be stored in the SIM message storage area.

The Seventh, Eighth and Ninth Fields: "MT",4,160

The string value "MT" indicates that both the phone message storage area and SIM message storage area will be used for storing inbound SMS messages. The value 4 tells us that there are currently four messages stored in the phone message storage area and SIM message storage area. 160 is the maximum number of messages that can be stored in the phone message storage area and SIM message storage area.

23. Writing SMS Messages to Memory / Message Storage (AT+CMGW)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.

23.1. 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]]]<CR>sms_message_body<Ctrl+z>

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"<CR>This is an example for illustrating the syntax of the +CMGW AT command in SMS text mode.<Ctrl+z>

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 <CR> Character

<CR>, 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 <Esc> 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 <Ctrl+z> Character

When you finish entering the SMS message body, you have to enter the <Ctrl+z> 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.

23.2. 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.

23.3. 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.

23.4. Example Demonstrating How to Use the +CMGW AT Command to Write SMS Text Messages to Message Storage in SMS Text Mode

Now let's see a more detailed example that demonstrates how to use the +CMGW AT command to write SMS text messages to message storage in SMS text mode and how the +CMGW 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=1OK

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 Write SMS Text Messages to

Second, use the AT command +CPMS (command name in text: Preferred Message Storage) to select the message storage area to write SMS text messages to. 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 +CMGW, assign the string value "SM" to the second parameter of the +CPMS AT command, like this:

AT+CPMS="ME","SM"

The first parameter of the +CPMS AT command is used to select the message storage area for reading and deleting SMS messages. It has no use to us here. So, it does not matter what value is assigned to the first parameter.

Now press the Enter key on the keyboard and you should see something similar to this:

AT+CPMS="ME","SM"+CPMS: 2,150,2,10,4,160

OK

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

Setting the SMSC Number to be Stored with the SMS Text Message

Third, use the AT command +CSCA (command name in text: Service Centre Address) to set the SMSC number to be stored with the SMS text message. Later if you send the SMS text message, it will be transmitted via the SMSC at this number. Usually the default setting is

correct and you do not need to make any changes. Below demonstrates how to use the +CSCA AT command to set "+85290000000" as the SMSC number:

AT+CSCA="+85290000000"OK

Detailed information about the +CSCA AT command can be found in the "Setting or Reading the Service Center Address / SMSC Address (AT+CSCA)" section of this SMS tutorial.

Writing Text Messages

Fourth, you can now use the +CMGW AT command to write a text message to the message storage area. Suppose you want the destination mobile phone number to be +85291234567 and the message status to be "stored unsent", you should enter something like this in the terminal program:

AT+CMGW="+85291234567",145,"STO UNSENT"

Then, press the Enter key of the keyboard to send a carriage return character to the GSM/GPRS modem or mobile phone. The GSM/GPRS modem or mobile phone will send back a prompt formed by four characters. They are the carriage return character, the linefeed character, the ">" character and the space character. If all characters are to be displayed, the characters you have sent to and received from the GSM/GPRS modem or mobile phone so far are:

AT+CMGW="+85291234567",145,"STO UNSENT"<CR><CR><LF>><Space>

However, the carriage return character and linefeed character have special meanings to a terminal program. When a terminal program sees a carriage return character, it moves the cursor to the beginning of the current line. When it sees a linefeed character, it moves the cursor to the same position on the next line. So, here is what you will actually see in a terminal program such as HyperTerminal:

AT+CMGW="+85291234567",145,"STO UNSENT"> 

If you want to cancel the write command at this point, press the Esc key on the keyboard. The GSM/GPRS modem or mobile phone will then return the OK final result code. Here shows the response returned from Nokia 6021:

AT+CMGW="+85291234567",145,"STO UNSENT">   OK

Some mobile devices return a slightly different response. One example is Philips 598:

AT+CMGW="+85291234567",145,"STO UNSENT"> 

OK

If you do not want to cancel the write command, enter the body of the SMS text message that you want to write to the message storage area. Press Enter on the keyboard if you want to start a new line. When finished, press Ctrl+z on the keyboard. The GSM/GPRS modem or mobile phone then attempts to write the text message to the message storage area and returns a response to the computer / PC. Suppose the SMS text message is "It is easy to write text messages.". Below shows what you should see in a terminal program:

AT+CMGW="+85291234567",145,"STO UNSENT"> It is easy to write text messages.+CMGW: 3

OK

The value in the information response is the index that indicates the location where the SMS text message is stored in the message storage area. In the above example, the information response tells us that the SMS text message "It is easy to write text messages." has been written to the memory location at index 3.

The final result code OK tells us the execution of the +CMGW AT command is successful. If it fails, the GSM/GPRS modem or mobile phone will return either the final result code ERROR or +CMS ERROR. For example, if the message storage area is out of storage space, the GSM/GPRS modem or mobile phone will return +CMS error 322, like this:

AT+CMGW="+85291234567",145,"STO UNSENT"> It is easy to write text messages.+CMS ERROR: 322

23.5. Syntax of the +CMGW AT Command in SMS PDU Mode

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

+CMGW=TPDU_length[,message_status]<CR>SMSC_number_and_TPDU<Ctrl+z>

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=42,2<CR>07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E<Ctrl+z>

The TPDU_length Parameter

The first parameter of the +CMGW AT command, TPDU_length, specifies the length (in octets. 1 octet = 8 bits) of the TPDU (Transfer Protocol Data Unit) assigned to the SMSC_number_and_TPDU parameter. In the earlier example command line, the value assigned to the SMSC_number_and_TPDU parameter is:

07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

It can be divided into two parts. The following part is the TPDU:

01000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

The TPDU is coded in hexadecimal format. Each character represents 4 bits, i.e. 1/2 octet. The TPDU has 84 characters and so there are totally 42 octets. That's why the value assigned to the TPDU_lengthparameter is 42.

The message_status Parameter

The second 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:

0. It refers to the message status "received unread". 1. It refers to the message status "received read".

2. It refers to the message status "stored unsent". This is the default value.

3. It refers to the message status "stored sent".

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 2.

Note: To some mobile devices (for example, Sony Ericsson T68i and Philips 598), an error will occur if the TPDU type is SMS-SUBMIT and the message_status parameter value is 0 or 1, or if the TPDU type is SMS-DELIVER and the message_status parameter value is 2 or 3.

The <CR> Character

<CR>, 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 PDU Mode".

The SMSC_number_and_TPDU Parameter

The third parameter of the +CMGW AT command, SMSC_number_and_TPDU, specifies the SMSC number and the TPDU in hexadecimal format. Entering the <Esc> character will cancel the +CMGS 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 PDU Mode".

In the earlier example command line, the value assigned to the SMSC_number_and_TPDU parameter is:

07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

Here is some of the information encoded in the above hexadecimal sequence:

TPDU type: SMS-SUBMIT SMSC number: +85290000000

Destination phone number: +85291234567

Text message: "It is easy to send text messages."

If you want to learn how the hexadecimal sequence is coded, please go to the section titled "Some Explanation about the Coding of the SMSC_number_and_TPDU Parameter Value of the +CMGS AT Command" of this SMS tutorial.

Besides the TPDU type SMS-SUBMIT, the +CMGW AT command accepts other TPDU types such as SMS-DELIVER.

The <Ctrl+z> Character

When you finish entering the value for the SMSC_number_and_TPDU parameter, you have to enter the<Ctrl+z> character to mark the end of the value. The GSM/GPRS modem or mobile phone will then attempt to write the SMS message to the message storage area.

23.6. SMSC Number Stored with the SMS Message (SMS PDU Mode)

In SMS PDU mode, you can specify an SMSC number to the +CMGW AT command through theSMSC_number_and_TPDU parameter. If no SMSC number is specified, the GSM/GPRS modem or mobile phone retrieves the SMSC number specified by the +CSCA AT command (command name in text: Service Centre Address) and stores it with the SMS message. 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.

23.7. Format of the Information Response of the +CMGW AT Command in SMS PDU 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 PDU mode, the information response of the +CMGW AT command has the following format:

+CMGW: index

Here is an example that gives you some idea of how an actual information response should look like:

+CMGW: 5

The index Field

The information response of the +CMGW AT command has only one field called index. The field contains an integer that tells us the memory location to which the SMS message was written in the message storage area.

23.8. Example Demonstrating How to Use the +CMGW AT Command to Write SMS Text Messages to Message Storage in SMS PDU Mode

Now let's see a more detailed example that demonstrates how to use the +CMGW AT command to write SMS text messages to message storage in SMS PDU mode and how the +CMGW 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=0OK

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 values 0 and 1 represent SMS PDU mode and SMS 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 Write SMS Text Messages to

Second, use the AT command +CPMS (command name in text: Preferred Message Storage) to select the message storage area to write SMS text messages to. 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 +CMGW, assign the string value "SM" to the second parameter of the +CPMS AT command, like this:

AT+CPMS="ME","SM"

The first parameter of the +CPMS AT command is used to select the message storage area for reading and deleting SMS messages. It has no use to us here. So, it does not matter what value is assigned to the first parameter.

Now press the Enter key on the keyboard and you should see something similar to this:

AT+CPMS="ME","SM"+CPMS: 2,150,2,10,4,160

OK

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

(Optional in SMS PDU Mode) Setting the SMSC Number to be Stored with the SMS Text Message

Third, use the AT command +CSCA (command name in text: Service Centre Address) to set the SMSC number to be stored with the SMS text message. Later if you send the SMS text

message, it will be transmitted via the SMSC at this number. Usually the default setting is correct and you do not need to make any changes. In SMS PDU mode, you can skip this step since you can pass the SMSC number directly to the +CMGW AT command as a parameter. This is different from SMS text mode, in which the only way to set the SMSC number to be stored with the SMS text message is by using the +CSCA AT command.

Below demonstrates how to use the +CSCA AT command to set "+85290000000" as the SMSC number:

AT+CSCA="+85290000000"OK

Detailed information about the +CSCA AT command can be found in the "Setting or Reading the Service Center Address / SMSC Address (AT+CSCA)" section of this SMS tutorial.

Writing Text Messages

Fourth, you can now use the +CMGW AT command to write a text message to the message storage area. Suppose you want the SMS center number to be +85290000000, the destination mobile phone number to be +85291234567, and the text message to be "It is easy to send text messages.", the hexadecimal sequence to be passed to the +CMGW AT command should be:

07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

and the length of the SMS-SUBMIT TPDU embedded in the hexadecimal sequence is 42 octets. If you forget how these values are obtained, you may want to go back to the earlier section "Syntax of the +CMGW AT Command in SMS PDU Mode".

Now pass the length of the SMS-SUBMIT TPDU to the +CMGW AT command by entering the following command line in the terminal program:

AT+CMGW=42

Then, press the Enter key of the keyboard to send a carriage return character to the GSM/GPRS modem or mobile phone. The GSM/GPRS modem or mobile phone will send back a prompt formed by four characters. They are the carriage return character, the linefeed character, the ">" character and the space character. If all characters are to be displayed, the

characters you have sent to and received from the GSM/GPRS modem or mobile phone so far are:

AT+CMGW=42<CR><CR><LF>><Space>

However, the carriage return character and linefeed character have special meanings to a terminal program. When a terminal program sees a carriage return character, it moves the cursor to the beginning of the current line. When it sees a linefeed character, it moves the cursor to the same position on the next line. So, here is what you will actually see in a terminal program such as HyperTerminal:

AT+CMGW=42> 

If you want to cancel the write command at this point, press the Esc key on the keyboard. The GSM/GPRS modem or mobile phone will then return the OK final result code. Here shows the response returned from Nokia 6021 (or Sony Ericsson T68i):

AT+CMGW=42>   OK

Some mobile devices return a slightly different response. One example is Philips 598:

AT+CMGW=42> 

OK

If you do not want to cancel the write command, enter the hexadecimal sequence containing the SMS text message that you want to write to the message storage area. When finished, press Ctrl+z on the keyboard. The GSM/GPRS modem or mobile phone then attempts to write the text message to the message storage area and returns a response to the computer / PC. Below shows what you should see in a terminal program:

AT+CMGW=42> 07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E+CMGW: 3

OK

The value in the +CMGW information response is the index that indicates the memory location to which the SMS text message was written. From above, we know that the SMS text message was written to the memory location at index 3.

The final result code OK tells us the writing of the text message to message storage was successful. If the execution of the +CMGW AT command fails, the GSM/GPRS modem or mobile phone will return either the final result code ERROR or +CMS ERROR. For example, if the message storage area is out of storage space, the GSM/GPRS modem or mobile phone will return +CMS error 322, like this:

AT+CMGW=42> 07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E+CMS ERROR: 322

24. Deleting SMS Messages from Message Storage (AT+CMGD)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.

24.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 flagvalues: 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.

24.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 flagparameter. 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)".

24.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=1OK

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 theindex parameter does not matter since it will be ignored by the GSM/GPRS modem or mobile phone.

AT+CMGD=1,4OK

25. Sending SMS Messages from a Computer / PC Using AT Commands (AT+CMGS, AT+CMSS)Either of the AT commands +CMGS (command name in text: Send Message) and +CMSS (command name in text: Send Message from Storage) can be used to send SMS messages from a computer / PC. The key difference between them is that the +CMGS AT command takes the SMS message to be sent as a parameter, while the +CMSS AT command takes the index number that specifies the location of the SMS message in the message storage area as a parameter. Following is an example for illustrating the difference. Suppose you want to send the text message "Sending text messages is easy." from a computer / PC to the mobile phone number 91234567 using the +CMGS AT command in SMS text mode. Here is the command line to be used:

AT+CMGS="91234567"<CR>Sending text messages is easy.<Ctrl+z>

To send the same text message using the +CMSS AT command, first you have to use the AT command +CMGW (command name in text: Write Message to Memory) to write the text message to the message storage area. In SMS text mode, the command line should be:

AT+CMGW="91234567"<CR>Sending text messages is easy.<Ctrl+z>

Let's say the SMS text message is now located at index 3 of the message storage area. You can use the +CMSS AT command to send the text message to the message center by the following command line:

AT+CMSS=3

As storage space is limited, if every SMS text message sent is left in the message storage area, there will come a time when no more SMS text messages can be written. To free some storage space, you can use the AT command +CMGD (command name in text: Delete Message) to delete the SMS text message from the message storage area, like this:

AT+CMGD=3

As you can see above, sending an SMS message by the +CMSS AT command is a bit cumbersome, since it involves more steps and AT commands. However, a copy of the sent SMS message can be saved in the message storage area. This is not achievable with the +CMGS AT command.

In some situations, it is more convenient to send SMS messages by the +CMSS AT command than the +CMGS AT command. For example, if you have to send the same SMS message to multiple recipients, using the +CMSS AT command is more convenient:

AT+CMSS=3,"91234567"

AT+CMSS=3,"97777777"

AT+CMSS=3,"96666666"

...

Note: To keep things simple in the above examples, we assume that the same message storage area is used by the AT commands +CMSS, +CMGW, +CMGD and for receiving SMS messages. But this may not be true in real situations. To learn how to set the message storage areas to be used by different SMS operations, please refer to the earlier section "Preferred Message Storage (AT+CPMS)" of this SMS tutorial.

25.1. Comparison Table of the AT Commands +CMGS and +CMSS

The comparison table below summarizes the differences between the AT commands +CMGS and +CMSS that are discussed in the previous section.

+CMGS AT command +CMSS AT command

Parameters +CMGS takes the SMS message to be sent as a parameter.

+CMSS takes the index number that specifies the location of the SMS message in the message storage area as a parameter.

Sending new SMS messages

Using +CMGS is more convenient. +CMGS can be used to send new SMS messages without the help of other AT commands.

However, no copy of the SMS message sent is saved in the message storage area.

+CMSS has to be used together with +CMGW and +CMGD for sending new SMS messages. As a result, the process involves more steps and is a bit cumbersome.

However, a copy of the SMS message sent can be saved in the message storage area.

Sending the same SMS message to multiple recipients

Using +CMGS is less convenient. Using +CMSS is more convenient.

25.2. Syntax of the +CMGS AT Command in SMS Text Mode

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

+CMGS=address[,address_type]<CR>sms_message_body<Ctrl+z>

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+CMGS="+85291234567",145<CR>This is an example for illustrating the syntax of the +CMGS AT command in SMS text mode.<Ctrl+z>

The address Parameter

The first parameter of the +CMGS 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", "91234567", etc. Note that the value passed to the address parameter should be a string, i.e. it should be enclosed in double quotes.

The address_type Parameter

The second parameter of the +CMGS 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. It means 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. It means 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 <CR> Character

<CR>, which represents the carriage return character, follows the address_type 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 +CMGS AT Command to Send SMS Text Messages in SMS Text Mode".

The sms_message_body Parameter

The third parameter of the +CMGS AT command, sms_message_body, specifies the body of the SMS message to be sent. Entering the <Esc> character will cancel the +CMGS AT command. If you don't understand what this means, see the example in the section "Example Demonstrating How to Use the +CMGS AT Command to Send SMS Text Messages in SMS Text Mode".

The <Ctrl+z> Character

When you finish entering the SMS message body, you have to enter the <Ctrl+z> character to mark the end of the SMS message body. The GSM/GPRS modem or mobile phone will then attempt to send the SMS message to the SMS center.

25.3. Format of the Information Response of the +CMGS AT Command in SMS Text Mode

If the GSM/GPRS modem or mobile phone sends the SMS message successfully, it will return an information response to the computer / PC. In SMS text mode, the information response of the +CMGS AT command has the following format: (Optional fields are enclosed in square brackets.)

+CMGS: message_reference[,service_center_time_stamp]

Here is an example that gives you some idea of how an actual information response should look like:

+CMGS: 5,"07/02/05,08:30:45+32"

The message_reference Field

The first field of the information response of the +CMGS AT command, message_reference, contains an integer in the range from 0 to 255. It is a reference number allocated by the GSM/GPRS modem or mobile phone to the SMS message sent.

The service_center_time_stamp Field

The second field of the information response of the +CMGS AT command, service_center_time_stamp, contains a string that tells the time and date at which the SMS message arrived at the service center (i.e. SMSC). The time stamp format is "yy/MM/dd,hh:mm:ss±zz". (yy = year, MM = month, dd = day, hh = hour, mm = minute, ss = second, zz = time zone. Note: the unit of time zone is a quarter of an hour.)

For example, "07/02/05,08:30:45+32" represents 5 February 2007, 8:30:45 GMT+8 hours. "+32" means GMT+8 hours, since 32 quarters of an hour = 8 hours.

Note that service_center_time_stamp is an optional field. A GSM/GPRS modem or mobile phone includes this field in the information response only if the messaging service value set by the AT command +CSMS (command name in text: Select Message Service) is 1 and the mobile network supports the use of this messaging service value. By default the messaging service value is 0. Usually this is the value to be used and the service_center_time_stamp field is not shown.

25.4. Example Demonstrating How to Use the +CMGS AT Command to Send SMS Text Messages in SMS Text Mode

Now let's see a more detailed example that demonstrates how to use the +CMGS AT command to send SMS text messages in SMS text mode and how the +CMGS 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=1OK

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.

Setting the SMSC Number to be Used to Send SMS Text Messages

Second, use the AT command +CSCA (command name in text: Service Centre Address) to set the SMSC number to be used to send SMS text messages. Usually the default setting is correct

and you do not need to make any changes. Below demonstrates how to use the +CSCA AT command to set "+85290000000" as the SMSC number:

AT+CSCA="+85290000000"OK

Detailed information about the +CSCA AT command can be found in the "Setting or Reading the Service Center Address / SMSC Address (AT+CSCA)" section of this SMS tutorial.

Sending Text Messages

Third, use the +CMGS AT command to send a text message to the SMSC. Suppose you want to send a text message to the mobile phone number +85291234567, you should enter something like this in a terminal program:

AT+CMGS="+85291234567"

Then, press the Enter key of the keyboard to send a carriage return character to the GSM/GPRS modem or mobile phone. The GSM/GPRS modem or mobile phone will send back a prompt formed by four characters. They are the carriage return character, the linefeed character, the ">" character and the space character. If all characters are to be displayed, the characters you have sent to and received from the GSM/GPRS modem or mobile phone so far are:

AT+CMGS="+85291234567"<CR><CR><LF>><Space>

However, the carriage return character and linefeed character have special meanings to a terminal program. When a terminal program sees a carriage return character, it moves the cursor to the beginning of the current line. When it sees a linefeed character, it moves the cursor to the same position on the next line. So, here is what you will actually see in a terminal program such as HyperTerminal:

AT+CMGS="+85291234567"> 

If you want to cancel the send command at this point, press the Esc key on the keyboard. The GSM/GPRS modem or mobile phone will then return the OK final result code. Here shows the response returned from Nokia 6021:

AT+CMGS="+85291234567">   OK

Some mobile devices return a slightly different response. One example is Philips 598:

AT+CMGS="+85291234567"> 

OK

If you do not want to cancel the send command, enter the body of the SMS text message that you want to send from your computer / PC. Press Enter on the keyboard if you want to start a new line. When finished, press Ctrl+z on the keyboard. The GSM/GPRS modem or mobile phone then attempts to send the SMS text message to the SMSC and returns a response to the computer / PC. Suppose the SMS text message is "It is easy to send text messages.". Below shows what you should see in a terminal program:

AT+CMGS="+85291234567"> It is easy to send text messages.+CMGS: 12

OK

The value in the information response, 12, is the message reference number allocated to the SMS text message. The final result code OK indicates the sending of the SMS text message was successful. If the execution of the +CMGS AT command fails, the GSM/GPRS modem or mobile phone will return either the final result code ERROR or +CMS ERROR.

25.5. Syntax of the +CMGS AT Command in SMS PDU Mode

In SMS PDU mode, the syntax of the +CMGS AT command is:

+CMGS=TPDU_length<CR>SMSC_number_and_TPDU<Ctrl+z>

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+CMGS=42<CR>07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E<Ctrl+z>

The TPDU_length Parameter

The first parameter of the +CMGS AT command, TPDU_length, specifies the length (in octets. 1 octet = 8 bits) of the TPDU (Transfer Protocol Data Unit) assigned to the SMSC_number_and_TPDU parameter. In the earlier example command line, the value assigned to the SMSC_number_and_TPDU parameter is:

07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

It can be divided into two parts. The following part is the TPDU:

01000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

The TPDU is coded in hexadecimal format. Each character represents 4 bits, i.e. 1/2 octet. The TPDU has 84 characters and so there are totally 42 octets. That's why the value assigned to the TPDU_lengthparameter is 42.

The <CR> Character

<CR>, which represents the carriage return character, follows the TPDU_length 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 +CMGS AT Command to Send SMS Text Messages in SMS PDU Mode".

The SMSC_number_and_TPDU Parameter

The second parameter of the +CMGS AT command, SMSC_number_and_TPDU, specifies the SMSC number and the TPDU in hexadecimal format. Entering the <Esc> character will cancel the +CMGS AT command. If you don't understand what this means, see the example in the section "Example Demonstrating How to Use the +CMGS AT Command to Send SMS Text Messages in SMS PDU Mode".

The <Ctrl+z> Character

When you finish entering the value for the SMSC_number_and_TPDU parameter, you have to enter the<Ctrl+z> character to mark the end of the value. The GSM/GPRS modem or mobile phone will then attempt to send the SMS message to the SMS center.

25.5.1. Some Explanation about the Coding of the SMSC_number_and_TPDU Parameter Value of the +CMGS AT Command

This section provides some explanation about the coding of the SMSC_number_and_TPDU parameter value of the +CMGS AT command. Let's consider the SMSC_number_and_TPDU parameter value in the earlier example command line:

07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

The above value can be divided into two parts, as shown below. The first part contains information about the SMSC to be used for sending the SMS message. The second part is the TPDU.

07915892000000F0

01000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

25.5.1.1. The SMSC Part

The SMSC part can be further divided into three sub-fields, like this:

07 91 5892000000F0

The First Sub-field: Length of the Second and Third Sub-fields

The first sub-field specifies the length in octets of the following two sub-fields. There are 14 hexadecimal digits in "915892000000F0" and each hexadecimal digit represents 4 bits. So, there are totally 7 octets. That's why the value of the first sub-field is 0x07.

The Second Sub-field: Type of SMSC Number

The second sub-field specifies the type of the SMSC number assigned to the third sub-field. Two values are commonly used. They are 0x81 (129 in decimal) and 0x91 (145 in decimal):

0x81. It means the SMSC number is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) but it is not sure whether the SMSC number is an international number, a national number or a number of other types. For example, suppose the SMSC number provided to you by the mobile network operator for sending SMS messages is "+85290000000". If the value of the second sub-field is 0x81, the SMSC number assigned to the third sub-field can be either "85290000000" (country code included) or "90000000" (country code omitted).

0x91. It means the SMSC number is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) and it is an international number. For example, suppose the SMSC number provided to you by the mobile network operator for sending SMS messages is "+85290000000". If the value of the second sub-field is 0x91, the SMSC number assigned to the third sub-field should be "85290000000".

The Third Sub-field: SMSC Number

The third sub-field specifies the SMSC number for sending the SMS message. 0x5892000000F0 represents the phone number +85290000000. Here's how the value 0x5892000000F0 is obtained:

1. Starting from the left, group the digits of the phone number 85290000000 into pairs, like this: 85 29 00 00 00 0.

2. As the last group has only one digit, we add an "F" to make up a pair. The result is 85 29 00 00 00 0F.

3. Swap the digits in each pair and you will get 58 92 00 00 00 F0.

Note

It is possible to tell the GSM/GPRS modem or mobile phone to use the SMSC number specified by the AT command +CSCA (command name in text: Service Centre Address) for sending SMS messages. Just assign the value 0x00 to the first sub-field and omit the second and third sub-fields, i.e. the SMSC part becomes:

00

25.5.1.2. The TPDU Part (SMS-SUBMIT TPDU)

The TPDU part can be divided into nine sub-fields, as shown below. This is a TPDU of the type SMS-SUBMIT.

01 00 0B 91 5892214365F7 00 00 21 493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

The First Sub-field: First Octet of the TPDU

The first sub-field is the first octet of the TPDU. It tells the GSM/GPRS modem or mobile phone several things:

the type of the TPDU whether the SMSC should reject duplicated TPDUs

whether a validity period exists in the TPDU, and the format of the validity period if it exists

whether a reply path is requested

whether a user data header exists in the TPDU

whether a status report is requested from the SMSC

The value 0x01 means:

the type of the TPDU is SMS-SUBMIT the SMSC should not reject duplicated TPDUs

no validity period exists in the TPDU

no reply path is requested

no user data header exists in the TPDU

no status report is requested from the SMSC

The Second Sub-field: Message Reference Number

The second sub-field specifies the message reference number. It is an integer in the range from 0 to 255. The value 0x00 tells the GSM/GPRS modem or mobile phone to assign a message reference number to the SMS message automatically.

The Third Sub-field: Length of the Destination Phone Number

The third sub-field specifies the length in digits of the destination phone number. The destination phone number specified in the fifth sub-field is "+85291234567", which has 11 digits. Thus, the value of the third sub-field is 0x0B (i.e. 11 in decimal).

The Fourth Sub-field: Type of the Destination Phone Number

The fourth sub-field specifies the type of the destination phone number assigned to the fifth sub-field. Two values are commonly used. They are 0x81 (129 in decimal) and 0x91 (145 in decimal):

0x81. It means the destination phone number is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) but it is not sure whether the destination phone number is an international number, a national number or a number of other types. For example, suppose you want to send an SMS message to the phone number "+85291234567". If the value of the fourth sub-field is 0x81, the destination phone number assigned to the fifth sub-field can be either "85291234567" (country code included) or "91234567" (country code omitted).

0x91. It means the destination phone number is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) and it is an international number. For example, suppose you want to send an SMS message to the phone number "+85291234567". If the value of the fourth sub-field is 0x91, the destination phone number assigned to the fifth sub-field should be "85291234567".

The Fifth Sub-field: Destination Phone Number

The fifth sub-field specifies the destination phone number. The value 0x5892214365F7 represents the phone number +85291234567. Here's how the value 0x5892214365F7 is obtained:

Starting from the left, group the digits of the phone number 85291234567 into pairs, like this: 85 29 12 34 56 7.

As the last group has only one digit, we add an "F" to make up a pair. The result is 85 29 12 34 56 7F.

Swap the digits in each pair and you will get 58 92 21 43 65 F7.

The Sixth Sub-field: Protocol Identifier

The sixth sub-field specifies the protocol identifier. Its value should be 0x00 for normal cases.

The Seventh Sub-field: Data Coding Scheme

The seventh sub-field specifies the data coding scheme. With 0x00, we inform the GSM/GPRS modem or mobile phone that the text in the SMS message body is encoded according to the "GSM 7-bit default alphabet" text coding scheme.

The Eighth Sub-field: Length of the SMS Message Body

The eighth sub-field specifies the length of the SMS message body in septets (1 septet = 7 bits). The value 0x21 means there are 33 septets (or characters, since each character is represented by 7 bits according to the "GSM 7-bit default alphabet" text coding scheme) in the SMS message body.

The Ninth Sub-field: SMS Message Body

The ninth sub-field specifies the SMS message body. The value 0x493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E represents the text message "It is easy to send text messages.". Below shows how the hexadecimal value is obtained:

Encode the SMS text message "It is easy to send text messages." according to the character set "GSM 7-bit default alphabet". For example, 0x49 is used to represent the first character "I" in the SMS text message, 0x74 is used to represent the second character "t", etc. To find the code representing a certain character, see "Appendix: GSM 7-bit Default Alphabet Table (with Character Codes of ISO 8859 Latin 1)" of this SMS tutorial.

After encoding, the SMS text message "It is easy to send text messages." becomes:

49 74 20 69 73 20 65 61 73 79 20 74 6F 20 73 65 6E 64 20 74 65 78 74 20 6D 65 73 73 61 67 65 73 2E

With the character set "GSM 7-bit default alphabet", each character is represented by 7 bits. If the SMS text message is encoded as above, 1 bit is wasted in every octet. To prevent this from happening, the SMS specification requires us to pack the 7-bit characters in octets. The packing process is described below:

o The binary representation of the first character is 1001001. Since the most significant bit of the first octet is unused, we fill it by the least significant bit of the second character, which is 0. (The binary representation of the second character is 1110100.) As a result, the first octet contains the binary value 01001001, which is 0x49 in hexadecimal format.

o As the least significant bit of the second character has been moved to the first octet, the second octet now contains the binary value 111010 and the two bits at the leftmost position of the second octet is unused. We fill them by the two least significant bits of the third character, which are both 0. (The binary representation of the third character is 0100000.) As a result, the second octet contains 00111010, which is 0x3A in hexadecimal format.

o Similarly, since the two least significant bits of the third character have been moved to the second octet, the third octet now contains the binary value 01000 and the three bits at the leftmost position of the third octet is unused. So, we fill them by the three least significant bits of the fourth character, which are 0, 0 and 1. (The binary representation of the fourth character is 1101001.) Now the third octet contains the binary value 00101000, which is 0x28 in hexadecimal format.

o The packing process continues. This is illustrated in the following table.

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Octet 1 0 1 0 0 1 0 0 1 = 0x49

Octet 2 0 0 1 1 1 0 1 0 = 0x3A

Octet 3 0 0 1 0 1 0 0 0 = 0x28

Octet 4 0 0 1 1 1 1 0 1 = 0x3D

Octet 5 0 0 0 0 0 1 1 1 = 0x07

Octet 6 1 0 0 1 0 1 0 1 = 0x95

Octet 7 1 1 0 0 0 0 1 1 = 0xC3

Octet 8 1 1 1 1 0 0 1 1 = 0xF3

Octet 9 0 0 1 1 1 1 0 0 = 0x3C

Octet 10 1 0 0 0 1 0 0 0 = 0x88

Octet 11 1 1 1 1 1 1 1 0 = 0xFE

Octet 12 0 0 0 0 0 1 1 0 = 0x06

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Octet 13 1 1 0 0 1 1 0 1 = 0xCD

Octet 14 1 1 0 0 1 0 1 1 = 0xCB

Octet 15 0 1 1 0 1 1 1 0 = 0x6E

Octet 16 0 0 1 1 0 0 1 0 = 0x32

Octet 17 1 0 0 0 1 0 0 0 = 0x88

Octet 18 0 1 0 1 1 1 1 0 = 0x5E

Octet 19 1 1 0 0 0 1 1 0 = 0xC6

Octet 20 1 1 0 1 0 0 1 1 = 0xD3

Octet 21 0 1 0 0 0 0 0 1 = 0x41

Octet 22 1 1 1 0 1 1 0 1 = 0xED

Octet 23 1 1 1 1 0 0 1 0 = 0xF2

Octet 24 0 1 1 1 1 1 0 0 = 0x7C

Octet 25 0 0 0 1 1 1 1 0 = 0x1E

Octet 26 0 0 1 1 1 1 1 0 = 0x3E

Octet 27 1 0 0 1 0 1 1 1 = 0x97

Octet 28 1 1 1 0 0 1 1 1 = 0xE7

Octet 29 0 0 1 0 1 1 1 0 = 0x2E

25.6. Format of the Information Response of the +CMGS AT Command in SMS PDU Mode

If the GSM/GPRS modem or mobile phone sends the SMS message successfully, it will return an information response to the computer / PC. In SMS PDU mode, the information response of the +CMGS AT command has the following format: (Optional fields are enclosed in square brackets.)

+CMGS: message_reference[,SMS-SUBMIT-REPORT_TPDU]

The message_reference Field

The first field of the information response of the +CMGS AT command, message_reference, contains an integer in the range from 0 to 255. This field tells you the reference number that was allocated to your SMS message.

The SMS-SUBMIT-REPORT_TPDU Field

The second field of the information response of the +CMGS AT command, SMS-SUBMIT-REPORT_TPDU, contains a TPDU of the type SMS-SUBMIT-REPORT in hexadecimal format. It carries a positive submission report returned from the SMSC. It acknowledges the GSM/GPRS modem or mobile phone that the submission of the SMS message to the SMSC was successful. The value of theSMS-SUBMIT-REPORT_TPDU field is a string and so it is always enclosed in quotes.

Note that SMS-SUBMIT-REPORT_TPDU is an optional field. A GSM/GPRS modem or mobile phone includes this field in the information response only if the messaging service value set by the AT command +CSMS (command name in text: Select Message Service) is 1 and the mobile network supports the use of this messaging service value. By default the messaging service value is 0. Usually this is the value to be used and the SMS-SUBMIT-REPORT_TPDU field is not shown.

25.7. Example Demonstrating How to Use the +CMGS AT Command to Send SMS Text Messages in SMS PDU Mode

Now let's see a more detailed example that demonstrates how to use the +CMGS AT command to send SMS text messages in SMS PDU mode and how the +CMGS 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=0OK

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.

(Optional in SMS PDU Mode) Setting the SMSC Number to be Used to Send SMS Text Messages

Second, set the SMSC number to be used to send SMS text messages by the AT command +CSCA (command name in text: Service Centre Address). Usually the default setting is correct and you do not need to make any changes. In SMS PDU mode, you can skip this step since you can pass the SMSC number to the +CMGS AT command as a parameter. This is different from SMS text mode, in which the only way to set the SMSC number for sending SMS text messages is by using the +CSCA AT command.

Below demonstrates how to use the +CSCA AT command to set "+85290000000" as the SMSC number:

AT+CSCA="+85290000000"OK

Detailed information about the +CSCA AT command can be found in the "Setting or Reading the Service Center Address / SMSC Address (AT+CSCA)" section of this SMS tutorial.

Sending Text Messages

Third, use the +CMGS AT command to send a text message to the SMSC. Suppose you want to send the text message "It is easy to send text messages." to the mobile phone number +85291234567 through the SMS center at +85290000000, the hexadecimal sequence to be passed to the +CMGS AT command should be:

07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

and the length of the SMS-SUBMIT TPDU embedded in the hexadecimal sequence is 42 octets. We have discussed how to obtain these values in previous sections and so we will not talk about them again here.

Now pass the length of the SMS-SUBMIT TPDU to the +CMGS AT command by entering the following command line in a terminal program:

AT+CMGS=42

Then, press the Enter key of the keyboard to send a carriage return character to the GSM/GPRS modem or mobile phone. The GSM/GPRS modem or mobile phone will send back a prompt formed by four characters. They are the carriage return character, the linefeed character, the ">" character and the space character. If all characters are to be displayed, the characters you have sent to and received from the GSM/GPRS modem or mobile phone so far are:

AT+CMGS=42<CR><CR><LF>><Space>

However, the carriage return character and linefeed character have special meanings to a terminal program. When a terminal program sees a carriage return character, it moves the cursor to the beginning of the current line. When it sees a linefeed character, it moves the cursor to the same position on the next line. So, here is what you will actually see in a terminal program such as HyperTerminal:

AT+CMGS=42> 

If you want to cancel the send command at this point, press the Esc key on the keyboard. The GSM/GPRS modem or mobile phone will then return the OK final result code. Here shows the response returned from Nokia 6021 (or Sony Ericsson T68i):

AT+CMGS=42>   OK

Some mobile devices return a slightly different response. One example is Philips 598:

AT+CMGS=42> 

OK

If you do not want to cancel the send command, enter the hexadecimal sequence containing the SMS text message that you want to send from your computer / PC. When finished, press Ctrl+z on the keyboard. The GSM/GPRS modem or mobile phone then attempts to send the SMS text message to the SMSC and returns a response to the computer / PC. Below shows what you should see in a terminal program:

AT+CMGS=42> 07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E+CMGS: 12

OK

The value in the information response, 12, is the message reference number allocated to the SMS text message by the GSM/GPRS modem or mobile phone. The final result code OK indicates the sending of the SMS text message was successful. If the execution of the +CMGS AT command fails, the GSM/GPRS modem or mobile phone will return either the final result code ERROR or +CMS ERROR.

25.8. Syntax of the +CMSS AT Command in SMS Text Mode and PDU Mode

Unlike +CMGS, the syntax of the +CMSS AT command is the same in both SMS text mode and PDU mode. Below shows the syntax of the +CMSS AT command: (Optional parameters are enclosed in square brackets.)

+CMSS=index[,address[,address_type]]

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+CMSS=5,"+85291234567",145

The index Parameter

The first parameter of the +CMSS AT command, index, specifies the location of the SMS message in the message storage area.

The address Parameter

The second parameter of the +CMSS 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", "91234567", etc. 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 it can be omitted. If you do so, the +CMSS AT command will use the address stored in the SMS message header as the destination address.

The address_type Parameter

The third parameter of the +CMSS 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. It means 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. It means 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".

25.9. Format of the Information Response of the +CMSS AT Command in SMS Text Mode and PDU Mode

If the GSM/GPRS modem or mobile phone sends the SMS message successfully, it will return an information response to the computer / PC. The format of the +CMSS information response in SMS text mode and that in SMS PDU mode differ by a single parameter.

In SMS text mode, the information response of the +CMSS AT command has the following format: (Optional fields are enclosed in square brackets.)

+CMSS: message_reference[,service_center_time_stamp]

Here is an example that gives you some idea of how an actual information response should look like:

+CMSS: 5,"07/02/05,08:30:45+32"

In SMS PDU mode, the information response of the +CMSS AT command has the following format: (Optional fields are enclosed in square brackets.)

+CMSS: message_reference[,SMS-SUBMIT-REPORT_TPDU]

The message_reference Field

The first field of the information response of the +CMSS AT command, message_reference, contains an integer in the range from 0 to 255. It is a reference number allocated by the GSM/GPRS modem or mobile phone to the SMS message sent.

The service_center_time_stamp Field

In SMS text mode, the second field of the information response of the +CMSS AT command isservice_center_time_stamp. It contains a string that indicates the time and date at which the SMS message arrived at the service center (i.e. SMSC). The time stamp format is "yy/MM/dd,hh:mm:ss±zz", where yy = year, MM = month, dd = day, hh = hour, mm = minute, ss = second, zz = time zone. Note: the unit of time zone is a quarter of an hour.

For example, "07/02/05,08:30:45+32" represents 5 February 2007, 8:30:45 GMT+8 hours. "+32" means GMT+8 hours, since 32 quarters of an hour = 8 hours.

Note that service_center_time_stamp is an optional field. A GSM/GPRS modem or mobile phone includes this field in the information response only if the messaging service value set by the AT command +CSMS (command name in text: Select Message Service) is 1 and the mobile network supports the use of this messaging service value. By default the messaging service value is 0. Usually this is the value to be used and the service_center_time_stamp field is not shown.

The SMS-SUBMIT-REPORT_TPDU Field

In SMS PDU mode, the second field of the information response of the +CMSS AT command is SMS-SUBMIT-REPORT_TPDU. It contains a TPDU of the type SMS-SUBMIT-REPORT in hexadecimal format. It carries a positive submission report returned from the SMSC. It acknowledges the GSM/GPRS modem or mobile phone that the submission of the SMS message to the SMSC was successful. The value of the SMS-SUBMIT-REPORT_TPDU field is a string and so it is always enclosed in quotes.

Note that SMS-SUBMIT-REPORT_TPDU is an optional field. A GSM/GPRS modem or mobile phone includes this field in the information response only if the messaging service value set by the AT command +CSMS (command name in text: Select Message Service) is 1 and the mobile network supports the use of this messaging service value. By default the messaging service value is 0. Usually this is the value to be used and the SMS-SUBMIT-REPORT_TPDU field is not shown.

25.10. SMSC Number to be Used by the +CMSS AT Command to Send SMS Messages

Earlier in this SMS tutorial, we described how the AT command +CSCA (command name in text: Service Centre Address) could be used to set the SMSC number for sending SMS messages. The AT command +CSCA is straightforward to use. However, the following fact complicates things a bit: an SMS message can have an SMSC number stored with it in message storage and this SMSC number is used by the +CMSS AT command to send the SMS message.

25.10.1. SMS Text Mode

In SMS text mode, when you use the AT command +CMGW (command name in text: Write Message to Memory) to write an SMS message to the message storage area, the SMSC number specified by the AT command +CSCA is saved with the SMS message. The +CMSS AT command will use this SMSC number to send the SMS message.

To understand better, let's consider the following example. Suppose we use the AT command +CSCA to change the SMSC number to +85290000000, like this:

AT+CSCA="+85290000000"

Then we use the AT command +CMGW to write the text message "Sending text messages is easy." to the message storage area. In SMS text mode, the command line should be something like this:

AT+CMGW<CR>Sending text messages is easy.<Ctrl+z>

After that, we use the AT command +CSCA to change the SMSC number to +85291111111:

AT+CSCA="+85291111111"

At this point, if we use the +CMSS AT command to send the text message out, the GSM/GPRS modem or mobile phone will perform the task using the SMSC number +85290000000, not the current number +85291111111.

Once an SMS message has been written to the message storage area, there is no way to change the SMSC number saved with it via AT commands. But you can always create a new SMS message with the same message body but with a different SMSC number.

Note: It is possible that the command behavior on your mobile device is slightly different from what was described above. So, you may want to do something similar to the above example to find out the exact command behavior on your mobile device.

25.10.2. SMS PDU Mode

In SMS PDU mode, when you use the AT command +CMGW to write an SMS message to the message storage area, a certain SMSC number is saved with the SMS message. This SMSC number will be used by the AT command +CMSS for sending the SMS message out. The value assigned to theSMSC_number_and_TPDU parameter of the AT command +CMGW determines what SMSC number is saved with the SMS message.

Note that once an SMS message has been written to the message storage area, there is no way to change the SMSC number saved with it via AT commands. But you can always create a new SMS message with the same message body but with a different SMSC number.

The value of the SMSC_number_and_TPDU parameter can be divided into two parts: the SMSC part and TPDU part. Following is an example that gives you some idea of how an actual value of theSMSC_number_and_TPDU parameter should look like. The SMSC part is displayed in bold type.

07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

25.10.2.1. Case 1: SMSC Part is Not "00"

If the SMSC part is not "00", a certain SMSC number is encoded in the SMSC part. It will be saved with the SMS message in message storage. The AT command +CMSS will use this SMSC number for sending the SMS message. In the above example, the SMSC number +85290000000 is encoded in "07915892000000F0".

25.10.2.2. Case 2: SMSC Part is "00"

If the SMSC part is "00", no SMSC number is encoded in the SMSC part. The AT command +CMGW will retrieve the SMSC number specified by the AT command +CSCA, generate an SMSC part from the SMSC number, and replace the old SMSC part "00" with the newly generated SMSC part. In other words, the SMSC number specified by the AT command +CSCA will be saved with the SMS message. This SMSC number will be used by the AT command +CMSS for sending the SMS message out.

To understand better, let's consider the following example. Suppose we use the AT command +CSCA to change the SMSC number to +85290000000, like this:

AT+CSCA="+85290000000"

Then we use the AT command +CMGW to write the text message "It is easy to send text messages." to the message storage area. In SMS PDU mode, the command line should be something like this: (The SMSC part is displayed in bold type.)

AT+CMGW=42<CR>0001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E<Ctrl+z>

After that, we use the AT command +CMGR (command line in text: Read Message) to read the SMS text message. The information response returned is shown below. (The SMSC part is displayed in bold type.)

+CMGR: 2,,4207915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

As you can see, the hexadecimal value returned is different from what we assigned to the AT command +CMGW. The old SMSC part "00" was replaced by the new SMSC part "07915892000000F0", which specifies the SMSC number +85290000000.

More Details

More details about the SMSC_number_and_TPDU parameter of the AT command +CMGW can be found from the section "Syntax of the +CMGW AT Command in SMS PDU Mode" of this SMS tutorial.

Note

Mobile devices such as Nokia 6021 and Philips 598 work in the way described above. However, it is possible that the command behavior on your mobile device is slightly different from what was described. So, you may want to do something similar to the above example to find out the exact command behavior on your mobile device.

One mobile phone that has a different command behavior is Sony Ericsson T68i. The difference is the time at which the old SMSC part "00" is replaced by the newly generated SMSC part. On Sony Ericsson T68i, the replacement is done when the +CMSS AT command is executed. To understand better, let's consider the following example. Suppose we use the AT command +CSCA to change the SMSC number to +85290000000, like this:

AT+CSCA="+85290000000"

Then we use the AT command +CMGW to write the text message "It is easy to send text messages." to the message storage area. In SMS PDU mode, the command line should be something like this: (The SMSC part is displayed in bold type.)

AT+CMGW=42<CR>0001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E<Ctrl+z>

After that, we use the AT command +CMGR to read the SMS text message. The information response returned is shown below. (The SMSC part is displayed in bold type.)

+CMGR: 2,,420001440B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

As you can see, the SMSC part is the same as the one we assigned to the +CMGW AT command.

Now use the +CMSS AT command to send the text message out. Then use the AT command +CMGR to read the SMS text message again. This time you should find that the SMSC part was changed to "07915892000000F0", which specifies the SMSC number +85290000000.

+CMGR: 2,,4207915892000000F001440B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

25.10.3. Reading the SMSC Number Saved with an SMS Message

Sometimes you may want to find out the SMSC number saved with an SMS message. It is not difficult to do so. The following sections discuss how this can be done through the AT command +CMGR(command name in text: Read Message).

25.10.3.1. SMS Text Mode

In SMS text mode, first assign the value 1 to the AT command +CSDH (command name in text: Show Text Mode Parameters) to tell the GSM/GPRS modem or mobile phone that we want detailed header information. Then use the AT command +CMGR to read the SMS message from the message storage area. The SMSC number can be found in the ninth field of the +CMGR information response.

Here is an example for illustration. Suppose there is an SMS text message "It is easy to send text messages." located at index 5 of the message storage area. To read the SMSC number saved with it, enter the command line "AT+CSDH=1" and then "AT+CMGR=5". The result shown in a terminal program such as HyperTerminal of Microsoft Windows should be something like this:

AT+CSDH=1OKAT+CMGR=5+CMGR: "STO UNSENT","+85291234567",,145,17,0,0,167,"+85290000000",145,30It is easy to send text messages.

OK

The value of the ninth field of the +CMGR information response, i.e. +85290000000, is the SMSC number saved with the SMS text message.

25.10.3.2. SMS PDU Mode

If the GSM/GPRS modem or mobile phone is operating in SMS PDU mode, first use the +CMGR AT command to read the SMS message from the message storage area. Then decode the hexadecimal sequence of the fourth field of the +CMGR information response.

Here is an example for illustration. Suppose there is an SMS text message "It is easy to send text messages." located at index 5 of the message storage area. To read the SMSC number saved with it, enter the command line "AT+CMGR=5". In SMS PDU mode, the result shown in a terminal program such as HyperTerminal of Microsoft Windows should be something like this:

AT+CMGR=5+CMGR: 2,,4207915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

OK

The hexadecimal sequence of the fourth field of the +CMGR information response is:

07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

It can be divided into two parts: the SMSC part and TPDU part. Here is the SMSC part:

07915892000000F0

The SMSC part can be further divided into three sub-fields, like this:

07 91 5892000000F0

The value of the first sub-field 0x07 tells us that the second and third sub-fields consist of 7 octets totally. The value of the second sub-field 0x91 indicates the SMSC number encoded in the third sub-field is in the typical international numbering format. The value of the third sub-field 0x5892000000F0 tells us the SMSC number saved with the SMS text message. To decode the hexadecimal value, group the digits into pairs, like this: 58 92 00 00 00 F0. Then swap the digits in each pair and you will get 85 29 00 00 00 0F. Remove the last "F", because it was placed there to fill the empty bits of the last octet and it is not part of the SMSC number. After that, combine the digits and you will get the SMSC number +85290000000.

More information about the sub-fields is available in the section "The SMSC Part" of this SMS tutorial.

25.11. Example Demonstrating How to Use the +CMSS AT Command to Send Text Messages from a Message Storage Area (SMS Text Mode and SMS PDU Mode)

Now let's see a more detailed example that demonstrates how to use the +CMSS AT command to send text messages from a message storage area and how the +CMSS AT command should be used together with other AT commands. Since the syntax of the +CMSS AT command is the same in both SMS text mode and SMS PDU mode, the information provided below applies to both modes.

Selecting the Message Storage Area to Write Text Messages to and Send Text Messages from

First, use the +CPMS AT command (command name in text: Preferred Message Storage) to select the message storage area to write text messages to (+CMGW AT command) and send text messages from (+CMSS AT command). For example, to use the message storage area in the SIM card for writing and sending SMS text messages, assign the string value "SM" to the second parameter of the +CPMS AT command, like this:

AT+CPMS="ME","SM"

The first parameter of the +CPMS AT command is used to select the message storage area for reading and deleting SMS messages. It has no use to us here. So, it does not matter what value is assigned to the first parameter.

Now press the Enter key on the keyboard and you should see something similar to this:

AT+CPMS="ME","SM"+CPMS: 2,150,2,10,4,160

OK

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

Setting the SMSC Number to be Used to Send SMS Text Messages

Second, use the AT command +CSCA (command name in text: Service Centre Address) to set the SMSC number to be used to send SMS text messages. Usually the default setting is correct and you do not need to make any changes. Below demonstrates how to use the +CSCA AT command to set "+85290000000" as the SMSC number:

AT+CSCA="+85290000000"OK

Detailed information about the +CSCA AT command can be found in the "Setting or Reading the Service Center Address / SMSC Address (AT+CSCA)" section of this SMS tutorial.

Writing Text Messages

Third, use the +CMGW AT command (command line in text: Write Message to Memory) to write the text message to message storage. Suppose the recipient mobile phone number is +85291234567. If the GSM/GPRS modem or mobile phone is operating in SMS text mode, you should enter something like this in the terminal program and press the Enter key of the keyboard:

AT+CMGW="+85291234567"

The GSM/GPRS modem or mobile phone will return a prompt, as shown below:

AT+CMGW="+85291234567"> 

Now enter the body of the SMS text message "It is easy to send text messages." and press Ctrl+z on the keyboard to finish. You should see something like this in the terminal program:

AT+CMGW="+85291234567"> It is easy to send text messages.+CMGW: 3

OK

The value 3 in the +CMGW information response is the index that specifies the location where the text message is stored in the message storage area. Later, we have to use this index to tell the +CMSS AT command which text message should be sent.

If the GSM/GPRS modem or mobile phone is operating in SMS PDU mode, here's what you should type and see in the terminal program:

AT+CMGW=42> 0001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E+CMGW: 3

OK

For further details the +CMGW AT command, please refer to the earlier section "Writing SMS Messages to Memory / Message Storage (AT+CMGW)" of this SMS tutorial.

Sending Text Messages from Message Storage

Fourth, specify the index "3" to the +CMSS AT command to send the text message out:

AT+CMSS=3+CMSS: 20

OK

"20" is the message reference number allocated by the GSM/GPRS modem or mobile phone to the text message. The final result code OK indicates the text message was sent to the SMSC successfully. If the execution of the AT command +CMSS fails, the GSM/GPRS modem or mobile phone will return either the final result code ERROR or +CMS ERROR.

In the above example, the address parameter of the AT command +CMSS is omitted. Thus, the mobile phone number stored with the SMS text message (i.e. +85291234567) will be used as the destination address. Suppose you want to send the text message to another mobile phone number, say +85298765432, you should assign "+85298765432" to the address parameter of +CMSS, as shown below:

AT+CMSS=3,"+85298765432"+CMSS: 21

OK

26. Reading SMS Messages from a Message Storage Area Using AT Commands (AT+CMGR, AT+CMGL)To enable a computer / PC to read SMS messages from a message storage area, the GSM/GPRS modem or mobile phone has to support either of the AT commands +CMGR (command name in text: Read Messages) and +CMGL (command name in text: List Messages). The +CMGR AT command is used to read an SMS message at a certain location of the message storage area, while the +CMGL AT command is used to read SMS messages that have a certain status from the message storage area. The status can be "received unread",

"received read", "stored unsent", "stored sent", etc. The +CMGL AT command also allows you to retrieve all SMS messages stored in the message storage area.

Following is an example for illustrating the difference between +CMGR and +CMGL. Suppose you want to use your computer / PC to read a text message from the message storage area and you know the index at which the SMS text message is located. In this case, you should use the +CMGR AT command. Here is the command line to be typed (assume the SMS text message is stored at index 3):

AT+CMGR=3

The GSM/GPRS modem or mobile phone should return something like this:

+CMGR: "REC READ","+85291234567",,"07/02/18,00:12:05+32"Hello, welcome to our SMS tutorial.

OK

Suppose you are now in another situation. You want to use your computer / PC to retrieve all inbound SMS messages that have not been read before. In this case, you should use the +CMGL AT command. In SMS text mode, the command line to be used should be:

AT+CMGL="REC UNREAD"

The GSM/GPRS modem or mobile phone should return something like this:

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

OK

The +CMGL AT command can also be used to read all SMS messages stored in the message storage area. To do so in SMS text mode, the command line should be:

AT+CMGL="ALL"

This time the response of the GSM/GPRS modem or mobile phone should be something like this:

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

OK

As you can see above, the +CMGR AT command can only be used to read one SMS message at a time, while the +CMGL AT command can be used to read multiple SMS messages at a time.

Another difference between the AT commands +CMGR and +CMGL is that +CMGR can be used to retrieve more message details than +CMGL when the GSM/GPRS modem or mobile phone is operating in SMS text mode. Here are the message details that can be retrieved by the +CMGR AT command in SMS text mode:

Status of the SMS message ("received unread", "received read", "stored unsent", "stored sent", etc).

Originator/recipient phone number stored in the SMS message header and the type of the phone number.

Text associated to the originator/recipient phone number in the phonebook.

Time and date at which the SMS message arrived at the SMSC (available to incoming SMS messages only).

The first octet (1 octet = 8 bits) of the TPDU (Transport Protocol Data Unit) of the SMS message.

The protocol identifier value in the TPDU of the SMS message.

The data coding scheme value in the TPDU of the SMS message.

SMSC number stored with the SMS message and the type of the SMSC number.

Validity period of the SMS message (available to outgoing SMS messages only).

Body of the SMS message and its length.

Here are the message details that can be retrieved by the +CMGL AT command in SMS text mode:

The index specifying the location of the SMS message in the message storage area. Status of the SMS message ("received unread", "received read", "stored unsent", "stored

sent", etc).

Originator/recipient phone number stored in the SMS message header and the type of the phone number.

Text associated to the originator/recipient phone number in the phonebook.

Time and date at which the SMS message arrived at the SMSC (available to incoming SMS messages only).

Body of the SMS message and its length.

If you compare the above two lists, you will find that the following message details can only be retrieved by the +CMGR AT command but not the +CMGL AT command:

The first octet of the TPDU of the SMS message. The protocol identifier value in the TPDU of the SMS message.

The data coding scheme value in the TPDU of the SMS message.

SMSC number stored with the SMS message and the type of the SMSC number.

Validity period of the SMS message (available to outgoing SMS messages only).

Note: If the GSM/GPRS modem or mobile phone is operating in SMS PDU mode, the message details that can be retrieved by +CMGR and +CMGL are the same.

26.1. Comparison Table of the AT Commands +CMGR and +CMGL

The comparison table below summarizes the differences between the AT commands +CMGR and +CMGL that are discussed in the previous section.

+CMGR AT command +CMGL AT command

Usage +CMGR is used to read an SMS message at a certain location of the message storage area.

+CMGL is used to read SMS messages that have a certain status (examples: "received unread", "received read", "stored unsent" and "stored sent") from the message

+CMGR AT command +CMGL AT command

storage area. It also allows you to retrieve all SMS messages stored in the message storage area.

Number of SMS messages returned

+CMGR returns one SMS message.

+CMGL returns one or more SMS messages.

Message details returned (SMS text mode)

1.

(Note: +CMGR does not provide the index of the SMS message in the response because it is the parameter value you passed to +CMGR.)

2. Status of the SMS message ("received unread", "received read", "stored unsent", "stored sent", etc).

3. Originator/recipient phone number stored in the SMS message header and the type of the phone number.

4. Text associated to the originator/recipient phone number in the phonebook.

5. Time and date at which the SMS message arrived at the SMSC (available to incoming SMS messages only).

6. The first octet (1 octet = 8 bits) of the TPDU (Transport Protocol Data Unit) of the SMS message.

7. The protocol identifier value in the TPDU of the SMS message.

8. The data coding scheme value in the TPDU of the SMS message.

9. SMSC number stored with the SMS message and the

1. The index specifying the location of the SMS message in the message storage area.

2. Status of the SMS message ("received unread", "received read", "stored unsent", "stored sent", etc).

3. Originator/recipient phone number stored in the SMS message header and the type of the phone number.

4. Text associated to the originator/recipient phone number in the phonebook.

5. Time and date at which the SMS message arrived at the SMSC (available to incoming SMS messages only).

6.7.8.9.10.

11. Body of the SMS message and its length.

+CMGR AT command +CMGL AT command

type of the SMSC number.

10. Validity period of the SMS message (available to outgoing SMS messages only).

11. Body of the SMS message and its length.

Message details returned (SMS PDU mode)

Same

26.2. Using the +CMGR AT Command to Read an SMS Message from a Message Storage Area

The AT command +CMGR (command name in text: Read Message) is used to read a message from a message storage area. The location of the message to be read from the message storage area is specified by an index number. The message to be retrieved by the AT command +CMGR does not necessarily have to be an SMS message. It can be of other message types such as status reports and cell broadcast messages, but we will only focus on SMS messages here.

In SMS text mode, you can retrieve the following information about an SMS message with the AT command +CMGR:

Status of the SMS message ("received unread", "received read", "stored unsent", "stored sent", etc).

Originator/recipient phone number stored in the SMS message header and the type of the phone number.

Text associated to the originator/recipient phone number in the phonebook.

Time and date at which the SMS message arrived at the SMSC (available to incoming SMS messages only).

The first octet (1 octet = 8 bits) of the TPDU (Transport Protocol Data Unit) of the SMS message.

The protocol identifier value in the TPDU of the SMS message.

The data coding scheme value in the TPDU of the SMS message.

SMSC number stored with the SMS message and the type of the SMSC number.

Validity period of the SMS message (available to outgoing SMS messages only).

Body of the SMS message and its length.

In SMS PDU mode, the list becomes:

Status of the SMS message ("received unread", "received read", "stored unsent", "stored sent", etc).

Text associated to the originator/recipient phone number in the phonebook.

SMSC number stored with the SMS message and the type of the SMSC number.

TPDU of the SMS message and its length.

After using the +CMGR AT command to read an SMS message with the "received unread" status, the status will be changed to "received read".

The message storage area from which SMS messages are read 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.

26.2.1. Syntax of the +CMGR AT Command in SMS Text Mode

The syntax of the +CMGR AT command in SMS text mode is:

+CMGR=index

index is an integer specifying the location of the SMS message to be read from the message storage area.

26.2.2. Format of the Information Response of the +CMGR AT Command in SMS Text Mode

If the GSM/GPRS modem or mobile phone successfully reads the SMS message from message storage, it will return an information response to the computer / PC. In SMS text mode, the format of the information response of the +CMGR AT command is different for different message types. In the sections that follow, we assume the message to be read is an SMS message but not of other message types like status reports and cell broadcast messages.

26.2.2.1. Incoming SMS Messages and Outgoing SMS Messages

If the SMS message retrieved is an SMS message received from the SMS center (i.e. incoming SMS message), the information response of the +CMGR AT command in SMS text mode has the following format: (Optional fields are enclosed in square brackets.)

+CMGR: message_status,address,[address_text],service_center_time_stamp[,address_type,TPDU_first_octet,protocol_identifier,data_coding_scheme,service_center_address,service_center_address_type,sms_message_body_length]<CR><LF>sms_message_body

If the SMS message retrieved is an SMS message that you wrote to the message storage area for sending out (i.e. outgoing SMS message), the format is: (Optional fields are enclosed in square brackets.)

+CMGR: message_status,address,[address_text][,address_type,TPDU_first_octet,protocol_identifier,data_coding_scheme,[validity_period],service_center_address,service_center_address_type,sms_message_body_length]<CR><LF>sms_message_body

As you can see above, the format of the +CMGR information response for incoming SMS messages and for outgoing SMS messages is slightly different. The differences are:

The service_center_time_stamp field only exists in the information response for incoming SMS messages.

The validity_period field only exists in the information response for outgoing SMS messages.

Before we discuss each of the fields that appear in the +CMGR information response, let's see two examples that give you some idea of how an actual +CMGR information response should look like:

For incoming SMS messages:

+CMGR: "REC READ","+85291234567",,"07/04/20,10:08:02+32",145,4,0,0,"+85290000000",145,49It is easy to read text messages via AT commands.

For outgoing SMS messages:

+CMGR: "STO SENT","+85291234567",,145,17,0,0,167,"+85290000000",145,36A simple demo of SMS text messaging.

26.2.2.2. Details of the Fields that Appear in the +CMGR Information Response

The message_status Field

The first field of the information response of the +CMGR AT command, message_status, is a string that indicates the status of the SMS message. It can be one of the following four 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".

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

The address Field

The second field of the information response of the +CMGR AT command, address, is a string that contains the address/phone number stored in the SMS message header. If the SMS message read is an incoming SMS message, the address field contains the originator address. If the SMS message read is an outgoing SMS message, the address field contains the recipient address. Usually the address field value is a phone number formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163). For example, "+85291234567".

The address_text Field

The third field of the information response of the +CMGR AT command, address_text, is a string that contains the text associated to address in the phonebook. For example, if the text "Alice" is associated to the phone number "91234567" in the phonebook, address_text will be "Alice". The AT command +CSCS (command name in text: Select TE Character Set) can be used to specify the character set for displaying address_text. Note that address_text is an optional field. Some GSM/GPRS modems and mobile phones (examples: most Nokia products, including my Nokia 6021) leave this field empty.

The service_center_time_stamp Field

The fourth field of the +CMGR information response for incoming SMS messages,service_center_time_stamp, is a string that contains the time and date at which the SMS message arrived at the service center (i.e. SMSC). The time stamp format is "yy/MM/dd,hh:mm:ss±zz" (yy = year, MM = month, dd = day, hh = hour, mm = minute, ss = second, zz = time zone [Note: the unit of time zone is a quarter of an hour]). For example, "07/02/05,08:30:45+32" represents 5 February 2007, 8:30:45 GMT+8 hours. (+32 means GMT+8 hours, since 32 quarters of an hour = 8 hours.)

Note that service_center_time_stamp is an optional field. This field is available to incoming SMS messages but not to outgoing SMS messages since outgoing SMS messages have not gone through an SMSC.

The address_type Field

The fifth (fourth) field of the +CMGR information response for incoming SMS messages (outgoing SMS messages), address_type, is an integer that indicates the type of the address contained in the addressfield. Usually it is one of the following two values:

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".

Note that address_type is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the fieldaddress_type will be shown in the information response of the +CMGR AT command.

The TPDU_first_octet Field

The sixth (fifth) field of the +CMGR information response for incoming SMS messages (outgoing SMS messages), TPDU_first_octet, contains the first octet (expressed as an integer) of the TPDU of the SMS message.

Note that TPDU_first_octet is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the fieldTPDU_first_octet will be shown in the information response of the +CMGR AT command.

The protocol_identifier Field

The seventh (sixth) field of the +CMGR information response for incoming SMS messages (outgoing SMS messages), protocol_identifier, contains the value (expressed as an integer) of the TP-Protocol-Identifier parameter in the TPDU of the SMS message.

Note that protocol_identifier is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the fieldprotocol_identifier will be shown in the information response of the +CMGR AT command.

The data_coding_scheme Field

The eighth (seventh) field of the +CMGR information response for incoming SMS messages (outgoing SMS messages), data_coding_scheme, contains the value (expressed as an integer) of the TP-Data-Coding-Scheme parameter in the TPDU of the SMS message.

Note that data_coding_scheme is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the fielddata_coding_scheme will be shown in the information response of the +CMGR AT command.

The validity_period Field

The eighth field of the +CMGR information response for outgoing SMS messages, validity_period, contains the validity period assigned to the SMS message. There are three possible validity period formats: relative format, absolute format and enhanced format. To find out the format of validity_period, examine the value of the TP-Validity-Period-Format parameter in the TPDU of the SMS message. It is located at bit 4 and bit 3 of the first octet of the TPDU. (Note: Bit 0 is the rightmost bit.) For example, let's say the value of the TPDU_first_octet Field is 17. Its binary representation is 00010001. As you can see, bit 4 is 1 and bit 3 is 0.

If the relative format is used (bit 4 = 1, bit 3 = 0), validity_period will be an integer in the range from 0 to 255 that indicates the period after which the SMSC should delete the SMS message, counted from the time at which the SMSC receives the SMS message. The validity period can be calculated by the following formulae:

If validity_period is in the range from 0 to 143, validity period = (validity_period + 1) x 5 minutes.

If validity_period is in the range from 144 to 167, validity period = 12 hours + ((validity_period - 143) x 0.5 hours).

If validity_period is in the range from 168 to 196, validity period = (validity_period - 166) x 1 day.

If validity_period is in the range from 197 to 255, validity period = (validity_period - 192) x 1 week.

If the absolute format is used (bit 4 = 1, bit 3 =1), validity_period will be a string that contains the time and date at which the SMSC should delete the SMS message. The time/date format is "yy/MM/dd,hh:mm:ss±zz" (yy = year, MM = month, dd = day, hh = hour, mm = minute, ss = second, zz = time zone [Note: the unit of time zone is a quarter of an hour]). For example, "07/02/05,08:30:45+32" represents 5 February 2007, 8:30:45 GMT+8 hours. (+32 means GMT+8 hours, since 32 quarters of an hour = 8 hours.)

If the enhanced format is used (bit 4 = 0, bit 3 = 1), validity_period will be a hexadecimal coded string. Since the enhanced format is not commonly used, we do not discuss the details here.

Note that validity_period is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the fieldvalidity_period will be shown in the information response of the +CMGR AT command. Also, note thatvalidity_period is available to outgoing SMS messages but not to incoming SMS messages, since there is no such value stored in the headers of incoming SMS messages.

The service_center_address Field

The ninth field of the information response of the +CMGR AT command, service_center_address, is a string that contains the SMSC address stored with the SMS message. Usually it is a phone number formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163). For example, "+85291234567".

Note that service_center_address is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the fieldservice_center_address will be shown in the information response of the +CMGR AT command.

The service_center_address_type Field

The tenth field of the information response of the +CMGR AT command, service_center_address_type, is an integer that indicates the type of the SMSC address contained in the service_center_address field. Usually it is one of the following two values:

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

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

Note that service_center_address_type is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the field service_center_address_type will be shown in the information response of the +CMGR AT command.

The sms_message_body_length Field

The eleventh field of the information response of the +CMGR AT command, sms_message_body_length, is an integer that indicates the number of characters contained in the sms_message_body field. For example, there are 48 characters in "It is easy to read text messages via AT commands". If the value of the sms_message_body field is "It is

easy to read text messages via AT commands", the value of thesms_message_body_length field will be 48.

Note that sms_message_body_length is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the field sms_message_body_length will be shown in the information response of the +CMGR AT command.

The sms_message_body Field

The twelfth field of the information response of the +CMGR AT command, sms_message_body, contains the body of the SMS message.

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=1OK

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 andsms_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=1OK

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,49It is easy to read text messages via AT commands.

+CMGR: "STO UNSENT","+85291234567",,145,17,0,0,167,"+85290000000",145,39A 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

26.2.4. Syntax of the +CMGR AT Command in SMS PDU Mode

The syntax of the +CMGR AT command in SMS PDU mode is the same as that in SMS text mode:

+CMGR=index

In the above line, index is an integer specifying the location of the SMS message to be read from the message storage area.

26.2.5. Format of the Information Response of the +CMGR AT Command in SMS PDU Mode

If the GSM/GPRS modem or mobile phone reads the SMS message from message storage successfully, it will return an information response to the computer / PC. In SMS PDU mode, the information response of the +CMGR AT command has the following format: (Optional fields are enclosed in square brackets.)

+CMGR: message_status,[address_text],TPDU_length<CR><LF>SMSC_number_and_TPDU

Before we discuss each of the fields that appear in the +CMGR information response, let's see an example that gives you some idea of how an actual +CMGR information response should look like:

+CMGR: 0,,6207915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

OK

The message_status Field

The first field of the information response of the +CMGR AT command, message_status, is an integer that indicates the status of the SMS message. It can be one of the following four values:

0. It refers to the message status "received unread". 1. It refers to the message status "received read".

2. It refers to the message status "stored unsent".

3. It refers to the message status "stored sent".

The address_text Field

The second field of the information response of the +CMGR AT command, address_text, is a string that contains the text associated to address in the phonebook, where address is the phone number encoded in the TPDU of the SMSC_number_and_TPDU field. For example, if the phone number encoded in the TPDU is "91234567" and the text "Alice" is associated to the phone number "91234567" in the phonebook, address_text will be "Alice". The AT command +CSCS (command name in text: Select TE Character Set) can be used to specify the character set for displaying address_text.

Note that address_text is an optional field. Some GSM/GPRS modems and mobile phones (examples: most Nokia products, including my Nokia 6021) leave this field empty.

The TPDU_length Field

The third field of the information response of the +CMGR AT command, TPDU_length, is an integer that indicates the length (in octets. 1 octet = 8 bits) of the TPDU contained in the SMSC_number_and_TPDUfield. In the earlier example command line, the value of the SMSC_number_and_TPDU field is:

07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

It can be divided into two parts. The following part is the TPDU:

040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

The TPDU is coded in hexadecimal format. Each character represents 4 bits, i.e. 1/2 octet. The TPDU has 124 characters and so there are totally 62 octets. That's why the value of the TPDU_length field is 62.

The SMSC_number_and_TPDU Field

The fourth field of the information response of the +CMGR AT command, SMSC_number_and_TPDU, specifies the SMSC number and the TPDU (Transfer Protocol Data Unit) in hexadecimal format. If the SMS message to be read is an incoming SMS message, the TPDU will be of the type SMS-DELIVER. If the SMS message to be read is an outgoing SMS message, the TPDU will be of the type SMS-SUBMIT.

26.2.6. Some Explanation about the Decoding of the SMSC_number_and_TPDU Field Value of the +CMGR AT Command

This section provides some explanation about the decoding of the SMSC_number_and_TPDU field value of the +CMGR AT command so that you can extract the information you need from it. Let's consider theSMSC_number_and_TPDU field value in the earlier example command line:

07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

The above value can be divided into two parts, as shown below. The first part contains information about the SMSC. The second part is the TPDU.

07915892000000F0

040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

26.2.6.1. Incoming SMS Messages and Outgoing SMS Messages

The SMS message discussed above is an incoming SMS message. The first part indicates which SMSC the SMS message was received from, and the second part is a TPDU of the type SMS-DELIVER. If the SMS message retrieved by the +CMGR AT command is an outgoing SMS message, the first part indicates which SMSC will be used to send the SMS message, and the second part is a TPDU of the type SMS-SUBMIT, whose format is different from that of an SMS-DELIVER TPDU.

The format of the SMSC_number_and_TPDU field value of an outgoing SMS message is the same as that mentioned in the earlier section "Some Explanation about the Coding of the SMSC_number_and_TPDU Parameter Value of the +CMGS AT Command". Thus, we will not discuss about it again here. In the following sections, we assume the SMS message retrieved by the +CMGR AT command is an incoming SMS message and the TPDU is of the type SMS-DELIVER.

26.2.6.2. The SMSC Part

The SMSC part can be further divided into three sub-fields, like this:

07 91 5892000000F0

The First Sub-field: Length of the Second and Third Sub-fields

The first sub-field specifies the length in octets of the following two sub-fields. There are 14 hexadecimal digits in "915892000000F0" and each hexadecimal digit represents 4 bits, i.e. 1/2 octet. So, there are totally 7 octets. That's why the value of the first sub-field is 0x07.

The Second Sub-field: Type of SMSC Number

The second sub-field specifies the type of the SMSC number assigned to the third sub-field. Two values are commonly used. They are 0x81 (129 in decimal) and 0x91 (145 in decimal):

0x81. It means the SMSC number is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) but it is not sure whether the SMSC number is an international number, a national number or a number of other types. For example, suppose the SMSC number from which the SMS message was received is

"+85290000000". If the value of the second sub-field is 0x81, the SMSC number assigned to the third sub-field may be "85290000000" (country code included) or "90000000" (country code omitted).

0x91. It means the SMSC number is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) and it is an international number. For example, suppose the SMSC number from which the SMS message was received is "+85290000000". If the value of the second sub-field is 0x91, the SMSC number assigned to the third sub-field should be "85290000000".

The Third Sub-field: SMSC Number

The third sub-field specifies the SMSC number from which the SMS message was received. 0x5892000000F0 represents the phone number +85290000000. Here's how the value 0x5892000000F0 is obtained:

1. Starting from the left, group the digits of the phone number 85290000000 into pairs, like this: 85 29 00 00 00 0.

2. As the last group has only one digit, we add an "F" to make up a pair. The result is 85 29 00 00 00 0F.

3. Swap the digits in each pair and you will get 58 92 00 00 00 F0.

26.2.6.3. The TPDU Part (SMS-DELIVER TPDU)

The TPDU part can be divided into nine sub-fields, as shown below. This is a TPDU of the type SMS-DELIVER.

04 0B 91 5892214365F7 00 00 70402132522423 31 493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

The First Sub-field: First Octet of the TPDU

The first sub-field is the first octet of the TPDU. It tells us several things:

the type of the TPDU whether the SMSC has messages that are waiting to be sent to us

whether a reply path exists

whether a user data header exists in the TPDU

whether a status report is requested by the sender of the SMS message

The value 0x04 means:

the type of the TPDU is SMS-DELIVER the SMSC has no message that is waiting to be sent to us

no reply path exists

no user data header exists in the TPDU

no status report is requested by the sender of the SMS message

The Second Sub-field: Length of the Sender Phone Number

The second sub-field specifies the length in digits of the sender phone number. The sender phone number specified in the fourth sub-field is "+85291234567", which has 11 digits. Thus, the value of the second sub-field is 0x0B (i.e. 11 in decimal).

The Third Sub-field: Type of the Sender Phone Number

The third sub-field specifies the type of the sender phone number assigned to the fourth sub-field. Two values are commonly used. They are 0x81 (129 in decimal) and 0x91 (145 in decimal):

0x81. It means the sender phone number is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) but it is not sure whether the sender phone number is an international number, a national number or a number of other types. For example, suppose there is an incoming SMS message that was originated from the phone number "+85291234567". If the value of the third sub-field is 0x81, the sender phone number assigned to the fourth sub-field may be "85291234567" (country code included) or "91234567" (country code omitted).

0x91. It means the sender phone number is formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163) and it is an international number. For example, suppose there is an incoming SMS message that was originated from the phone number "+85291234567". If the value of the third sub-field is 0x91, the sender phone number assigned to the fourth sub-field should be "85291234567".

The Fourth Sub-field: Sender Phone Number

The fourth sub-field specifies the sender phone number. The value 0x5892214365F7 represents the phone number +85291234567. Here's how the value 0x5892214365F7 is obtained:

Starting from the left, group the digits of the phone number 85291234567 into pairs, like this: 85 29 12 34 56 7.

As the last group has only one digit, we add an "F" to make up a pair. The result is 85 29 12 34 56 7F.

Swap the digits in each pair and you will get 58 92 21 43 65 F7.

The Fifth Sub-field: Protocol Identifier

The fifth sub-field specifies the protocol identifier. Its value should be 0x00 for normal cases.

The Sixth Sub-field: Data Coding Scheme

The sixth sub-field specifies the data coding scheme. The value 0x00 indicates that the text in the SMS message body is encoded according to the "GSM 7-bit default alphabet" text coding scheme.

The Seventh Sub-field: Service Center Time Stamp

The seventh sub-field specifies the time and data at which the SMS message arrived at the service center (i.e. SMSC). The value 0x70402132522423 represents 12 April 2007, 23:25:42 GMT+8 hours. Here's how the value 0x70402132522423 is decoded:

Starting from the left, group the digits of the value 0x70402132522423 into pairs, like this: 70 40 21 32 52 24 23.

Swap the digits in each pair and you will get 07 04 12 23 25 42 32. Starting from the left, the values represent year, month, day, hour, minute, second and time zone respectively. Note that the unit of time zone is a quarter of an hour.

The decoding of the time zone field requires a bit more explanation. The binary representation of the hexadecimal value 0x32 is 00110010. It can be divided into three parts, like this:

0 011 0010

The first part represents the plus-minus sign. If the value of the first part is 0, the sign is "+". If it is 1, the sign is "-". The second part represents the most significant digit of the time zone. "011" is equal to 3 in decimal. The third part represents the least significant digit of the time zone. "0010" is equal to 2 in decimal. Overall, "00110010" represents GMT+32 quarters of an hour = GMT+8 hours.

The Eighth Sub-field: Length of the SMS Message Body

The eighth sub-field specifies the length of the SMS message body in septets (1 septet = 7 bits). The value 0x31 means there are 49 septets (or characters, since each character is represented

by 7 bits according to the "GSM 7-bit default alphabet" text coding scheme) in the SMS message body.

The Ninth Sub-field: SMS Message Body

The ninth sub-field specifies the SMS message body. The value below:

0x493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

represents the text message "It is easy to read text messages via AT commands.". Below shows how to convert the hexadecimal value to text:

Group the digits of the hexadecimal value into pairs, as shown below. Each pair represents one octet of data.

49 3A 28 3D 07 95 C3 F3 3C 88 FE 06 C9 CB 61 32 88 5E C6 D3 41 ED F2 7C 1E 3E 97 E7 20 7B 3A 0C 0A 52 41 E3 77 BB 1D 76 93 E7 2E

The binary representation of the first octet is 01001001. With the character set "GSM 7-bit default alphabet", each character is represented by 7 bits. The seven least significant bits, 1001001, represent the first character. The hexadecimal value of 1001001 is 0x49, which represents the character "I" according to the character set "GSM 7-bit default alphabet". To find out which character is represented by a certain hexadecimal code, see "Appendix: GSM 7-bit Default Alphabet Table (with Character Codes of ISO 8859 Latin 1)" of this SMS tutorial.

The binary representation of the second octet is 00111010. Now move the most significant bit of the first octet to the rightmost position of the second octet. The result is 001110100. The seven least significant bits, 1110100, represent the second character. The hexadecimal value of 1110100 is 0x74, which represents the character "t" according to the character set "GSM 7-bit default alphabet".

Similarly, move the two unused bits at the leftmost position of the second octet to the rightmost position of the third octet. The binary representation of the third octet is 00101000 and so the result is 0010100000. The seven least significant bits, 0100000, represent the third character. The hexadecimal value of 0100000 is 0x20, which represents the space character according to the character set "GSM 7-bit default alphabet".

The unpacking process continues. This is illustrated in the following table.

Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Character 1

1 0 0 1 0 0 1 = 0x49

Character 2

1 1 1 0 1 0 0 = 0x74

Character 3

0 1 0 0 0 0 0 = 0x20

Character 4

1 1 0 1 0 0 1 = 0x69

Character 5

1 1 1 0 0 1 1 = 0x73

Character 6

0 1 0 0 0 0 0 = 0x20

Character 7

1 1 0 0 1 0 1 = 0x65

Character 8

1 1 0 0 0 0 1 = 0x61

Character 9

1 1 1 0 0 1 1 = 0x73

Character 10

1 1 1 1 0 0 1 = 0x79

Character 11

0 1 0 0 0 0 0 = 0x20

Character 12

1 1 1 0 1 0 0 = 0x74

Character 13

1 1 0 1 1 1 1 = 0x6F

Character 14

0 1 0 0 0 0 0 = 0x20

Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Character 15

1 1 1 0 0 1 0 = 0x72

Character 16

1 1 0 0 1 0 1 = 0x65

Character 17

1 1 0 0 0 0 1 = 0x61

Character 18

1 1 0 0 1 0 0 = 0x64

Character 19

0 1 0 0 0 0 0 = 0x20

Character 20

1 1 1 0 1 0 0 = 0x74

Character 21

1 1 0 0 1 0 1 = 0x65

Character 22

1 1 1 1 0 0 0 = 0x78

Character 23

1 1 1 0 1 0 0 = 0x74

Character 24

0 1 0 0 0 0 0 = 0x20

Character 25

1 1 0 1 1 0 1 = 0x6D

Character 26

1 1 0 0 1 0 1 = 0x65

Character 27

1 1 1 0 0 1 1 = 0x73

Character 28

1 1 1 0 0 1 1 = 0x73

Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Character 29

1 1 0 0 0 0 1 = 0x61

Character 30

1 1 0 0 1 1 1 = 0x67

Character 31

1 1 0 0 1 0 1 = 0x65

Character 32

1 1 1 0 0 1 1 = 0x73

Character 33

0 1 0 0 0 0 0 = 0x20

Character 34

1 1 1 0 1 1 0 = 0x76

Character 35

1 1 0 1 0 0 1 = 0x69

Character 36

1 1 0 0 0 0 1 = 0x61

Character 37

0 1 0 0 0 0 0 = 0x20

Character 38

1 0 0 0 0 0 1 = 0x41

Character 39

1 0 1 0 1 0 0 = 0x54

Character 40

0 1 0 0 0 0 0 = 0x20

Character 41

1 1 0 0 0 1 1 = 0x63

Character 42

1 1 0 1 1 1 1 = 0x6F

Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Character 43

1 1 0 1 1 0 1 = 0x6D

Character 44

1 1 0 1 1 0 1 = 0x6D

Character 45

1 1 0 0 0 0 1 = 0x61

Character 46

1 1 0 1 1 1 0 = 0x6E

Character 47

1 1 0 0 1 0 0 = 0x64

Character 48

1 1 1 0 0 1 1 = 0x73

Character 49

0 1 0 1 1 1 0 = 0x2E

0

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)

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=0OK

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,,6207915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

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,,6207915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

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

26.3. Using the +CMGL AT Command to List SMS Messages of a Certain Status from a Message Storage Area

The AT command +CMGL (command name in text: List Messages) is used to list messages of a certain status (for example, "received unread" and "received read") from a message storage area. The messages listed by the AT command +CMGL do not necessarily have to be SMS messages. They can be of other message types such as status reports and cell broadcast messages, but we will only focus on SMS messages here.

In SMS text mode, you can retrieve the following information about an SMS message with the AT command +CMGL:

The index specifying the location of the SMS message in the message storage area. Status of the SMS message ("received unread", "received read", "stored unsent", "stored

sent", etc).

Originator/recipient phone number stored in the SMS message header and the type of the phone number.

Text associated to the originator/recipient phone number in the phonebook.

Time and date at which the SMS message arrived at the SMSC (available to incoming SMS messages only).

Body of the SMS message and its length.

In SMS PDU mode, the list becomes:

The index specifying the location of the SMS message in the message storage area. Status of the SMS message ("received unread", "received read", "stored unsent", "stored

sent", etc).

Text associated to the originator/recipient phone number in the phonebook.

SMSC number stored with the SMS message and the type of the SMSC number.

TPDU of the SMS message and its length.

If the status of a message is "received unread", after being retrieved by the +CMGL AT command, the status is changed to "received read".

The message storage area from which SMS messages are read 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.

26.3.1. Syntax of the +CMGL AT Command in SMS Text Mode

The syntax of the +CMGL AT command in SMS text mode is: (Optional parameter is enclosed in square brackets.)

+CMGL[=message_status]

The message_status parameter takes a string that specifies the message status to be used to find messages to return. The SMS specification has defined five message status values, which are shown below. You can perform a test operation to find the actual values supported by your GSM/GPRS modem or mobile phone. See the "Finding the Message Status Values Supported by the +CMGL AT Command in SMS Text Mode" section for the details. Usually all five status values are supported.

REC UNREAD. It refers to the message status "received unread". It is the default value. REC READ. It refers to the message status "received read".

STO UNSENT. It refers to the message status "stored unsent".

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

ALL. It tells the +CMGL AT command to list all messages.

For example, if "REC READ" is specified to the message_status parameter, the +CMGL AT command will list all messages that have the "received read" status from the message storage area.

Note that message_status is an optional parameter. If it is omitted, the +CMGL AT command will use the default value "REC UNREAD".

26.3.2. Format of the Information Response of the +CMGL AT Command in SMS Text Mode

If the GSM/GPRS modem or mobile phone reads the SMS messages from message storage successfully, it will return an information response to the computer / PC. Suppose the messages to be listed are SMS messages (not of other message types like status reports and cell broadcast messages), the information response of the +CMGL AT command in SMS text mode has the following format: (Optional fields are enclosed in square brackets.)

+CMGL:index,message_status,address,[address_text],[service_center_time_stamp][,address_type,sms_message_body_length]<CR><LF>sms_message_body[<CR><LF>+CMGL: ...]

Before we discuss each of the fields that appear in the +CMGL information response, let's see an example that gives you some idea of how an actual +CMGL information response should look like:

+CMGL: 1,"REC READ","+85291234567",,"07/05/01,08:00:15+32",145,37It is easy to list SMS text messages.

The index Field

The first field of the information response of the +CMGL AT command, index, is an integer that specifies the location of the SMS message in the message storage area.

The message_status Field

The second field of the information response of the +CMGL AT command, message_status, is a string that indicates the status of the SMS message. It can be one of the following four 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".

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

The address Field

The third field of the information response of the +CMGL AT command, address, is a string that contains the address/phone number stored in the SMS message header. If the SMS message read is an incoming SMS message, the address field contains the originator address. If the SMS message read is an outgoing SMS message, the address field contains the recipient address. Usually the address field value is a phone number formatted using the typical ISDN / telephony numbering plan (ITU E.164/E.163). For example, "+85291234567".

The address_text Field

The fourth field of the information response of the +CMGL AT command, address_text, is a string that contains the text associated to address in the phonebook. For example, if the text "Alice" is associated to the phone number "91234567" in the phonebook, address_text will be "Alice". The AT command +CSCS (command name in text: Select TE Character Set) can be used to specify the character set for displaying address_text. Note thataddress_text is an optional field. Some GSM/GPRS modems and mobile phones (examples: most Nokia products, including my Nokia 6021) leave this field empty.

The service_center_time_stamp Field

The fifth field of the information response of the +CMGL AT command, service_center_time_stamp, is a string that contains the time and date at which the SMS message arrived at the service center (i.e. SMSC). The time stamp format is "yy/MM/dd,hh:mm:ss±zz" (yy = year, MM = month, dd = day, hh = hour, mm = minute, ss = second, zz = time zone [Note: the unit of time zone is a quarter of an hour]). For example, "07/02/05,08:30:45+32" represents 5 February 2007, 8:30:45 GMT+8 hours. (+32 means GMT+8 hours, since 32 quarters of an hour = 8 hours.)

Note that service_center_time_stamp is an optional field. This field is available to incoming SMS messages but not to outgoing SMS messages, since outgoing SMS messages have not gone through an SMSC.

The address_type Field

The sixth field of the information response of the +CMGL AT command, address_type, is an integer that indicates the type of the address contained in theaddress field. Usually it is one of the following two values:

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".

Note that address_type is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the field address_type will be shown in the information response of the +CMGL AT command.

The sms_message_body_length Field

The seventh field of the information response of the +CMGL AT command, sms_message_body_length, is an integer that indicates the number of characters contained in the sms_message_body field. For example, there are 36 characters in "It is easy to list SMS text messages". If the value of thesms_message_body field is "It is easy to list SMS text messages", the value of the sms_message_body_length field will be 36.

Note that sms_message_body_length is an optional field and it is not shown by default. The AT command +CSDH (command name in text: Show Text Mode Parameters) can be used to control whether the field sms_message_body_length will be shown in the information response of the +CMGL AT command.

The sms_message_body Field

The eighth field of the information response of the +CMGL AT command, sms_message_body, contains the body of the SMS message.

Multiple SMS Messages in the Information Response

If there are multiple SMS messages to be returned, the information response of the +CMGL AT command will contain more than one "+CMGL: ...". Two characters, <CR><LF>, are used to delimit two "+CMGL: ...". Here is an example for illustration. Suppose the command line 'AT+CMGL="ALL"' is executed and three SMS messages are listed. If all characters are shown, the command line and the response returned will be displayed like this:

AT+CMGL="ALL"<CR><CR><LF>+CMGL: ...<CR><LF>+CMGL: ...<CR><LF>+CMGL: ...<CR><LF><CR><LF>OK<CR><LF>

In a terminal program such as HyperTerminal of Microsoft Windows, a carriage return character causes the cursor to be moved to the beginning of the current line, and a linefeed character causes the cursor to be moved to the same position on the next line. Hence, the above result will be displayed like this in a terminal program such as HyperTerminal of Microsoft Windows:

AT+CMGL="ALL"+CMGL: ...+CMGL: ...+CMGL: ...

OK

26.3.3. Finding the Message Status Values Supported by the +CMGL AT Command in SMS Text Mode

As mentioned earlier, the +CMGL AT command takes one parameter, message_status. Performing a test operation with the +CMGL AT command allows you to find all message status values that are supported by the message_status parameter. Here is the syntax of the information response returned after the execution of the test command "+CMGL=?":

+CMGL: (message_status1,message_status2,...)

In SMS text mode, the values inside the above parentheses (message_status1, message_status2, etc) are of the string type. The SMS specification has defined five message status values: "REC UNREAD", "REC READ", "STO UNSENT", "STO SENT" and "ALL". They are supported by most GSM/GPRS modems and mobile phones. A description of these five message status values can be found in the earlier section "Syntax of the +CMGL AT Command in SMS Text Mode".

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

AT+CMGL=?+CMGL: ("REC UNREAD","REC READ","STO UNSENT","STO SENT","ALL")

OK

From above, we know that the Nokia 6021 mobile phone supports all of the five message status values defined in the SMS specification.

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)

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=1OK

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+CMGLOK

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=1OK

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

AT+CMGL="ALL"+CMGL: 1,"STO SENT","+85291234567",,,145,35Hello, welcome to our SMS tutorial.+CMGL: 2,"REC READ","+85291234567",,"07/02/18,00:05:10+32",145,36A simple demo of SMS text messaging.+CMGL: 3,"REC READ","+85291234567",,"07/02/18,00:07:22+32",145,37It 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.

26.3.5. Syntax of the +CMGL AT Command in SMS PDU Mode

The syntax of the +CMGL AT command in SMS PDU mode is: (Optional parameter is enclosed in square brackets.)

+CMGL[=message_status]

The message_status parameter takes an integer that specifies the message status to be used to find messages to return. The SMS specification has defined five message status values, which are shown below. You can perform a test operation to find the actual values supported by your GSM/GPRS modem or mobile phone. See the "Finding the Message Status Values Supported by the +CMGL AT Command in SMS PDU Mode" section for the details. Usually all five status values are supported.

0. It refers to the message status "received unread". It is the default value. 1. It refers to the message status "received read".

2. It refers to the message status "stored unsent".

3. It refers to the message status "stored sent".

4. It tells the +CMGL AT command to list all messages.

For example, if 1 is specified to the message_status parameter, the +CMGL AT command will list all messages that have the "received read" status from the message storage area.

Note that message_status is an optional parameter. If it is omitted, the +CMGL AT command will use the default value 0.

26.3.6. Format of the Information Response of the +CMGL AT Command in SMS PDU Mode

If the GSM/GPRS modem or mobile phone reads the SMS message from message storage successfully, it will return an information response to the computer / PC. In SMS PDU mode, the information response of the +CMGL AT command has the following format: (Optional fields are enclosed in square brackets.)

+CMGL: index,message_status,[address_text],TPDU_length<CR><LF>SMSC_number_and_TPDU[<CR><LF>+CMGL: ...]

Before we discuss each of the fields that appear in the +CMGL information response, let's see an example that gives you some idea of how an actual +CMGL information response should look like:

+CMGL: 1,0,,6207915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

The index Field

The first field of the information response of the +CMGL AT command, index, is an integer that specifies the location of the SMS message in the message storage area.

The message_status Field

The second field of the information response of the +CMGL AT command, message_status, is an integer that indicates the status of the SMS message. It can be one of the following four values:

0. It refers to the message status "received unread". 1. It refers to the message status "received read".

2. It refers to the message status "stored unsent".

3. It refers to the message status "stored sent".

The address_text Field

The third field of the information response of the +CMGL AT command, address_text, is a string that contains the text associated to address in the phonebook, where address is the phone number encoded in the TPDU of the SMSC_number_and_TPDU field. For example, if the phone number encoded in the TPDU is "91234567" and the text "Alice" is associated to the phone number "91234567" in the phonebook, address_text will be "Alice". The AT command +CSCS (command name in text: Select TE Character Set) can be used to specify the character set for displaying address_text.

Note that address_text is an optional field. Some GSM/GPRS modems and mobile phones leave this field empty. (Examples: Philips 598 supports this field, while most Nokia products, including my Nokia 6021, and Sony Ericsson T68i does not.)

The TPDU_length Field

The fourth field of the information response of the +CMGL AT command, TPDU_length, is an integer that indicates the length (in octets. 1 octet = 8 bits) of the TPDU contained in the SMSC_number_and_TPDUfield. In the earlier example command line, the value of the SMSC_number_and_TPDU field is:

07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

It can be divided into two parts. The following part is the TPDU:

040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

The TPDU is coded in hexadecimal format. Each character represents 4 bits, i.e. 1/2 octet. The TPDU has 124 characters and so there are totally 62 octets. That's why the value of the TPDU_length field is 62.

The SMSC_number_and_TPDU Field

The fifth field of the information response of the +CMGL AT command, SMSC_number_and_TPDU, specifies the SMSC number and the TPDU (Transfer Protocol Data Unit) in hexadecimal format.

If the SMS message to be read is an incoming SMS message, the value of the SMSC_number_and_TPDU field will be something like this:

07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

The TPDU embedded in the above hexadecimal sequence is of the type SMS-DELIVER. Here is some of the information encoded in the hexadecimal sequence:

SMSC number from which the SMS message was received: +85290000000 Sender phone number: +85291234567

Time and date at which the SMSC received the SMS message: 12 April 2007, 23:25:42 GMT+8 hours

Text message: "It is easy to read text messages via AT commands."

For the details about how the hexadecimal sequence of an incoming SMS message is coded, please refer to 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.

If the SMS message to be read is an outgoing SMS message, the value of theSMSC_number_and_TPDU field will be something like this:

07915892000000F001000B915892214365F7000021493A283D0795C3F33C88FE06CDCB6E32885EC6D341EDF27C1E3E97E72E

The TPDU embedded in the above hexadecimal sequence is of the type SMS-SUBMIT. Its format is different from that of an SMS-DELIVER TPDU. Below shows some of the information encoded in the hexadecimal sequence above:

SMSC number to be used if the SMS message is sent out: +85290000000 Destination phone number: +85291234567

Text message: "It is easy to send text messages."

For the details about how the hexadecimal sequence of an outgoing SMS message is coded, please refer to the section titled "Some Explanation about the Coding of the SMSC_number_and_TPDU Parameter Value of the +CMGS AT Command" of this SMS tutorial.

Multiple SMS Messages in the Information Response

If there are multiple SMS messages to be returned, the information response of the +CMGL AT command will contain more than one "+CMGL: ...". Two characters, <CR><LF>, are used to delimit two "+CMGL: ...". Here is an example for illustration. Suppose the command line "AT+CMGL=4" (this command line tells the GSM/GPRS modem or mobile phone to retrieve all messages from message storage) is executed and three SMS messages are listed. If all characters are shown, the command line and the response returned will be displayed like this:

AT+CMGL=4<CR><CR><LF>+CMGL: ...<CR><LF>+CMGL: ...<CR><LF>+CMGL: ...<CR><LF><CR><LF>OK<CR><LF>

In a terminal program such as HyperTerminal of Microsoft Windows, a carriage return character causes the cursor to be moved to the beginning of the current line, and a linefeed character causes the cursor to be moved to the same position on the next line. Hence, the above result will be displayed like this in a terminal program such as HyperTerminal of Microsoft Windows:

AT+CMGL=4+CMGL: ...+CMGL: ...+CMGL: ...

OK

26.3.7. Finding the Message Status Values Supported by the +CMGL AT Command in SMS PDU Mode

As mentioned earlier, the +CMGL AT command takes one parameter, message_status. Performing a test operation with the +CMGL AT command allows you to find all message status values that are supported by the message_status parameter. Here is the format of the information response returned after the execution of the test command "+CMGL=?":

+CMGL: (list_of_status_values)

In SMS PDU mode, the values inside the above parentheses are integers. The SMS specification has defined five message status values: 0, 1, 2, 3 and 4. They are supported by most GSM/GPRS modems and mobile phones. A description of these five message status values can be found in the earlier section "Syntax of the +CMGL AT Command in SMS PDU Mode".

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

AT+CMGL=?+CMGL: (0-4)

OK

From above, we know that the Nokia 6021 mobile phone supports all of the five message status 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)".

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

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 PDU 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 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=0OK

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 +CMGL, 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.

Listing SMS Text Messages

Third, use the +CMGL AT command to list SMS text messages from the message storage area. Suppose you would like to retrieve all new SMS messages received. As the status of new inbound SMS messages is "received unread", you should specify 0 to the +CMGL AT command, as shown below:

AT+CMGL=0

Since 0 is the default parameter value, you can simply omit it, like this:

AT+CMGL

Now press the Enter key on the keyboard and the GSM/GPRS modem or mobile phone will execute the command line "AT+CMGL". If successful, it returns all messages that have the "received 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,0,,5107915892000000F0040B915892214365F70000701010221555232441D03CDD86B3CB2072B9FD06BDCDA069730AA297F17450BB3C9F87CF69F7D905+CMGL: 3,0,,6207915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

OK

From above, we know that the message storage area contains two new inbound SMS text messages. Let's take the first SMS text message as an example to illustrate the meaning of different field values in the +CMGL information response above:

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, 0, indicates the status of the text message is "received unread".

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, 51, tells us the TPDU encoded in the hexadecimal sequence consists of 51 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 of an inbound SMS text message 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.

For the decoding of the hexadecimal sequence of an outgoing SMS text message, please refer to the "Some Explanation about the Coding of the SMSC_number_and_TPDU Parameter Value of the +CMGS AT Command" section of this SMS tutorial.

The final result code OK informs us that the SMS text messages were read from message storage 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.

After using the +CMGL AT command to list the SMS text messages, their status was changed from "received unread" to "received read". So, if the command line "AT+CMGL" is executed again, no text message will be returned. Here shows the response returned from Philips 598:

AT+CMGLOK

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

To see the two text messages again, specify either 1 or 4 to the +CMGL AT command. The value 1 instructs +CMGL to list all messages that have the "received read" status from message storage, while the value 4 instructs +CMGL to list all messages from message storage. The response returned from the GSM/GPRS modem or mobile phone is slightly different from the earlier one. The changes are highlighted in bold type below:

AT+CMGL=1+CMGL: 2,1,,5107915892000000F0040B915892214365F70000701010221555232441D03CDD86B3CB2072B9FD06BDCDA069730AA297F17450BB3C9F87CF69F7D905+CMGL: 3,1,,6207915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E

OK