PHP via the serial port to send SMS, PHP serial text message _php tutorial

Source: Internet
Author: User
Tags dio

PHP via the serial port to send text messages, PHP serial port to send text messages


With the progress of technology, the field of text messaging has produced three modes in time: BLOCK mode, the text mode based on the AT command, and the PDU mode based on the AT command. The TEXT mode is relatively simple, and a variety of Nokia phones support this model. Most Siemens phones only support PDU MODE. PDU mode is a method of sending and receiving text messages, and text message text is sent by hexadecimal encoding. Currently, the PDU has replaced Block MODE.

SMS is a specification developed by ETSI (GSM 03.40 and GSM 03.38). When using 7-bits encoding, it can send up to 160 characters, but with 8-bit encoding, you can send up to 140 characters, usually not directly from the phone, and 16-bit encoding, a maximum of 70 characters, is used to display Unicode (UCS2) text information, Can be displayed by most mobile phones.

Today we are talking about PDU MODE,UCS2 encoding, which means that you can send up to 70 characters, whether in English or Chinese.
Suppose you now send the following message: "Hello". Before sending, you should know the SMS center number where the SIM card is located, such as the Mobile SMS Center number:

Phone number received: 13638197275
Hangzhou SMS Center No.: 13800571500
Message content: Hello
Send this text message, to be encoded after the phone will be executed, encoding will become the following string of characters:
0891683180501705f011000d91683136187972f5000800044f60597d
If you don't understand it, explain this string of codes from beginning to end:
08– refers to the length of the message center number, which means (683180501705f0) the length divided by 2, i.e. 08 = (2+14)/2
91– refers to the short information Center number type. 91 is TON/NPI comply with the international/e.164 standard, refers to the number before the ' + ' number, and there are other values, but 91 most commonly used.
683180501705F0-SMS Center number. Because the position is slightly handled, the actual number should be: 8613800571500 (the letter F is a complement of even-length added characters).
11-File Header bytes
00-type of information (tp-message-reference)
0D-called number length
91-called Number type

In fact, in the actual processing, we usually write 11000d91 to die in the program, because at home, the data will not change.

683136187972F5-called number, after the displacement process, the actual number is "8613638197275".

Above (+) + (0D) + (683136187972F5), which constitutes the second part of the entire text message destination address (tp-destination-address).

Go on...
00-Protocol ID Tp-pid, here is generally 00
08-Data encoding scheme Tp-dcs (tp-data-coding-scheme), using the previously mentioned USC2 (16bit) data encoding
00-Validity TP-VP (tp-valid-period)
04-Length Tp-udl (tp-user-data-length), that is, the information length/2 16 into 04
4f60597d here is the text message content, the actual content is: "Hello"

According to the above situation, you can write the text message encoding program script.

First, SMS Center number processing:

1, the Short Information Center number "+8613800571500" to remove the + number, see if the length is even, if not, finally add F
= "8613800571500F"
2, the odd digit and even the number of bits exchange.
= "683108501705f0″
3, the short Information Center number is preceded by a character 91,91 is the meaning of internationalization
= "91683108501705f0″
4, calculate the length, result in addition to 2, formatted into 2 bits of 16 binary string, 16/2 = 8 = "08″
= "0891683108501705f0″

Second, mobile phone number processing:

1, the mobile phone number +8613638197275 to remove the + sign, to see if the length is even, if not, finally add F
= "8613638197275F"
2, the mobile phone number odd digits and even digits exchange.
= "683136187972f5″

Third, SMS part processing:

1. Convert string to Unicode code,
The Unicode code for "Hello" is 4f60597d
2, the length in addition to 2, reserved two bits 16 binary number, namely 4f60597d = 8/2 "04″",
= "044f60597d″

Four, the combination

1, mobile phone number before plus string 11000d91 (1100: Fixed, 0D: the length of the mobile phone number, not counting +, hexadecimal, 91: send
To the phone for 91, sent to PHS for 81),
i.e. 11000d91 + 683136187972f5
= 11000d91683136187972f5
2, mobile phone number plus 000800 and just short message content, 000800 also write dead on it can
i.e. 11000d91683136187972f5 + 000800 + 044f60597d
= 11000d91683136187972f5000800044f60597d
3, the entire information length divided by 2, formatted into 2 decimal digits
namely 11000d91683136187972f5000800044f60597d = 38 bits/2 19

Five, so the content to be sent is

At+cmgf=0 <回车> #此处为设定短信发送模式PDU
Ok
At+cmgs=19 <回车>
> #输入短信内容编码

Append 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 ("Open serial ttyS0 Failed");} Dio_tcsetattr () only Linux//Windows uses 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 was start on ttys0\n"; SMS Center Number $smsc = "8613800571500"; $invert _smsc = invertnumbers ($smsc); Convert SMS Center number $inter = Chr (13); Carriage return character $ctrlz = Chr (26); CTRL + Z//Send message $text = ' Hello '; $send _to = ' 8613638197275 '; $PDU _phone = Hex2str (Utf82unicode ($text)); $PDU _phone = sprintf ("%02x", strlen ($PDU _phone)/2). $PDU _phone; $PDU _phone = ' 11000d91 '. Invertnumbers ($send _to). ' 000800 '. $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  = ' 0891 '. Invertnumbers ($SMSC); $PDU _all = $pdu _addr. $PDU _phone. $ctrlz. $inter; @dio_write ($FD, $PDU _all);   Dio_close ($FD);   Mine is 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;  }?>

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1028971.html www.bkjia.com true http://www.bkjia.com/PHPjc/1028971.html techarticle PHP through the implementation of the serial port to send text messages, PHP serial text messaging with the progress of technology, the field of SMS delivery has produced three modes in time: BLOCK mode, based on the text mode, based on ...

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