Home Backend Development PHP Tutorial PHP透过串口发短信

PHP透过串口发短信

Jun 13, 2016 pm 12:21 PM
at gt nbsp pdu

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;      }             ?>  
Copy after login

 

 

附1:各地移动短信中心号码

 

输入当地移动局的短消息号码:

+8613800xxx500("+"号必须输入),

其中xxx 为当地的电话区号。

--- 电话区号是三位的地区:

直接用电话区号替换xxx 即可。

例如:深圳电话区号是755,

移动的短消息中心号码是:+8613800755500

--- 电话区号是两位的地区:

请在区号后加"0"补足三位替换xxx。

例如:北京电话区号是10,

移动的短消息中心号码是:+8613800100500

目前联通165 网已在全国范围内开展业务。

在使用短信业务之前,需设置短信中心服务号码:

北京+8613010112500

上海+8613010314500

深圳+8613010888500

山东+8613010171500

江苏+8613010341500

浙江+8613010360500

福建+8613010380500

四川+8613010811500

重庆+8613010831500

海南+8613010501500

黑龙江+8613010980500

吉林+8613010911500

天津+8613010130500

河北+8613010180500

内蒙古+8613010950500

山西+8613010701500

安徽+8613010305500

新疆+8613010969500

青海+8613010776500

甘肃+8613010879500

宁夏+8613010796500

贵州+8613010788500

云南+8613010868500

湖南+8613010731500

湖北+8613010710500

广东+8613010200500

广西+8613010591500

河南+8613010761500

江西+8613010720500

辽宁+8613010240500

 

附2:常用AT 指令:

AT+CSMS 选择短信息服务

AT+CPMS 选择短信息内存

AT+CMGF 选择短信息格式

AT+CSCA 短信息中心地址

AT+CNMI 显示新收到的短信息

AT+CMGR 读短信息

AT+CMGS 发送短信息

AT+CMGL 列出SIM 卡中短信息

AT+CMSS 从SIM 内存中发短信息

AT+CMGW 向SIM 内存中写入待发短信息

AT+CMGD 删除SIM 内存中的短信息

AT+CSCB 选择蜂窝广播信息

 

附3:短信息的接收

接收短消息实质上就是从SIM 或缓存中读出信息。这主要利用AT+CMGR 和AT+CMG

L 两条指令来完成,由于无线模块不同的厂商对AT 指令集的解释代码和响应信息不一样,

所以首先要确认能否与MODEM 建立起通信,一般用AT 指令完成此确认;然后用AT+CM

GF 指令选定短消息的数据格式;在收到MODEM 的正确回答反以AT 指令完成读出功能。

一般用AT+CMGL 读取以前的信息,在收到MODEM 的RING(振铃)数据时,用AT+CM

GR 读取实时信息。以下是用H6221-W 的接收SMS 的一个实例,它说明了PDU 模式的应

用。

操作过程如下({}内为注释):

发送:AT

回答:OK {已建立联接}

发送:AT+CMGF=0 {选用PDU 格式}

回答:OK {允许选择PDU 格式}

发送:AT+CMGL=2 {列出已有的短信息}

回答:+CMGL:1,2,,24{1 表示信息个数,2 表示未发信息,24 表示信息总容量}

0D71683108370105F004000D81683179133208F10000026080410033802632184C

F682D

95E0DC2B36D3D170A0243106933D97A0243106933D97A02451068B1983492608

OK

以上这组PDU 格式的十六进制字符串,不但包含了短消息的内容,同时包含了发送者

的号码、短信息中心号码、短消息发送时间等。

下面对信息内容进行分析:

0D:短信息中心地址(号码)长度。

91:短信息中心号码类型,91 是TON/NPI。TON/NPI 遵守International/E.164标准,

指在号码前需加'+'号;此外还可直有其他数值,但91 最常用。

683108370105F0:SMSC 短信息所使用的服务中心号码13807310500。它经过十六

进制以字节为单位的高低半字节换位处理,号码是奇数的添F,构成一个HEX 字节。

04:PDU 类型,文件头字节。

0B:主叫号码长度。

81:主叫号码类型。

3179133208F1:0A 主叫号码,也经过了处理,实际号码为13973123801。

00:PID,为协议标识。

00:DCS 短信息编码类型是GSM Default Alphabet,即由7 位ASCII 码移位组成8 位

十六进制码(octet),其方法见表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 短信息发送时间,02/06/08/14:00:33.08。

26:UDL 经处理后的8 位码(octet)短信息字节长度,它小于消息ASCII 码的长度。

32184CF682D95E30DC2B36D3D170A0243106933D97A0243106933D97A0245106

8B1983492608:UD 编码后的PDU数据,短信息内容“2002/06/08/13:48ID102OKID103

OK ID201FAIL”。

1楼Craftsman_Gao
免费发短信?
Statement of this Website
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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

See all articles