Currently, text and PDU (Protocol Data Unit) are commonly used for sending short messages. Use text mode to send and receive short message code simple, easy to achieve, but the biggest drawback is not to send and receive Chinese text messages, and PDU mode not only support Chinese text messages, but also send English text messages. PDU mode send and receive SMS can use 3 kinds of encoding: 7-bit, 8-bit and UCS2 encoding. 7-bit encoding is used to send ordinary ASCII characters, 8-bit encoding is typically used to send data messages, and UCS2 encodings are used to send Unicode characters. The general PDU code consists of a B C D E F G H I J m 13.
A: SMS Center address length, 2-bit hexadecimal (1-byte).
B: Short Message Center number type, 2-digit hexadecimal number.
C: The SMS Center number, the length of the b+c will be determined by the data in a.
D: File header Byte, 2-bit hexadecimal number.
E: Information type, 2-bit hexadecimal number.
F: Called number length, 2-digit hexadecimal number.
G: Called number type, 2-digit hexadecimal number, value same as B.
H: Called number, length is determined by the data in F.
I: Protocol ID, 2-bit hexadecimal number.
J: Data encoding scheme, 2-bit hexadecimal number.
K: Valid, 2-digit hexadecimal number.
L: User data length, 2-bit hexadecimal number.
M: User data, whose length is determined by the data in L. J is set to use UCS2 encoding, here is the Unicode character in English and Chinese.
PDU Coding Protocol Simple Description
Example 1: smsc number is +8613800250500 , the opposite number is 13693092030 , the message content is "hello!." The PDU string from the mobile phone can be a
A/F0 0D-A/9B FD 0E 01 Control specification, Specific analysis:
-sub-list Paragraph meaning description
08 The length of the address information A total of 8 eight-bit bytes (including)
91 address format (TON/NPI) in international format number (in front plus ' + ')
F0 address 8613800250500, complement ' F ' together into even several
11 basic parameters (TP-MTI/VFP) Send, TP-VP in relative format
00 message base value (TP-MR) 0
0D Destination Address number A total of 13 decimal digits (excluding 91 and ' F ')
91 Destination address format (TON/NPI) in international format number (in front plus ' + ')
F0 target Address (TP-DA) 8613693092030, complement ' F ' make up even number of
00 Protocol identification (TP-PID) is common GSM type, point-to-point
00 User Information encoding Method (Tp-dcs) 7-bit encoding
00 validity (TP-VP) 5 minutes
06 User information Length (tp-udl) actual length 6 bytes
C8 9B FD 0E 01 user information (Tp-ud) "Hello!" smsc smsc smsc The
Example 2 receive: SMSC number is +8613800250500 , the opposite number is 13693092030 , message content is " Hello! ". The PDU string received by the mobile phone can be a
F0 a 0D from F0 to the 7D 00 21 for all of the same in the same. According to the specification, specific analysis:
paragraph meaning description
08 The length of the address information eight-bit bytes (including a.)
91 address format (TON/NPI) with international format number (in front plus ' + ')
68 31 F0 address 8613800250500, add ' F ' to even several
84 basic parameters (TP-MTI/MMS/RP) Receive, no more messages, have reply address
0D reply address number A total of 13 decimal digits (excluding 91 and ' F ')
91 reply address format (TON/NPI) Use international format number (in front plus ' + ')
F0 reply to address (Tp-ra) 8613693092030, complement ' F ' make up even number of
00 Protocol identification (TP-PID) is common GSM type, point-to-point
08 User Information encoding Method (Tp-dcs)  UCS2 encoding
30 30 21 80 63 54 80 Timestamp (tp-scts) 2003-3-12 08:36:45 + 8 time zone
06 User information Length (tp-udl) actual length 6 bytes
4F 7D 00 21 user information (Tp-ud) "Hello!" smsc smsc
If the highest bit (TP-RP) of the basic parameter is 0, there is no three segment of the reply address. This is often the case with short messages sent from the Internet.
Note that the number and time representations are not followed in the normal order, and that the odd numbers are added to the "F" to even.
In PDU mode, you can encode the content that you send by using three encodings, which are 7-bit, 8-bit, and UCS2 encodings. The 7-bit encoding is used to send a generic ASCII character that encodes a string of 7-bit characters (up to 0) into 8-bit data, each 8 character "compressed" into 7; 8-bit encoding is often used to send data messages, such as pictures and ringtones. , and the UCS2 encoding is used to send Unicode characters. The maximum capacity of the user information (Tp-ud) segment of the PDU string is 140 bytes, so the maximum number of characters that can be sent in the three encoding modes are 160, 140, and 70, respectively. Here, an English letter, a Chinese character, and a data byte are treated as one character.
It is important to note that the user information length (tp-udl) of the PDU string is different in different coding modes. 7-bit encoding refers to the number of characters of the original short message, not the number of bytes encoded. 8-bit encoding is the number of bytes. When UCS2 is encoded, it is also a byte number, equal to twice times the number of characters in the original short message. If there is a header in the user information (Tp-ud) (The Tp-udhi of the basic parameter is 1), the user information length (tp-udl) is equal to the sum of the length of the header and the number of bytes encoded in the encoding mode. If the proposed compression algorithm for GSM 03.42 (Tp-dcs 3 bits is 001), the length is also the sum of bytes after compression encoding or the length of the header and the number of bytes after compression.
The above text describes a bit of empty feeling, below with a small demo example to explain. Demo's function is to get to send SMS PDU code.
Send a text message, need mobile phone number and SMS service center, this example and later explain all to China Mobile phone number development as an example. The Mobile SMS Service center number is 13800100500.
View the PDU Press the external button code as follows:
Code 1 String _mobilenumber = "12345678901"; Mobile phone number
2 string _content = "Test"; SMS Same Volume
3 string Centerno = "13800100500";
4 string length = string. Empty;
5 string SMSTOSENDPDU = String. Empty;
6 length = Pduencoding.countsmslength (_mobilenumber, _content);
7 SMSTOSENDPDU = pduencoding.pduencodingsms (Centerno, _mobilenumber, _content);
8 String str = "at+cmgs=" + length + "/r" + SMSTOSENDPDU + "/x01a"; Instruction format
9 Tbsmspducode.text = str;
10
The relevant methods are as follows:
Code 1///<summary>
2///Compute Message length
3///</summary>
4///<param name= "_PhoneNumber" > Phone </param>
5///<param name= "_targettext" > Context </param>
6///<returns></returns>
7 public static string Countsmslength (String _phonenumber, String _targettext)
8 {
9 if (! (_phonenumber.substring (0, 2) = = "86"))
10 {
One _phonenumber = String.Format ("86{0}", _PhoneNumber); Check whether the phone number is written in the standard format, no, and then make up ""
12}
Return (8 + (_phonenumber.length% 2) = = 0? _PHONENUMBER.LENGTH/2: (_phonenumber.length + 1)/2) + _targettext.length * 2). ToString ();
14}
15
///<summary>
17///The entire PDU code to send the text message
///</summary>
///<param name= "Cscanumber" ></param>
///<param name= "PhoneNumber" ></param>
///<param name= "Targettext" ></param>
///<returns></returns>
public static string Pduencodingsms (String _cscanumber, String _PhoneNumber, String _targettext)
24 {
if (! (_cscanumber.substring (0, 2) = = "86"))
26 {
_cscanumber = String.Format ("86{0}", _cscanumber); Check whether the phone number is written in the standard format, no, and then make up ""
28}
if (! (_phonenumber.substring (0, 2) = = "86"))
30 {
_PhoneNumber = String.Format ("86{0}", _PhoneNumber); Check whether the phone number is written in the standard format, no, and then make up ""
32}
33//Start number
StringBuilder _SB = new StringBuilder ();
_SB. Append ("0891");
36//SMS Center
Panax Notoginseng _SB. Append (Reversalnumber (_cscanumber));
&nbs