Home  >  Article  >  Backend Development  >  PHP sends text messages through serial port

PHP sends text messages through serial port

WBOY
WBOYOriginal
2016-08-08 09:23:10913browse

With the advancement of technology, three modes have emerged in the field of text messaging: BLOCK MODE, TEXT MODE based on AT commands, and PDU MODE based on AT commands. Among them, TEXT MODE is relatively simple, and many Nokia mobile phones support this mode. Most Siemens mobile phones only support PDU MODE. PDU mode is a method of sending and receiving text messages. The text of the text message is transmitted after hexadecimal encoding. Currently, 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, it can send up to 140 characters, which usually cannot be displayed directly through the mobile phone; and when using 16-bit encoding, up to 70 characters, Used to display Unicode (UCS2) text information, which can be displayed by most mobile phones.

Today’s discussion is PDU MODE, UCS2 encoding, which means that a maximum of 70 characters can be sent, regardless of English or Chinese.
Suppose you want to send the following message: "Hello". Before sending, you need to know the SMS center number where the SIM card is located, such as China Mobile’s SMS center number:

Received mobile phone number: 13638197275
Hangzhou SMS Center Number: 13800571500
Text message content: Hello
To send this text message, the phone will execute it after encoding. After encoding, it will become the following string of characters:
0891683180501705F011000D91683136187972F5000800044F60597D
You don’t understand, please explain this string of codes from beginning to end:
08 – It 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 – Refers to the SMS center number type. 91 means that TON/NPI complies with the International/E.164 standard, which means that a ‘+’ sign needs to be added before the number; there are other values, but 91 is the most commonly used.
​ 683180501705F0 - SMS center number. Due to slight processing in position, the actual number should be: 8613800571500 (the letter F is a character added to make up the even length).
11 - File header bytes
00 - Message type (TP-Message-Reference)
0D - Called number length
91 - Called number type

In fact, in actual processing, we usually write 11000D91 into the program, because in China, these data will not change.

683136187972F5- The called number has been shifted and 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, here is generally 00
08 - Data coding scheme TP-DCS (TP-Data-Coding-Scheme) uses the USC2 (16bit) data coding mentioned earlier
00 - Validity period TP-VP (TP-Valid-Period)
04 - Length TP-UDL (TP-User-Data-Length), which is the hexadecimal 04 of the message length/2
4F60597D Here is the text message content. The actual content is: "Hello"

Based on the above situation, you can write the program script for text message encoding.

1. SMS center number processing:
1. Remove the + sign from the SMS center number "+8613800571500" to see if the length is an even number. If not, add F
=> "8613800571500F" at the end
2. Remove the odd digits Swap with even bits.
=> “683108501705F0″
3. Add the character 91 in front of the SMS center number. 91 means internationalization
=> “91683108501705F0″
4. Calculate the length, divide the result by 2, and format it into a 2-digit 16 Base string, 16 / 2 = 8 => “08″
=> “0891683108501705F0″

2. Mobile phone number processing:
1. Remove the + sign from the mobile phone number +8613638197275 and see if the length is an even number. If not, add F
=> "8613638197275F" at the end
2. Swap the odd and even digits of the mobile phone number.
=> “683136187972F5″

3. Short message processing:
1. Convert the string to Unicode code,
The unicode code of “Hello” is 4F60597D
2. Divide the length by 2 and keep two digits in hexadecimal System number, that is 4F60597D = 8 / 2 => “04″,
=> “044F60597D″

4. Combination
1. Add the string 11000D91 before the mobile phone number (1100: fixed, 0D: the length of the mobile phone number, not counting the + sign, expressed in hexadecimal, 91: 91 when sending
to the mobile phone, 91 when sending to the small Lingtong is 81),
That is, 11000D91 + 683136187972F5
=> 11000D91683136187972F5
2. After the mobile phone number, add 000800 and the content of the text message just now, just write down 000800
That is, 11000D91683136 187972F5 + 000800 + 044F60597D
=> 11000D91683136187972F5000800044F60597D
3. Divide the length of the entire message by 2 and format it into a 2-digit decimal number
That is 11000D91683136187972F5000800044F60597D => 38 bits / 2 => 19

5. So the content to be sent is
AT+CMGF=0 #This is to set the SMS sending mode PDU
OK
AT+CMGS=19
> #Enter the text message content encoding

Attach 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("打开串口ttyS0失败");  
    }  
      
    // dio_tcsetattr() only Linux   
    // Windows 使用 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";  
      
    //短信中心号码  
    $smsc = "8613800571500";  
    $invert_smsc = invertNumbers($smsc); // 转换短信中心号码  
    $inter = chr(13); // 回车字符  
      
    $ctrlz = chr(26); // ctrl+z  
      
    // 发送信息  
    $text   
       = '你好';  
    $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);  
      
    // 我的是utf-8编码   
    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;  
    }  
       
    ?>  

Attachment 1: Mobile SMS center number in various places

Enter the SMS number of the local mobile bureau:

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

where xxx is the local telephone area code.

---For areas where the telephone area code is three digits:

Just replace xxx with the telephone area code.

For example: Shenzhen’s telephone area code is 755,

China Mobile’s SMS center number is: +8613800755500

--- For areas where the telephone area code is two digits:

Please add "0" after the area code and replace it with three digits xxx .

For example: Beijing’s telephone area code is 10,

China Mobile’s short message center number is: +8613800100500

Currently, China Unicom 165 network has launched operations nationwide.

Before using the SMS service, you need to set the SMS center service 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

Appendix 2: Common AT commands:

AT+CSMS Select short message service

AT+CPMS Select short message memory

AT+CMGF Select SMS Format

AT+CSCA SMS center address

AT+CNMI Display newly received SMS messages

AT+CMGR Read SMS messages

AT+CMGS Send SMS messages

AT+CMGL List SMS messages in SIM card

AT+CMSS Send SMS from SIM memory

AT+CMGW Write the SMS to be sent to SIM memory

AT+CMGD Delete the SMS in SIM memory

AT+CSCB Select cellular broadcast information

Appendix 3: Receiving short messages

Receiving short messages is essentially reading the information from the SIM or cache. This is mainly done using the two commands AT+CMGR and AT+CMG

L. Since different manufacturers of wireless modules have different interpretation codes and response messages for the AT command set,

so you must first confirm whether it can be established with the MODEM. For communication, the AT command is generally used to complete this confirmation; then the AT+CM

GF command is used to select the data format of the short message; after receiving the correct answer from the MODEM, the AT command is used to complete the reading function.

Generally use AT+CMGL to read previous information, and when receiving MODEM's RING data, use AT+CM

GR to read real-time information. The following is an example of receiving SMS using H6221-W, which illustrates the application of PDU mode.

The operation process is as follows ({} are comments):

Send: AT

Answer: OK {Connection established}

Send: AT+CMGF=0 {Select PDU format}

Answer: OK {Allow selection PDU format}

Send: AT+CMGL=2 {List existing short messages}

Answer: +CMGL: 1, 2,, 24 {1 represents the number of messages, 2 represents unsent messages, 24 represents messages Total capacity}

0D71683108370105F004000D81683179133208F10000026080410033802632184C

F682D

95E0DC2B36D3D170A0243106 933D97A0243106933D97A02451068B1983492608

OK

The above set of hexadecimal strings in PDU format not only contains the content of the short message, but also the number and short message of the sender

Center number, SMS sending time, etc.

The message content is analyzed below:

0D: Short message center address (number) length.

91: SMS center number type, 91 is TON/NPI. TON/NPI complies with International/E.164 Standard,

means that a '+' sign needs to be added before the number; in addition, there can be other numerical values, but 91 is the most commonly used.

683108370105F0: The service center number used for SMSC text messages is 13807310500. It is processed by transposing the high and low nibbles in byte units in hexadecimal. The number is an odd number plus F, forming a HEX byte.

04: PDU type, file header bytes.

0B: Calling number length.

81: Calling number type.

3179133208F1: 0A The calling number has also been processed, and the actual number is 13973123801.

00: PID, which is the protocol identifier.

00: The DCS short message encoding type is GSM Default Alphabet, which is composed of 7-bit ASCII code shifting to form an 8-bit

hex code (octet). The method 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

5th hex 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 sending time, 02/06/08/14:00:33.08.

26: UDL processed 8-bit code (octet) short message byte length, which is smaller than the message ASCII code length.

32184CF682D95E30DC2B36D3D170A0243106933D97A0243106933D97A0245106

8B1983492608: UD encoded PDU Data, text message content "2002/06/08/13: 48ID102OKID103

OK ID201FAIL".

The above introduces PHP to send text messages through the serial port, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn