


PHP implements the idea and specific implementation of sending text messages through the serial port
This article mainly introduces the idea and specific implementation of sending text messages through php through the serial port. Interested friends can refer to it. I hope it will be helpful to everyone.
With the advancement of technology, three modes have emerged in the field of text message sending and receiving in time: 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.
What we are discussing today 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 mobile SIM card is located. For example, China Mobile’s SMS center number:
Received mobile phone number: 13638197275
Hangzhou SMS center number: 13800571500
Content of the message: Hello
To send this text message, the phone will execute it after encoding. After encoding, it will become the following string of characters:
0891683180501705F011000D91683136187972F5000800044F60597D
If you don’t understand, please explain this string of encoding from beginning to end. :
08 – 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 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 During processing, we usually hard-code 11000D91 in 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), using the previously mentioned USC2 (16bit) data encoding
00 - Validity period TP-VP (TP-Valid-Period)
04 - Length TP-UDL (TP-User-Data-Length), which is sixteen times the information length/2 Enter 04
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 numbers from the SMS center number "8613800571500" to see if the length is an even number. If not, add F# at the end. ##=> “8613800571500F”
2. Swap the odd and even digits.
=> “683108501705F0″
3. Add the character 91 in front of the SMS center number. 91 means internationalization.
=> “91683108501705F0″
4. Calculate the length and divide the result 2. Format it into a 2-digit hexadecimal string, 16 / 2 = 8 => “08″
=> “0891683108501705F0″
2. Mobile phone number processing:
1. Remove the digits from the mobile phone number 8613638197275 and see if the length is an even number. If not, add F=> "8613638197275F" at the end
2. Delete the odd and even digits of the mobile phone number exchange.
=> “683136187972F5″
3. Short message processing:
1. Convert string to Unicode code,“Hello "The unicode code is 4F60597D
2. Divide the length by 2 and retain two hexadecimal digits, 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: send
to the mobile phone as 91, send to the small Lingtong is 81),
is 11000D91 683136187972F5
=> 11000D91683136187972F5
2. Add 000800 and the content of the text message just after the mobile phone number. You can also write down 000800.
is 11000D91683136187972 F5 000800 044F60597D
=> 11000D91683136187972F5000800044F60597D
3. Divide the entire message length by 2 and format it into a 2-digit decimal number
That is 11000D91683136187972F5000800044F60597D => 38 digits/ 2 => 19
5. So the content to be sent is
AT CMGF=0
OK
AT CMGS=19< ;Enter>
> #Enter the text message content encoding
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("打开串口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; } ?>
Summary:The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
How to implement email sending in php
php ajax no-refresh message system
The above is the detailed content of PHP implements the idea and specific implementation of sending text messages through the serial port. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
