Tuesday, February 10, 2009

Sending SMS in C#.NET using GSM Modem and AT(ATtention) Commands.

SMS (Short Message Service )as specified by the ETSI organization (Documents GSM 3.38, 3.40) can be up to 160 characters long.Each character in the message is represented by a seven bit default alphabet.8 bit messages(MAX 140 character) are usually used for sending data's like image and ring tones etc in smart messaging .16 bit messages (70 character) are used for UCS2 text messages in which a 16 bit text message of class zero will appear as a flash SMS or alert SMS in some Phones.It is strongly recommended that you read all the above specified documents.

There are two ways of sending and receiving SMS messages:

1.PDU (Protocol Description Unit) mode,
2.Text Mode(Unavailable on some phones eg: SonyEricsson Cybershot k810i).

It should be noted that there are several encoding alternatives for displaying an SMS message Out of which some of the common options are "PCCP437","PCDN","8859-1","IRA" and "GSM".This are set by AT Commands (AT+CSCS) when trying to read the message using a computer application.If you read the message on your phone ,phone will choose appropriate encoding.
If text mode is used the application is limited by a set of preset encoding options.If PDU mode is used any encoding can be implemented.we explain PDU mode in detail here.

PDU format
The PDU string is a collection of meta information a PDU string would contain information about sender,information about his/her SMS service centre, timestamps ,the actual message itself .The PDU string consist of hexa-decimal octets and decimal semi-octets.The following is an example of a PDU string which represents an message "hellohello":

Sending a Message to in PDU format to phone number 9447537254

The above PDU message consists of the following:

07 ->Length of SMS Service Centre Number. in this case 7 octet.

91 ->Represents international format. If unknown 81 is used.

194924909979 ->Message Centre address in decimal semi-octets.

11 ->First octet of the SMS-SUBMIT message.

00 ->TP Message Reference This value lets the phone set the message reference
number itself.

0C ->Length of Sender Phone number.

91 ->Represents international format of the phone number.

194974352745 -> Represents Sender phone number in decimal semi-octets.

00 ->Default Protocol identifier.

00 ->Default Data Coding scheme.

AA ->TP Validity period. AA-means 4 days note that this octet is optional.

0A ->TP length of message in Hexa-decimal.

E8329BFB4697D9EC37 -> User data in hexa-decimal in this case "hellohello".


Decimal semi octet?

For example Consider the phone number 919447537254.The corresponding decimal semi-octet is calculated by simply swapping each pair as follows :

91 - ie 19
94 - 49
47 - 74
53 - 35
72 - 27
54 - 45

Creating user data ?

The message "hellohello" consist of 10 characters each represented by septets(7 bit each).Before SMS transfer this septets must be transformed in to octets.


First take the decimal equivalent of each character and convert it to 7 bit binary equivalent.

Alphabet ------Decimal----------------Septet

h ---------------------->104 ----------------------------- >1101000
e ---------------------->101 ----------------------------- >1100101
l ---------------------->108 ----------------------------- >1101100
l ---------------------->108 ----------------------------- >1101100
o ---------------------->111 ----------------------------- >1101111
h ---------------------->104 ----------------------------- >1101000
e ---------------------->101 ----------------------------- >1100101
l ---------------------->108 ----------------------------- >1101100
l ---------------------->108 ----------------------------- >1101100
o ---------------------->111 ----------------------------- >1101111

Next convert the 7bit binay to 8 bit binary as follows:

The first septet(h) is turned in to an octet by adding the right most bit of the second septet this bit is inserted to the left which yields 1+110100 =11101000(E8).The right most bit of the second character is then consumed , so the second character (septet) needs two bits of the third charecter to make an 8 bit octet.This process is continued yielding the following octets.(It should be noted that there are only 9 octets for 10 septets).

1101000 ---------------------->11101000 -------------->E8
1100101 ---------------------->00110010-------------->32
1101100 ---------------------->10011011 -------------->9B
1101100 ---------------------->11111101 -------------->FD
1101111 ---------------------->01000110 -------------->46
1101000 ---------------------->10010111 -------------->97
1100101 ---------------------->11011001 -------------->D9
1101100 ----------------------- ----------- -----------------
1101100 ---------------------->11101100 -------------->EC
1101111 ---------------------->110111 ------------------>37

There for the message "hellohello" is converted in to the 9 octets "E8 32 9B FD 46 97 D9 EC 37" .

Useful AT Commands for Sending SMS :

NB: "\r" should be supplied for the command to be executed"

AT\r - Text Communication between phone and any accessories .Determines the presence of a phone

AT+CMGF=0 -> Tells the TA that PDU mode is used. AT+CMGF=? shows if the command is supported by the phone .the response will be of the form +CMGF:(List of Supporting Modes eg:0,1)
AT+CSCA? -> Command for retrieving message centre address.The response will be of the form +CSCA: followed by the message centre address.
AT+CMGS - >AT command for sending message from a TE to the network (SMS-SUBMIT).The command should specify the length of actual TP data unit in octet (excluding SMSC address octet) followed by the actual PDU string.(eg: AT+CMGS="length in octet"\r"Actual PDU" Ctrl^ Z. The decimal equivalent of Ctrl ^Z is 26 , simply convert it to char at the end of PDU string)

Receiving SMS in PDU format

Receiving SMS is a little bit tricky .The following Decoding process may be followed:

DOWNLOAD sample project here


cellphone apllication development cum website development hosting kerala cochin

Useful links:


  1. Hi, I couldn't find any C# nor .NET in your post. Did I missed out the something?

  2. thanks, it's really useful!

  3. Thanks for a really useful bit of code. I've been programming C for 30 years. Bit shifting, which is easy in C, is a pain in C# so I've been putting this off until I was in the right mood. You've just saved me the trouble.
    Thanks again.

  4. Thanks for this great piece of code. I've been putting off doing just this for a few weeks now. You've just saved me.
    Thanks again.

  5. Thanks for this great piece of code. I've been putting off doing just this for a few weeks now. You've just saved me.
    Thanks again

  6. Hi,Thank you for giving best idea..I have run this code but message is not send to mobile.What can i do for that .Please send detail to my mail id : sundaravadivelno1@gmail.com

    Thank You

  7. Can you please send me the code at saha.sudipan@gmail.com

  8. Thanks a lot for giving these good lines of code

  9. i didn't got any design in c# when i open it in Visual Studio 2005... Plz help

  10. Hi..Thanks a lot for the code but i couldnt able to send the messages .Can u plz send me the code to SakthisKumar@gmail.com



Related Posts with Thumbnails