search
HomeBackend DevelopmentPHP TutorialPHP 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 #This is to set the SMS sending mode PDU
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(&#39;/dev/ttyS0&#39;, O_RDWR); 
if(!$fd) 
{ 
  die("打开串口ttyS0失败"); 
} 
  
// dio_tcsetattr() only Linux 
// Windows 使用 exec(&#39;mode COM1: baud=9600 data=8 stop=1 parity=n xon=on&#39;); 
dio_tcsetattr($fd, array( 
 &#39;baud&#39; => 9600, 
 &#39;bits&#39; => 8, 
 &#39;stop&#39; => 1, 
 &#39;parity&#39; => 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 
  = &#39;你好&#39;; 
$send_to = &#39;8613638197275&#39;; 
$pdu_phone = hex2str(utf82unicode($text)); 
$pdu_phone = sprintf("%02X", strlen($pdu_phone)/2) . $pdu_phone; 
$pdu_phone = &#39;11000D91&#39; . invertNumbers($send_to) . &#39;000800&#39; . $pdu_phone; 
$atcmd   = &#39;AT+CMGF=0&#39; . $inter; 
@dio_write($fd, $atcmd); 
$atcmd   = &#39;AT+CMGS=&#39; . sprintf("%d", strlen($pdu_phone)/2) . $inter; 
@dio_write($fd, $atcmd); 
$pdu_addr  = &#39;0891&#39; . 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 = &#39;&#39;; 
  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

php file upload class example

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!

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
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft