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

Preferences - Do not show ads

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=0
OK


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


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

OK


The values in the parentheses indicate the modes that the GSM/GPRS modem or mobile phone can operate in. The 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

Preferences - Do not show ads


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

Feedback Form (Expand)

What do you think about this web page?






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

SMS Gateway Service