Tag-Archive for » PDU信息 构成算法 CMGS CMGW «

PDU短信编写算法

例如短信息中心号码为移动上海 +8613800210500F

对方手机号码为 +8618802136442

要发送的信息为“中”

算法步骤:

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

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

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

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

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

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

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

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

三、短信息部分,用字符串 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
=> 11000D91688108126344F2

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

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

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

即AT+CMGS=17<回车>0891683108200105F011000D91688108126344F2000800024E2D^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

在超级终端演示如下

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

附信息内容:

Text

MAX

abcdefghijklmnopqrstuvwxwzabcdefghijklmnopqrstuvwxwzabcdefghijklmnopqrstuvwxwzabcdefghijklmnopqrstuvwxwzabcdefghijklmnopqrstuvwxwzabcdefghijklmno

PDU

General

0891683108200105F011000D91688108126344F2000800024E2D

MAX

0891683108200105F011000D91688108126344F2000800824E004E014E024E034E044E054E064E074E084E094E0A4E0B4E0C4E0D4E0E4E0F4E104E114E124E134E144E154E164E174E184E194E1A4E1B4E1C4E1D4E1E4E1F4E204E214E224E234E244E254E264E274E284E294E2A4E2B4E2C4E2D4E2E4E2F4E304E314E324E334E344E354E364E374E384E394E3A4E3B4E3C4E3D4E3E4E3F4E40