PHP通过串口实现发送短信,php串口发送短信
PHP通过串口实现发送短信,php串口发送短信
随技术进步,短信收发领域按时间先后产生了三种模式:BLOCK MODE,基于AT指令的TEXT MODE,基于AT指令的PDU MODE。其中,TEXT MODE比较简单,多款诺基亚手机均支持此款模式。西门子的手机大多数只支持PDU MODE。PDU 模式是收发短信的一种方法,短信正文经过十六进制编码后被传送。目前,PDU已取代BLOCK MODE。
SMS是由Etsi所制定的一个规范(GSM 03.40 和GSM 03.38)。当使用7-bits编码时,它可以发送最多160个字符;但用8-bit编码,最多可以发送140个字符,通常无法直接通过手机显示;还有用16-bit编码时,最多70个字符,被用来显示Unicode(UCS2)文本信息,可以被大多数的手机所显示。
今天讨论的是PDU MODE,UCS2编码,也就是说,最多只能发送70个字符,不管英文还是中文。
假设现在要发送如下信息:“你好”。在没有发送之前,要知道手机SIM卡所在地的短信中心号,例如移动的短信中心号:
接收的手机号:13638197275
杭州短信中心号:13800571500
短信内容: 你好
发送这条短信,要进行编码后手机才会执行,编码后会变成以下一串字符:
0891683180501705F011000D91683136187972F5000800044F60597D
看不懂吧,从头到尾把这串编码解释一下:
08 – 指的是短信中心号的长度,也就是指(91)+(683180501705F0)的长度除以2,即 08 =(2+14)/ 2
91 – 指的是短信息中心号码类型。91是TON/NPI遵守International/E.164标准,指在号码前需加‘+'号;此外还有其它数值,但91最常用。
683180501705F0 - 短信息中心号码。由于位置上略有处理,实际号码应为:8613800571500(字母F是补足偶数长度添加的字符)。
11 - 文件头字节
00 - 信息类型(TP-Message-Reference)
0D - 被叫号码长度
91 - 被叫号码类型
其实在实际处理中,我们通常把11000D91写死在程序中,因为在国内,这些数据都是不会改变的。
683136187972F5 - 被叫号码,经过了位移处理,实际号码为“8613638197275”。
上面的(00 )+(0D )+(91 )+(683136187972F5 ),构成了整个短信的第二部份目的地址(TP-Destination-Address)。
继续...
00 - 协议标识TP-PID,这里一般为00
08 - 数据编码方案TP-DCS(TP-Data-Coding-Scheme),采用前面说的USC2(16bit)数据编码
00 - 有效期TP-VP(TP-Valid-Period)
04 - 长度TP-UDL(TP-User-Data-Length),也就是信息长度/2的十六进04
4F60597D 这里就是短信内容了,实际内容为:“你好”
根据以上情况,就可以写出短信编码的程序脚本了。
一、短信中心号码处理:
1、将短信息中心号码“+8613800571500”去掉+号,看长度是否为偶数,如果不是,最后添加F
=> “8613800571500F”
2、将奇数位和偶数位交换。
=> “683108501705F0″
3、将短信息中心号码前面加上字符91,91是国际化的意思
=> “91683108501705F0″
4、算出长度,结果除2,格式化成2位的16进制字符串,16 / 2 = 8 => “08″
=> “0891683108501705F0″
二、手机号码处理:
1、将手机号码+8613638197275去掉+号,看看长度是否为偶数,如果不是,最后添加F
=> “8613638197275F”
2、将手机号码奇数位和偶数位交换。
=> “683136187972F5″
三、短信息部分处理:
1、转字符串转换为Unicode代码,
“你好”的unicode代码 为4F60597D
2、将长度除2,保留两位16进制数,即 4F60597D = 8 / 2 => “04″,
=> “044F60597D″
四、组合
1、手机号码前加上字符串 11000D91(1100:固定,0D:手机号码的长度,不算+号,十六进制表示,91:发送
到手机为91,发送到小灵通为81),
即 11000D91 + 683136187972F5
=> 11000D91683136187972F5
2、手机号码后加上 000800 和刚才的短信息内容,000800也写死就可以了
即 11000D91683136187972F5 + 000800 + 044F60597D
=> 11000D91683136187972F5000800044F60597D
3、整条信息长度除以2,格式化成2位的十进制数
即 11000D91683136187972F5000800044F60597D => 38位 / 2 => 19
五、所以要发送的内容为
AT+CMGF=0 #此处为设定短信发送模式PDU
OK
AT+CMGS=19
> #输入短信内容编码
附加最终PHP代码:
<?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; } ?>
以上所述就是本文的全部内容了,希望大家能够喜欢。

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

AI Hentai Generator
Generate AI Hentai for free.

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

Validator can be created by adding the following two lines in the controller.
