PHP sends text via serial port, PHP sends text via serial port

Source: Internet
Author: User
Tags dio

PHP sends text via serial port, PHP sends text 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 command, and pdu mode Based on AT command. 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.

The pdu mode and UCS2 encoding are discussed today. 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:
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-The called number has been displaced. 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 ).

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, that is, 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 <press enter> # Set the SMS sending mode PDU here
AT + CMGS = 19 <press enter>
> # Enter the text message content encoding

Add the final PHP code:

<? Php // Requirement dio, use cmd install: pecl install dio set_time_limit (0); // Windows use COM1: $ fd = dio_open ('/dev/ttys0', O_RDWR ); if (! $ Fd) {die ("failed to open the serial port ttyS0");} // dio_tcsetattr () only Linux // use exec ('mode COM1: baud = 9600 data = 8 stop = 1 parity = n xon = on '); dio_tcsetattr ($ fd, array ('baud' => 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 character $ Ctrlz = chr (26); // ctrl + z // send message $ text = 'hao'; $ send_to = '2016 '; $ pdu_phone = hex2str (utf82unicode ($ text); $ pdu_phone = sprintf ("% 02X", strlen ($ pdu_phone)/2 ). $ pdu_phone; $ 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 = '2013 '. invertNumbers ($ smsc); $ pdu_all = $ pdu_addr. $ pdu_phone. $ ctrlz. $ inter; @ dio_write ($ fd, $ pdu_all); dio_close ($ fd); // my 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);} retur N $ 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: Enter the short message number of the local mobile text center: + 8613800xxx500 ("+" must be entered), 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 ".

Related Article

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