PHP through the serial text message, PHP serial text message _php tutorial

Source: Internet
Author: User
Tags dio

PHP through the serial text message, PHP serial text message

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:
If you don't understand it, explain this string of codes from beginning to end:
-refers to the length of the message center number, which means (683180501705f0) + (the length of the () + () divided by 2, i.e. 08 = (2+14)/2
– 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).
One-file header bytes
XX -Information type (tp-message-reference)
0D -called number length
-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 (xx) + (0D) + (683136187972f5), constitutes the second part of the entire text message destination address (tp-destination-address).

Go on...
XX -Protocol ID tp-pid, typically 00
-Data encoding scheme Tp-dcs (tp-data-coding-scheme), using the previously mentioned USC2 (16bit) data encoding
xx -validity TP-VP (tp-valid-period)
-Length tp-udl (tp-user-data-length), which is the information length/2 of 16 in 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
namely 11000d91683136187972f5 + 000800 +044f60597d
= 11000d91683136187972f5000800044f60597d
3, the entire information length divided by 2, formatted into 2 decimal digits
i.e. 11000d91683136187972f5000800044f60597d =>38 bit/2 = 19

Five, so the content to be sent is
At+cmgf=0 <回车> #此处为设定短信发送模式PDU
At+cmgs=19 <回车>
> #输入短信内容编码

Append the final PHP code:

  9600, ' bits ' = 8, ' Stop ' = 1, ' Parity ' and 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); My one is UTF.-8 coded 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 ($hex          String, $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;   }?>

attached 1: Mobile SMS Center numbers are entered in the local mobile office SMS Number: +8613800xxx500 ("+" number must be entered), where xxx is the local area code. ---Dialling code is three-bit area: replace XXX directly with the telephone area code. For example: Shenzhen telephone area code is 755, the mobile Short Message center number is: +8613800755500---Telephone area code is two-bit area: Please add "0" after the area code to top up three-bit replacement xxx. For example: Beijing telephone code is 10, the mobile Short Message center number is: +8613800100500 the current Unicom 165 network has been operating nationwide. Before using the SMS service, you need to set up the SMS Center Services Number: Beijing +8613010112500 Shanghai +8613010314500 Shenzhen +8613010888500 Shandong +8613010171500 Jiangsu +8613010341500 Zhejiang + 8613010360500 Fujian + 86,130,103,805,004 Chuan +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 attached 2: Common at command: At+csms Select SMS Service AT+CPMS Select Short Message memory AT+CMGF Select SMS format AT+CSCA SMS Center address AT+CNMI Show new SMS messages at+cmgr read SMS At+cmgs send SMS AT+CMGL List SIM messages At+cmss sms from SIM memory AT+CMGW write outgoing SMS to SIM memory AT+CMGD Delete SMS in SIM memory AT+CSCB select Cellular Broadcast Information attached 3: Receiving and receiving short messages is essentially reading information from a SIM or cache. This mainly use At+cmgr and AT+CMGL two instructions to complete, because the wireless module different manufacturers of the at instruction set of the interpretation Code and response information is not the same, so first to confirm whether to establish communication with the modem, generally with at command to complete this confirmation; then use the AT+CMGF The command selects the data format of the short message, and the correct answer to the modem is to complete the readout function with the AT command. Generally use AT+CMGL to read the previous information, when receiving the modem ring (ringing) data, with At+cmgr read real-time information. The following is an example of receiving SMS with H6221-W, which illustrates the application of PDU mode. The procedure is as follows (comments within {}): send: At answer: OK {established join} send: at+cmgf=0 {select PDU format} answer: OK {allow PDU format to be selected} send: at+cmgl=2 {list existing SMS} answer: +cmgl:1,2,,24{1 Indicates the number of messages, 2 indicates no information, 24 indicates total information capacity} 0d71683108370105f004000d81683179133208f10000026080410033802632184cf682d95e0dc2b36d3d170a0243106933d97a0243106933d97a02451 068b1983492608ok above this set of PDU format hexadecimal string, not only contains the content of the short message, but also contains the sender's number, the short Message center number, the sending time of short messages. The following analysis of the content of information: 0D: Short Message Center address (number) length. 91: SMS Center number type, 91 is TON/NPI. TON/NPI comply with the international/e.164 standard, refers to the number before the "+" number, but also straight with other values, but 91 most commonly used. The service center number used by 683108370105f0:smsc SMS is 13807310500. It passes hexadecimal in bytes of high and low half-byte transposition processing, the number is odd to add F, constitute a hex byte. 04:PDU type, file header byte. 0B: Caller number length. 81: Calling number type. 3179133208f1:0a calling number, also processed, the actual number is 13973123801. 00:pid, identified for the protocol. 00:dcs SMS encoding type is the GSM Default Alphabet, that is, the 7-bit ASCII code is shifted to form a 8-bit hexadecimal code (octet), the method is shown in table 2. 1sthex B0 A6 A5 A4 A3 A2 A1 a02ndhex C1 C0 B6 B5 B4 B3 B2 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 G0 F6 f56thhex H6 H5 H4 H3 H2 H1 H0 g602608041003380:scts SMS send Time, 02/06/08/14:00 : 33.08. 26:udl the processed 8-bit code (octet) SMS byte length, which is less than the length of the message ASCII code. 32184CF682D95E30DC2B36D3D170A0243106933D97A0243106933D97A02451068B1983492608:UD encoded PDU data, SMS Content "2002/06/08/ 13:48id102okid103ok Id201fail ". true techarticle PHP through the serial port text messaging, PHP serial text messaging with the progress of technology, the field of text messaging has produced three modes in time: BLOCK mode, based on the text mode at the command, based on the ...

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