例如短信息中心号码为深圳 +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
=> phone = 11000D9168683116325476F8
3、phone 长度除以2,格式化成2位的十进制数
即 11000D9168683116325476F8
五、所以要发送的内容为
AT+CMGS=18<回车,ASCII为13>
addr+phone+<^Z>+换行回车<13和10>
即AT+CMGS=18<回车>0891683108705505F011000D
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