PHP sends text messages through serial ports with technological advances. in the text message sending and receiving field, three modes are successively created: BLOCKMODE, TEXTMODE based on AT commands, and PDUMODE based on AT commands. Among them, TEXTMODE is relatively simple, and multiple Nokia mobile phones support this mode. Most Siemens mobile phones only support PDUMODE. The PDU mode is a method for sending and receiving text messages. The text message is sent after hexadecimal encoding. At present, PDU have replaced B PHP by sending text messages via serial port
As technology advances, the TEXT message sending and receiving field has successively produced three modes: block mode, text mode based on AT commands, and PDUMODE based on AT commands. Text mode is relatively simple, and multiple Nokia mobile phones support this MODE. Most Siemens mobile phones only support pdu mode. The PDU mode is a method for sending and receiving text messages. The text message is sent after hexadecimal encoding. Currently, PDU has replaced block mode.
SMS is a specification developed by Etsi (GSM 03.40 And GSM 03.38 ). When 7-bits encoding is used, it can send a maximum of 160 characters. However, it is 8-bit encoded and can send a maximum of 140 characters, which is usually not displayed on a mobile phone; it can contain up to 70 characters in 16-bit encoding and is used to display Unicode (UCS2) text information, which can be displayed on most mobile phones.
???? We are talking about PDUMODE, UCS2 encoding. that is to say, a maximum of 70 characters can be sent, whether in English or Chinese.
???? Suppose you want to send the following message: "Hello ". Before sending a message, you must know the SMS center number of the SIM card, for example, the mobile SMS center number:
?? ?? Received mobile phone number: 13638197275
?? ?? Hangzhou SMS Center: 13800571500
?? ?? SMS content: Hello
??? To send this text message, the mobile phone will only be executed after encoding, and the encoding will become the following string of characters:
0891683180501705F011000D91683136187972F5000800044F60597D
?? I can't understand it. I will explain the encoding from start to end:
?? ??08-It refers to the length of the SMS center number, that is, the length of (91) + (683180501705F0) divided by 2, that is, 08 = (2 + 14)/2
?? ??91-It refers to the short message center number type. 91 indicates that TON/NPI complies with the International/E.164 standard and requires the plus sign (+) before the number. In addition, there are other values, but 91 is the most commonly used.
?? ???683180501705F0? -SMS center number. The actual number should be 8613800571500 (the letter F is a character that supplements the length of an even number ).
?? ???11-File header bytes
?? ???00-Information type (TP-Message-Reference)
?? ???0D-Called number length
?? ???91-Called number type
In actual processing, we usually write 11000D91 into the program, because in China, the data will not change.
?? ???683136187972F5-? Called number. after displacement, the actual number is "8613638197275 ".
??? The above (00) + (0D) + (91) + (683136187972F5), which constitutes the second part of the Destination Address (TP-Destination-Address) of the entire text message ).
??? Continue...
?? ?00-Protocol identifier TP-PID, which is generally 00
?? ?08-Data encoding Scheme TP-DCS (TP-Data-Coding-Scheme), using the previously mentioned USC2 (16 bit) Data encoding
?? ?00-Validity Period TP-VP (TP-Valid-Period)
???04?-Length TP-UDL (TP-User-Data-Length), that is, the Length of information/The hexadecimal 04 of 2
??? 4F60597D? Here is the text message content. the actual content is: "Hello"
?? Based on the above situation, you can write a script for text message encoding.
1. handling of SMS center numbers:
1. remove the "+ 8613800571500" text message center number and check whether the length is an even number. if not, add F
=> "8613800571500F"
2. exchange odd and even digits.
=> "683104501705f0 ″
3. adding 91,91 to the front of the SMS center number indicates internationalization.
=> "91683425501705f0 ″
4. calculate the length. in addition to 2, the result is formatted as a two-digit hexadecimal string, 16/2 = 8 => "08 ″
=> "0891683425501705f0 ″
II. mobile phone number processing:
1. remove the mobile phone number + 8613638197275 + and check whether the length is an even number. if not, add F
=> "8613638197275F"
2. exchange odd and even numbers of mobile phone numbers.
=> "683136187972F5 ″
III. partial handling of short messages:
1. convert string to Unicode code,
"Hello" unicode code is 4F60597D
2. Divide the length by 2 and retain two hexadecimal numbers, namely? 4F60597D? = 8/2 => "04 ″,
=> "044F60597D ″
IV. combination
1. add the character string 11000D91 (1100: Fixed, 0D: the length of the mobile phone number, not counting the number +, expressed in hexadecimal format, 91: Sent
91 on the mobile phone and 81 on the phone ),
11000D91 +? 683136187972F5
=> 11000D91683136187972F5
2. after the mobile phone number is added with 000800 and the short message content just now, 000800 can also be written to death.
11000D91683136187972F5? + 000800 + 044F60597D
=>? 11000D91683136187972F5000800044F60597D
3. Divide the length of the entire information by 2 and format it into two decimal digits.
That is, 11000D91683136187972F5000800044F60597D? => 38 bits/2 => 19
5. the content to be sent is
AT + CMGF = 0 <回车> # Set the SMS sending mode PDU.
OK
AT + CMGS = 19 <回车>
> # Enter the text message content encoding
Add the final PHP code:
9600, 'bits '=> 8, 'stop' => 1, 'parity' => 0); // $ ff = dio_stat ($ fd ); // print_r ($ ff); // echo "gsm at is start on ttyS0 \ n"; // SMS center number $ smsc = "8613800571500 "; $ invert_smsc = invertNumbers ($ smsc); // Convert the SMS center number $ inter = chr (13); // enter the string $ ctrlz = chr (26 ); // ctrl + z // send message $ text = ''; $ send_to = '000000'; $ pdu_phone = hex2str (utf82unicode ($ text )); $ pdu_phone = sprintf ("% 02X", strlen ($ pdu_phone)/2 ). $ pdu_pho Ne; $ pdu_phone = '11000d91 '. invertNumbers ($ send_to ). '123 '. $ pdu_phone; $ atcmd = 'at + CMGF = 0 '. $ inter; @ dio_write ($ fd, $ atcmd); $ atcmd = 'at + CMGS = '. sprintf ("% d", strlen ($ pdu_phone)/2 ). $ inter; @ dio_write ($ fd, $ atcmd); $ pdu_addr = '20180101 '. invertNumbers ($ smsc); $ pdu_all = $ pdu_addr. $ pdu_phone. $ ctrlz. $ inter; @ dio_write ($ fd, $ pdu_all); dio_close ($ fd); // I am a UTF-8 encoded function utf82unicode ($ str ){ Return iconv ("UTF-8", "UCS-2BE", $ str);} function hex2str ($ hexstring) {$ str = ''; for ($ I = 0, $ len = strlen ($ hexstring); $ I <$ len; $ I ++) {$ str. = sprintf ("% 02X", ord (substr ($ hexstring, $ I, 1) ;}return $ str ;} function invertNumbers ($ msisdn) {$ len = strlen ($ msisdn); if (0! = Fmod ($ len, 2) {$ msisdn. = "F"; $ len = $ len + 1;} for ($ I = 0; $ I <$ len; $ I + = 2) {$ t = $ msisdn [$ I]; $ msisdn [$ I] = $ msisdn [$ I + 1]; $ msisdn [$ I + 1] = $ t ;} return $ msisdn ;}?>
?
Appendix 1: Number of China Mobile SMS Center
Enter the short message number of the local mobile board:
+ 8613800xxx500 ("+" is required ),
Xxx is the local telephone area code.
--- The telephone area number is in three regions:
Replace xxx with the telephone area code.
For example, the phone number in Shenzhen is 755,
The number of the mobile short message center is: + 8613800755500
--- The telephone area number is in two regions:
Add "0" after the area code to replace xxx with three digits.
For example, the Beijing telephone area code is 10,
The number of the mobile short message center is: + 8613800100500
Currently, China Unicom's 165 Network has launched its services nationwide.
Before using the text message service, you must set the SMS Center Service Number:
Beijing + 8613010112500
Shanghai + 8613010314500
Shenzhen + 8613010888500
Shandong + 8613010171500
Jiangsu + 8613010341500
Zhejiang + 8613010360500
Fujian + 8613010380500
Sichuan: + 8613010811500
Chongqing + 8613010831500
Hainan + 8613010501500
Heilongjiang + 8613010980500
Jilin + 8613010911500
Tianjin + 8613010130500
Hebei 8613010180500 +
Inner Mongolia + 8613010950500
Shanxi + 8613010701500
Anhui 8613010305500 +
Xinjiang + 8613010969500
Qinghai 8613010776500 +
Gansu + 8613010879500
Ningxia 8613010796500 +
Guizhou + 8613010788500
Yunnan 8613010868500 +
Hunan + 8613010731500
Hubei + 8613010710500
Guangdong 8613010200500 +
Guangxi 8613010591500 +
Henan + 8613010761500
Jiangxi + 8613010720500
Liaoning + 8613010240500
Appendix 2: Common AT commands:
AT + CSMS select short message service
AT + CPMS select short message memory
AT + CMGF select short message format
AT + CSCA short message center address
AT + CNMI display new messages
AT + CMGR read short message
Send short messages AT + CMGS
AT + CMGL list short messages in SIM card
AT + CMSS sends short messages from SIM memory
AT + CMGW writes messages to the SIM memory to be sent.
AT + CMGD delete short messages in SIM memory
AT + CSCB select cellular broadcast information
Appendix 3: Receive short messages
Receiving short messages is essentially reading information from the SIM or cache. This mainly uses AT + CMGR and AT + CMG
L two commands are used to complete the AT instruction set. because different manufacturers of the wireless module have different interpretation codes and response information for the AT instruction set,
Therefore, you must first confirm whether the communication can be established with the MODEM. Generally, the AT command is used to complete the confirmation, and then AT + CM is used.
The GF Command selects the data format of the short message. after receiving the correct answer from the MODEM, the AT command completes the reading function.
Generally, AT + CMGL is used to read the previous information. when receiving the RING (RING) data of the MODEM, AT + CM is used.
GR reads real-time information. The following is an example of receiving SMS with a H6221-W that illustrates the effect of the PDU mode
.
The procedure is as follows (note in ):
Send:
Answer: OK {established connections}
Send: AT + CMGF = 0 {select PDU format}
Answer: OK {select PDU format}
Send: AT + CMGL = 2 {list existing messages}
Answer: + CMGL: 1, 2, 24 {1 indicates the number of messages, 2 indicates the number of messages not sent, and 24 indicates the total information capacity}
0D71683108370105F004000D81683179133208F10000026080410033802632184C
F682D
95E0DC2B36D3D170A0243106933D97A0243106933D97A02451068B1983492608
OK
The above group of PDU format hexadecimal strings not only contain the content of short messages, but also contain the sender
Number, SMS center number, and SMS sending time.
The following information is analyzed:
0D: the length of the SMS center address (number.
91: The number type of the SMS center. 91 is TON/NPI. TON/NPI complies with International/E.164 standards,
Add the '+' sign before the number. In addition, other values can be directly added, but 91 is the most commonly used.
683104370105f0: The service center number 13807310500 used by SMSC short messages. It goes through 16
The hexadecimal structure is a HEX byte, which is a HEX byte.
04: PDU type, file header bytes.
0B: the length of the caller's number.
81: Caller ID.
3179133208F1: 0A caller number. the actual number is 13973123801.
00: PID, which is the protocol identifier.
00: The DCS short message encoding type is GSM Default Alphabet, which is composed of 8 digits by 7-bit ASCII code shift.
The octet method is shown in Table 2.
1 sthex B0 A6 A5 A4 A3 A2 A1 A0
2 ndhex C1 C0 B6 B5 B4 B3 B2 B1
3 rdhex D2 D1 D0 C6 C5 C4 C3 C2
4 thhex E3 E2 E1 E0 D6 D5 D4 D3
5 thhex F4 F3 F2 F1 F0 E6 E5 E4
6 thhex G5 G4 G3 G2 G1 G0 F6 F5
6 thhex H6 H5 H4 H3 H2 H1 H0 G6
02608041003380: SCTS short message sending time, 02/06/08/14. 08.
26: The length of the octet short message after processing by UDL, which is smaller than the ASCII code of the message.
32184CF682D95E30DC2B36D3D170A0243106933D97A0243106933D97A0245106
8B1983492608: UD-encoded PDU data, short message content "2002/06/08/13: 48ID102OKID103
OK ID201FAIL ".