手机AT指令 AT+CMGS发信息

最近在忙一个手机AT指令的项目,就是电脑通过蓝牙串口(无线)发送AT指令控制手机,比如给手机接受与发送短信(AT+CMGR AT+CMGS),给手机打电话(ATD110),获取手机电话本(AT+CPBR),手机类型(AT+CGMI)····

      这2天发送短信把我整惨了,网上AT+CMGF=0
OK
AT+CMGS=18
>0011000D91683105114501F500040103414141<^Z>
+CMGS: 205
都很多这样成功··找了无数都不行,最终GOOGLE出来0011000D91683105114501F500040103414141这一串是由一套强大算法组合而成的。如下:原网址:http://industry.ccidnet.com/art/1077/20050328/824955_1.html

例如短信息中心号码为深圳 +8613800755500 

对方手机号码为 +8613612345678

要发送的信息为“中”

算法步骤:

一、地址部分,用字符串 addr 表示

1、将短信息中心号码去掉+号,看看长度是否为偶数,如果不是,最后添加F
即 addr = "+8613800755500"
=> addr = "8613800755500F"

2、将奇数位和偶数位交换。
=> addr = "683108705505F0"

3、将短信息中心号码前面加上字符91,91是国际化的意思
=> addr = "91683108705505F0"

4、算出 addr 长度,结果除2,格式化成2位的16进制字符串,16 / 2 = 8 => "08"
=> addr = "0891683108705505F0"

二、手机号码部分,用字符串 phone

1、将手机号码去掉+号,看看长度是否为偶数,如果不是,最后添加F
即 phone = "+8613612345678"
=> phone = "8613612345678F"

2、将手机号码奇数位和偶数位交换。
=> phone = "683116325476F8"

三、短信息部分,用字符串 msg 表示

1、转字符串转换为Unicode代码,例如“中”的unicode代码为 4E2D,
(好像是将中字的ASCII码分开,分别转成16进制)。因为我用DELPHI,
请vb朋友自己找例子,网上应该许多

function TfrmMain.str_Gb2UniCode( text: string ): String;
var
i,j,len:Integer;
cur:Integer;
t:String;
ws:WideString;
begin
Result:='';
ws := text;
len := Length(ws);
i := 1;
j := 0;
while i <= len do
begin
cur := ord(ws[i]);
FmtStr(t,'%4.4X',[cur]); //BCD转换
Result := Result+t;
inc(i);
//移位计数达到7位的特别处理
j := (j+1) mod 7;
end;
end;

即 msg = "中"
即 msg = str_Gb2UniCode( msg )
=> msg = "4E2D"


2、将 msg 长度除2,保留两位16进制数,即 4E2D = 4 / 2 => "02",再加上 msg
=> msg = "024E2D"

四、组合

1、手机号码前加上字符串 11000D91,这是一些PDU代码,写死就行了,感兴趣可以参考相关PDU格式说明
即 phone = "11000D91" + phone
=> 11000D9168683116325476F8

2、手机号码后加上 000800 和刚才的短信息内容,000800也写死就可以了
即 phone = phone + "000800" + msg
即 11000D9168683116325476F8 + 000800 + 024E2D 
=> phone = 11000D9168683116325476F8000800024E2D

3、phone 长度除以2,格式化成2位的十进制数
即 11000D9168683116325476F8000800024E2D => 36位 / 2 => 18

五、所以要发送的内容为
AT+CMGS=18<回车,ASCII为13>
addr+phone+<^Z>+换行回车<13和10>

即AT+CMGS=18<回车>0891683108705505F011000D91683116325476F8000800024E2D^Z换行回车

2...
从终端设备向蜂窝电话发送短消息文本是十分简单的。以下的代码片段就演示了这个过程。所使用的电话是Nokia 7100系列,并且ME (mobile equipment)是连接在计算的#1号通信端口上。

注意到Ctrl-Z是用来表示终结发送到移动设备的文本输入。

使用Visual Basic来发送SMS文本到Nokia电话:

' Set up the communications port
MSComm1.CommPort = 1 ' Com Port 1

' Set for 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"

' Tell the control to read entire buffer when Input is used
MSComm1.InputLen = 0

' Open the port
MSComm1.PortOpen = True

' Send an 'AT' command to the phone
MSComm1.Output = "AT" & Chr$(13) & Chr(10)
' The phone will respond with an 'OK'

' Set up the phone for a text message
MSComm1.Output = "AT+CMGF=1" & Chr$(13) & Chr(10)
' The phone will respond with an 'OK'

' Prep for SMS, give destination type and destination address.
' Enter the destination type and destination address to prep for SMS
' e.g. AT+CMGS="+2145551212",129
MSComm1.Output = "AT+CMGS= " & Chr(34) & "+2145551212" & Chr(34) & ",129" & Chr$(13) & Chr(10) 
' The phone will return a'>' prompt, and await entry of the SMS message text.

' Now send the text to the phone and terminate with (Ctrl-Z)
MSComm1.Output = "This is a test. WOW! "
' The phone will respond with a conformation containing the 'message reference number' eg. +CMGS: 

' Close the port
MSComm1.PortOpen = False

在终端,看起来就象下面:

AT
OK
AT+CMGF=1
OK
AT+CMGS="+15127752607",129
>This is a test. WOW!
+CMGS: 49

OK
有了如上的算法:我的根据我的短信息中心号码为深圳 +8613010888500 

对方手机号码为 +8613267091950

再超级终端演示如下


手机成功接收到一条来自13267091950的"中"的短信息.

组成分析:0891683110808805F0(0891+信息中心)+11000D91+683162071959F0(手机号)+000800+024E2D 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值