PHP sends text messages through serial port

Source: Internet
Author: User
Tags dio
This article gives you a detailed description of how to use php to send text messages through a serial port and the specific implementation code. It is very practical. If you need it, you can refer to it.

This article gives you a detailed description of how to use php to send text messages through a serial port and the specific implementation code. It is very practical. If you need it, you can refer to it.

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:
0891683180501705F011000D91683136187972F5000800044F60597D
I can't understand it. I will explain the encoding from start to end:
08-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-indicates 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-length of the callednumber
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 processing, the actual number is "8613638197275 ".

The above (00) + (0D) + (91) + (683136187972F5) constitutes the second part of the Destination Address (TP-Destination-Address) of the entire text message ).

Continue...
00-Protocol Identification TP-PID, which is generally 00
08-Data encoding Scheme TP-DCS (TP-Data-Coding-Scheme), using the previously mentioned USC2 (16bit) Data Encoding
00-Period TP-VP (TP-Valid-Period)
04-Length TP-UDL (TP-User-Data-Length), that is, the Length of information/2 of the hexadecimal 04
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 <回车> # Set the SMS sending mode PDU.
OK
AT + CMGS = 19 <回车>
> # 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 string $ ctr Lz = chr (26); // ctrl + z // send message $ text = 'your'; $ 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, $ atcm D); $ pdu_addr = '000000 '. 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);} return $ str;} functio N 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 ;}?>

The above is all the content of this article. I hope you will like it.

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.