1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.
Ads by Lake Quincy Media

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
==============================================================
079119492490997911000C911949743527450000AA0AE8329BFB4697D9EC37

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


Description
===========

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.

Example
======

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

Septet-----------------Octet----------------Hex
==============================================
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

by,

cellphone apllication development cum website development hosting kerala cochin

Useful links:
http://www.dreamfabric.com/sms/
http://www.developershome.com/sms/atCommandsIntro.asp

38 comments:

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

    ReplyDelete
  2. thanks, it's really useful!

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

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

    ReplyDelete
  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

    ReplyDelete
  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
    By

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

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

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

    ReplyDelete
  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

    ReplyDelete
  11. i tried ur code. but it shows that try another port. phone not detected. i think i have problem with my connections..... can u plz help to connect modem with serial port
    my id is denuk21@gmail.com

    ReplyDelete
  12. does sony ericsson k608i support text mode or pdu

    ReplyDelete
  13. hi great aritcle........

    ReplyDelete
  14. Thanks a lot it was very useful......

    ReplyDelete
  15. hi...
    i have run the code.....but message not send to mobile....what can i do...send me source code with description about how to use it..my mail id is
    gunaselanr@gmail.com

    thanks,
    regrads,
    Gunaseelan R

    ReplyDelete
  16. hi.......

    i tried ur project..The mobile is connected successfully. but message not to send mobile , what can i do? send mail me for how to use ur project with detailed description.......

    gunaselanr@gmail.com

    thanks,
    regrads,
    guna

    ReplyDelete
  17. Nice Blog Friend

    Funny Sms
    www.esmsfun.com

    ReplyDelete
  18. Hey I tried you code, and its realy very usefull to me, it works perfectly, thank you very much.

    Bhausaheb.

    ReplyDelete
  19. To know more about SMS visit our new site
    www.codeglobe.in

    Article
    http://codeglobe.in/desktop-development/cnet/15-sending-sms-in-cnet.html

    ReplyDelete
  20. Thanks, but I think this code has to be optimized before sharing.
    You can write

    lenn = bb.ToString("X2");

    instead of

    lenn = bb.ToString("X");
    if (lenn.Length == 1)
    {
    lenn = "0" + lenn;
    }

    Method string SWAP(string str) should use Array.Reverse instead of 10 lines of (LOL) code.

    And so on...

    ReplyDelete
  21. congratulations. this article about beats every other i've seen on the web on SMS PDU. Thank you so much.

    ReplyDelete
  22. VERY VERY THANKS YOU FOR YOUR VALUEABLE CODE AND SUPPORT ON THIS IDEA...

    BY
    LENIN PAILY
    leninpaily@yahoo.co.in

    ReplyDelete
  23. PLS TELL ME ASP.NET CODE ON hem_sendme@rediffmail.com

    ReplyDelete
  24. superb man! u r the champ! i run your code and working perfectly

    ReplyDelete
  25. Really Thanks
    I did it in java but u really saved my time making it in C#

    Really Thanks Man :)

    ReplyDelete
  26. how to send to multiple mobile no. with single PDU string ?

    ReplyDelete
  27. // converts plain text string to pdu format
    public static string ConvertToPdu(string strPhone, string strMessage)
    {
    // trim extra characters
    strPhone = strPhone.Trim("+".ToCharArray());

    // build message header
    string strPdu = "00"; // length of message centre number (00 - implicit)
    strPdu += "11"; // first octet for message deliver
    strPdu += "00"; // tp message reference number
    strPdu += String.Format("{0:X2}", strPhone.Length); // length of receiver phone number
    strPdu += "91"; // 91 for international format, 81 for unknown
    strPdu += ToSemiOctet(strPhone); // recipient number in decimal half-octet
    strPdu += "00"; // protocol identifier
    strPdu += "00"; // octet coding scheme
    strPdu += "AA"; // AA (validity period set to 4 days)
    strPdu += String.Format("{0:X2}", strMessage.Length / 2); // message length

    // convert message to pdu format
    strPdu += ToOctet(strMessage);
    return strPdu;
    }

    // converts string to septet then to octet format
    public static string ToOctet(string strText)
    {
    // loop through text characters
    List arrTemp = new List();
    for (int i = 0; i < strText.Length; i++)
    {
    // convert to septet binary
    byte szTemp = Convert.ToByte(strText[i]);
    string strBinary = Convert.ToString(szTemp, 2).PadLeft(7, '0');
    arrTemp.Add(strBinary);
    }

    // loop through septet list
    for (int i = 0; i < arrTemp.Count - 1; i++)
    {
    // check string if consumed
    if (arrTemp[i] == "")
    continue;

    // get septet strings
    string strTemp1 = arrTemp[i];
    string strTemp2 = arrTemp[i + 1];

    // take bits from next septet to form octet
    int iSep = strTemp2.Length - (8 - strTemp1.Length);
    arrTemp[i] = strTemp2.Substring(iSep) + strTemp1;
    arrTemp[i + 1] = strTemp2.Substring(0, iSep);
    }

    // loop through octet list
    string strPdu = "";
    foreach (string strTemp in arrTemp)
    {
    // append to octet string
    if (strTemp != "")
    strPdu += String.Format("{0:X2}", Convert.ToInt32(strTemp, 2));
    }
    return strPdu;
    }

    // converts decimal to half octet
    public static string ToSemiOctet(string strText)
    {
    // apply padding if length is odd
    if (strText.Length % 2 != 0)
    strText += "F";

    // swap characters
    string strTemp = "";
    for (int i = 0; i < strText.Length - 1; i++)
    {
    // swap every two characters
    strTemp += strText.Substring(i + 1, 1) + strText.Substring(i, 1);
    i++;
    }
    return strTemp;
    }

    ReplyDelete
  28. hi can u send the entire code of this project plz its very urjent can u send me immediately to anitha.14@reddifmail.com.plz send.............

    ReplyDelete
  29. HOW TO CHANGE SENDER ID WITH PDU

    ReplyDelete
  30. Thanks alot.. been searchng for this for long time. Now just to get the delivery report. Anyone ideas?

    ReplyDelete
  31. thanks worked great.. any idea how to get delivery status?

    ReplyDelete
  32. I am bagesh kumar singh.

    can any one send me code for the sending SMS in asp.net..........
    please send it at bageshkumarbagiasp.net@gmail.com

    ReplyDelete
  33. Mobile phone connected successfully but the message not send can you share me working code, my id us aminbinwali@yahoo.com

    ReplyDelete
  34. yOUR r REALLY GOOD MAN THANKS FOR SUCH AN EFFECTIVE AND EFFICIENT CODE

    ReplyDelete
  35. hi this is geetha.. can you send the sending SMS in asp.net code to my id.. geetha.r14@gmail.com

    ReplyDelete

Related Posts with Thumbnails