PHP through the serial port to send SMS _php tips

Source: Internet
Author: User
Tags chr dio sprintf

With the technology progress, the field of SMS transceiver has produced three modes in time: Block mode, text mode based on at instruction, PDU mode based on at instruction. Among them, TEXT mode is relatively simple, a variety of Nokia phones are supported this model. Most of Siemens's mobile phones only support PDU MODE. PDU mode is a way to send and receive text messages, text messages are encoded by the hexadecimal code is transmitted. 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, up to 70 characters, is used to display Unicode (UCS2) text information, Can be displayed by most mobile phones.

Today we're talking about PDU MODE,UCS2 encoding, which means that you can send up to 70 characters, whether in English or Chinese.
Suppose you want to send the following message now: "Hello". Before you send it, know the message center number for your SIM card location, such as the Mobile SMS Center number:

Mobile number Received: 13638197275
Hangzhou SMS Center No.: 13800571500
SMS Content: Hello
To send this message, the phone will not execute until it is encoded, and the code will become the following string of characters:
0891683180501705f011000d91683136187972f5000800044f60597d
Don't get it, explain this string of code from beginning to end:
08– refers to the length of the message center number, which means that the length of (683180501705f0) + is divided by 2, or 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 need to add ' + ' number, in addition to other values, but 91 most commonly used.
683180501705F0-Short Message center number. Because the position is slightly processed, the actual number should be: 8613800571500 (the letter F is the complement of even-length added characters).
11-File Header bytes
00-Information Type (tp-message-reference)
0D-called number length
91-called Number type

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

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

The above (+) + (0D) + ($) + (683136187972F5) forms the second part of the entire text message's destination address (tp-destination-address).

Go on...
00-Protocol Identification Tp-pid, here is generally 00
08-Data coding Scheme Tp-dcs (tp-data-coding-scheme), using the USC2 (16bit) data coding described above
00-Valid TP-VP (Tp-valid-period)
04-Length Tp-udl (tp-user-data-length), i.e. information length/2 of 16 in 04
4f60597d here is the text message content, the actual content is: "Hello"

Based on the above, you can write a text message coding program script.

First, the Message Center number processing:

1, the Short Information Center number "+8613800571500" remove the + number, see whether the length is even, if not, finally add F
=> "8613800571500F"
2, the odd digit and even digit bit exchange.
=> "683108501705f0″
3, the short Information Center number before the addition of characters 91,91 is the meaning of internationalization
=> "91683108501705f0″
4, the calculation of the length, the result in addition to 2, formatted into 2-bit 16-string, 16/2 = 8 => "08″
=> "0891683108501705f0″

Second, mobile phone number processing:

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

Third, the short message part of the processing:

1. Convert string to Unicode code,
"Hello" Unicode code for 4F60597D
2, the length of the addition of 2, to retain two digits 16, that is 4f60597d = 8/2 => "04″,
=> "044f60597d″

Iv. Combination

1, mobile phone number before the string 11000d91 (1100: fixed, 0D: Cell phone number length, not counted +, hexadecimal, 91: send
To mobile for 91, sent to PHS for 81),
namely 11000d91 + 683136187972F5
=> 11000d91683136187972f5
2, mobile phone number plus 000800 and just the text message content, 000800 also write to death on the
namely 11000d91683136187972f5 + 000800 + 044f60597d
=> 11000d91683136187972f5000800044f60597d
3, the entire piece of information length divided by 2, formatted into 2 decimal digits
i.e. 11000d91683136187972f5000800044f60597d => 38-bit/2 => 19

Five, so to send the content is

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

Append 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 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 are 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 information $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; 
  The 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 is the entire contents of this article, I hope you can enjoy.

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.