PHP sends text messages through the serial port

Source: Internet
Author: User
Tags dio
PHP sends SMS via serial port

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:
-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
Ok
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 number Everywhere

Enter your local mobile office's short message number:

+8613800xxx500 (the "+" number must be entered),

where xxx is the local telephone area code.

---Dialling code is three-bit area:

Replace XXX directly with the telephone area code.

For example: Shenzhen telephone code is 755,

The Mobile Short Message center number is: +8613800755500

---Dialling code is two-bit area:

Please add "0" to the three-digit replacement XXX after the area code.

For example: Beijing telephone code is 10,

The Mobile Short Message center number is: +8613800100500

Currently Unicom 165 Network has been operating nationwide.

Before you use the SMS service, you need to set up a message center 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

attached 2: Common at Directives:

AT+CSMS Select SMS Service

AT+CPMS Select SMS Memory

AT+CMGF Select SMS Format

At+csca SMS Center Address

At+cnmi display of newly received SMS messages

At+cmgr Read SMS

At+cmgs Send SMS

AT+CMGL list SMS in SIM card

At+cmss sending SMS from SIM memory

AT+CMGW writing outgoing messages to SIM memory

AT+CMGD Delete sms in SIM memory

AT+CSCB Selecting cellular Broadcast information

Attachment 3: receipt of short messages

Receiving a short message is essentially reading the information from the SIM or cache. This mainly utilizes at+cmgr and AT+CMG

L Two instructions to complete, because the wireless module different manufacturers of the at Instruction set interpretation code and response information is not the same,

So first of all to confirm whether to establish communication with the modem, generally with at command to complete this confirmation; then use AT+CM

The GF command selects the data format of the short message, and the correct answer to the modem receives the AT command to complete the readout function.

Generally use AT+CMGL to read the previous information, when receiving the modem ring (ringing) data, with AT+CM

GR reads real-time information. The following is an example of receiving SMS with H6221-W, which shows that the PDU mode should be

Use.

The procedure is as follows (comments within {}):

Send: At

Answer: OK {A JOIN has been established}

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 means no information, 24 indicates total information capacity}

0d71683108370105f004000d81683179133208f10000026080410033802632184c

f682d

95e0dc2b36d3d170a0243106933d97a0243106933d97a02451068b1983492608

Ok

The above set of the PDU format of the hexadecimal string, not only contains the contents of the short message, but also contains the sender

Number, short Message center number, send time for short messages, and so on.

The following is an analysis of the information content:

0D: SMS Center Address (number) length.

91: SMS Center number type, 91 is TON/NPI. TON/NPI complies with international/e.164 standards,

It is required to add a ' + ' number before the number, and there are other values, but the 91 is most commonly used.

The service center number used by 683108370105f0:smsc SMS is 13807310500. It passes 16

Binary byte transposition in bytes, 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 GSM Default Alphabet, that is, the 7-bit ASCII code is shifted to make up 8 bits

The hexadecimal code (octet), which is shown in table 2.

1sthex B0 A6 A5 A4 A3 A2 A1 A0

2ndhex C1 C0 B6 B5 B4 B3 B2 B1

3rdhex D2 D1 D0 C6 C5 C4 C3 C2

4thhex E3 E2 E1 E0 D6 D5 D4 D3

5thhex F4 F3 F2 F1 F0 E6 E5 E4

6thhex G5 G4 G3 G2 G1 G0 F6 F5

6thhex H6 H5 H4 H3 H2 H1 H0 G6

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

32184cf682d95e30dc2b36d3d170a0243106933d97a0243106933d97a0245106

8B1983492608:UD encoded PDU data, SMS Content "2002/06/08/13:48id102okid103

OK Id201fail ".

1/ F Craftsman_gao
free texting?
  • 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: 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.