PHP sends text messages via serial port

Source: Internet
Author: User
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. PDU mode PHP sends 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), where xxx is the local phone area number. --- The telephone area number is a three-digit region: replace xxx with the telephone area number. For example, the phone number in Shenzhen is 755, and the number in the mobile short message center is + 8613800755500 --- the phone number is in two regions: add "0" after the area number to replace xxx. For example, the Beijing telephone area code is 10, and the mobile short message center number is: + 8613800100500. Currently, China Unicom 165 has launched 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 short message AT + CMGR read short message AT + CMGS send short message AT + CMGL list short messages in SIM card AT + CMSS send short messages from SIM memory AT + CMGW write short messages to SIM memory AT + CMGD delete short messages in SIM memory AT + CSCB select cell broadcast information
Appendix 3: The receipt and receipt of short messages is essentially reading information from the SIM or cache. This mainly uses the AT + CMGR and AT + CMGL commands. because different manufacturers of the wireless module interpret the AT instruction set code and response information differently, 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 the data format of the short message is selected using the AT + CMGF Command; after receiving the correct answer from the MODEM, use the AT command to complete the reading function. Generally, AT + CMGL is used to read the previous information. when receiving the RING (RING) data of the MODEM, AT + CMGR is used to read the real-time information. The following is an instance of receiving SMS with a H6221-W that illustrates the application of the PDU mode. The procedure is as follows (note in {}): Send: AT answer: OK {established connection} 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 that no messages are sent, 24 indicates total information capacity} refers to the hexadecimal string of the above PDU format. it not only contains the content of short messages, but also contains the sender's number, SMS center number, and short message 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, which means to add the '+' number before the number; in addition, other values can be directly included, but 91 is the most commonly used. 683104370105f0: The service center number 13807310500 used by SMSC short messages. It is converted into bytes in hexadecimal notation. The number is an odd number added to F, forming 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 consists of a 7-bit ASCII code shift and an 8-bit hexadecimal code (octet). The method is shown in Table 2. 1 sthex B0 A6 A5 A4 A3 A2 A1 A02ndhex C1 C0 B6 B5 B4 B3 B13rdhex D2 D1 D0 C6 C5 C4 C3 C24thhex E3 E2 E1 E0 D6 D5 D4 D35thhex F4 F3 F2 F1 F0 E6 e5 E46thhex G5 G4 G3 G2 G1 F6 F56thhex H6 H5 H4 H3 H2 H1 H0 G602608041003380: 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. Warning: UD-encoded PDU data. The short message content is "2002/06/08/13: 48ID102OKID103OK ID201FAIL ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.