“PDU模式发送短信”问题(在线等待),QQ:394860253 ( 积分: 50 )

  • 主题发起人 fanli1981
  • 开始时间
F

fanli1981

Unregistered / Unconfirmed
GUEST, unregistred user!
// Pdu模式编码
procedure TMainForm.SendInEncodePdu(sms, mobile: string);
var
Widesms : WideString; // 定义成WideString型,每个字符占两上字节
SendData : string;
tmp1,tmp2,tmp3 : string;
len : integer;
begin
// 参考pdu串 08 91 683108701305F0 11 00 0B 91 3176378290F9 00 00 00 02 C834
SendData := 'AT+CMGS="';
SendData := SendData + '0891';
// 每两位交换构造短信中心串
tmp1 := ExchangeCode(zhongxin);
SendData := SendData + tmp1;
SendData := SendData + '11000B91';
// 每两位交换构造接收手机号码串
tmp2 := ExchangeCode(mobile);
SendData := SendData + tmp2;
SendData := Senddata + '0000A7';
// 将中文GB2312编码转换为代码页为CP936的Unicode编码
Widesms := WideString(sms);
tmp3 := EncodeGb(Widesms);
// 此处为Widesms 的长度,不是sms的长度!
len := length(Widesms);
SendData := SendData + IntToStr(len) + tmp3;
SendData := SendData + '"' + chr(13);
// Pdu串构造完毕,发送
MSComm.Output := SendData;
end;

// 将中文GB2312编码转换为代码页为CP936的Unicode编码
function TMainForm.EncodeGb(var s: WideString): String;
var
i,len:Integer;
cur:Integer;
t:String;
begin
Result:='';
len:=Length(s);
i:=1;
while i<=len do
begin
cur:=ord(s);
FmtStr(t,'%4.4X',[cur]);
Result:=Result+t;
inc(i);
end;
end;

// 用来交换移位
function TMainForm.ExchangeCode(src: string): string;
var
Len, i : integer;
tmp : string;
begin
Len := length(src);
if (len <> 11) and (len <> 13) then
begin
ExchangeCode := ''; exit;
end;
src := src + 'F';

i := 1;
while i<=len do
begin
tmp := tmp + src[i + 1] + src;
Inc(i, 2);
end;
ExchangeCode := tmp;
end;
 
// Pdu模式编码
procedure TMainForm.SendInEncodePdu(sms, mobile: string);
var
Widesms : WideString; // 定义成WideString型,每个字符占两上字节
SendData : string;
tmp1,tmp2,tmp3 : string;
len : integer;
begin
// 参考pdu串 08 91 683108701305F0 11 00 0B 91 3176378290F9 00 00 00 02 C834
SendData := 'AT+CMGS=&quot;';
SendData := SendData + '0891';
// 每两位交换构造短信中心串
tmp1 := ExchangeCode(zhongxin);
SendData := SendData + tmp1;
SendData := SendData + '11000B91';
// 每两位交换构造接收手机号码串
tmp2 := ExchangeCode(mobile);
SendData := SendData + tmp2;
SendData := Senddata + '0000A7';
// 将中文GB2312编码转换为代码页为CP936的Unicode编码
Widesms := WideString(sms);
tmp3 := EncodeGb(Widesms);
// 此处为Widesms 的长度,不是sms的长度!
len := length(Widesms);
SendData := SendData + IntToStr(len) + tmp3;
SendData := SendData + '&quot;' + chr(13);
// Pdu串构造完毕,发送
MSComm.Output := SendData;
end;

// 将中文GB2312编码转换为代码页为CP936的Unicode编码
function TMainForm.EncodeGb(var s: WideString): String;
var
i,len:Integer;
cur:Integer;
t:String;
begin
Result:='';
len:=Length(s);
i:=1;
while i<=len do
begin
cur:=ord(s);
FmtStr(t,'%4.4X',[cur]);
Result:=Result+t;
inc(i);
end;
end;

// 用来交换移位
function TMainForm.ExchangeCode(src: string): string;
var
Len, i : integer;
tmp : string;
begin
Len := length(src);
if (len <> 11) and (len <> 13) then
begin
ExchangeCode := ''; exit;
end;
src := src + 'F';

i := 1;
while i<=len do
begin
tmp := tmp + src[i + 1] + src;
Inc(i, 2);
end;
ExchangeCode := tmp;
end;
 
这段程序还存在什么问题?
为什么不能发送出短信?
急!!!QQ:394860253
 
在你的最后生成的PDU码上,发送的PDU码要加char($1a)结尾
 
to:ZENGWEI_74
SendData := SendData + '&quot;' + chr(13) +char($1a);
是这样吗?
好像也不行!
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3054598

参见: http://www.sms2008.net 有一堆。
下载: http://www.sms2008.net/files/KoSMSGateWay_V2006.rar
通过Outlook收发短信演示: http://www.sms2008.net/files/Outlook_Demo.rar
通过Foxmail收发短信演示: http://www.sms2008.net/files/Foxmail_Demo.rar

现成产品,无需自已再花精力做了。
 
Paste
function Thandset.receiveShortMessage: string;
var
msg, tempMsg, NoStr, msgStr: string;
position, len, msgLen: integer;
begin
if not PortOpen then
exit;

result:= '';
msg:= '';

//&amp;Eacute;è&amp;Ouml;&amp;Atilde;&amp;Oacute;&amp;Atilde;PDU&amp;cedil;&amp;ntilde;&amp;Ecirc;&amp;frac12;&amp;frac12;&amp;Oacute;&amp;Ecirc;&amp;Otilde;
sendText('at+cmgf=0'+#13);
sleep(1000);
//PDU&amp;Auml;&amp;pound;&amp;Ecirc;&amp;frac12;&amp;Iuml;&amp;Acirc;&amp;para;&amp;Aacute;&amp;Icirc;&amp;acute;&amp;para;&amp;Aacute;&amp;para;&amp;Igrave;&amp;ETH;&amp;Aring;
sendText('AT+CMGL=0'+#13);
sleep(1000);

GetText(tempmsg);
position:= pos('+CMGL:',tempMsg);
while not (position=0) do begin
msg:= msg+ tempMsg;
sleep(1000);
getText(tempMsg);
position:= pos('+CMGL:',tempMsg);
end;

if assigned( FonHandsetMsgEvent) then
FonHandsetMsgEvent(Msg);

position:= pos('+CMGL:',msg);
len:= length(msg);
while not (position=0) do begin
msg:= copy(msg, position, len);
position:= pos(#13, msg); //&amp;raquo;&amp;ntilde;&amp;micro;&amp;Atilde;&amp;Otilde;&amp;acirc;&amp;Ograve;&amp;raquo;&amp;ETH;&amp;ETH;&amp;frac12;á&amp;Ecirc;&amp;oslash;&amp;micro;&amp;Auml;&amp;Icirc;&amp;raquo;&amp;Ouml;&amp;Atilde;
if position = 0 then
exit;
len:= length(msg);

if msg[position+1]= #10 then //&amp;raquo;&amp;ntilde;&amp;micro;&amp;Atilde;&amp;Iuml;&amp;Acirc;&amp;Ograve;&amp;raquo;&amp;ETH;&amp;ETH;&amp;frac14;°&amp;Ograve;&amp;Ocirc;&amp;ordm;ó&amp;micro;&amp;Auml;×&amp;Ouml;·&amp;ucirc;&amp;acute;&amp;reg;
msg:= copy(msg, position+2, len)
else
msg:= copy(msg, position+1, len);

msgLen:= StrToint('$'+ copy(msg, 57, 2))*2;
NoStr:= msg[26]+ msg[25]+' '+decodeHandSet(copy(msg, 27, 12));
msgStr:= decodeMsg(copy(msg, 59, msglen));
if result = '' then
result:= result+'&amp;Agrave;&amp;acute;×&amp;Ocirc; '+NoStr+' : '+msgStr
else
result:=result + #13#10 + '&amp;Agrave;&amp;acute;×&amp;Ocirc; '+NoStr+' : '+msgStr;

msg:= copy(msg, 59+msglen, len);
position:= Pos('+CMGL:',msg);
end;

end;

procedure Thandset.sendShortMessage(handsetNo, msg: string);
var
pWideStr: pwidechar;
msgLen: integer;
EMsg, returnMsg: string;
begin
if not portOpen then
exit;

if length(msg) = 0 then
exit;

if (length(handsetNo) <> 11) and (length(handsetNo) <> 12) then
raise exception.Create(handsetNo+':&amp;Ecirc;&amp;Ouml;&amp;raquo;ú&amp;ordm;&amp;Aring;&amp;Acirc;&amp;euml;&amp;Oacute;&amp;brvbar;&amp;Icirc;&amp;ordf;11&amp;raquo;ò12&amp;Icirc;&amp;raquo;');

msgLen:= length(msg);
getMem(pWideStr, msgLen*2);
pWideStr:=StringToWideChar(msg, pWideStr, msgLen);
EMsg:= encodeMsg(pWideStr);
try
sendText('at+cmgf=0'+#13);
sleep(1000);
sendText('AT+CMGS='+ Format('%.3d', [length(Emsg) div 2+15])+#13);
sleep(500);
sendText('0011000D9168'+encodeHandSet(handsetNo)+'0008A7'+
intToHex(length(Emsg) div 2,2)+ EMsg +#13+#26);
finally
FreeMem(pWideStr);
end;

sleep(5000);

{getText(returnMsg);
if assigned( FonHandsetMsgEvent) then
FonHandsetMsgEvent(returnMsg);}
end;

function Thandset.encodeHandSet(handsetNo: string): string;
var
i: integer;
ch: char;
begin
if length(handsetNo) = 11 then
result:= handsetNo+'F'
else if length(handsetNo) = 12 then
result:= handsetNo
else
raise exception.Create('&amp;Ecirc;&amp;Ouml;&amp;raquo;ú&amp;ordm;&amp;Aring;&amp;Acirc;&amp;euml;&amp;Oacute;&amp;brvbar;&amp;Icirc;&amp;ordf;11&amp;raquo;ò12&amp;Icirc;&amp;raquo;');


for i:=1 to 6 do begin
ch:= result[i*2];
result[i*2]:= result[i*2-1];
result[i*2-1]:= ch;
end;
end;

function Thandset.encodeMsg(wideMsg: PWideChar): string;
var
i: integer;
begin
result:= '';
for i:= 0 to length(wideMsg) - 1 do
result:= result+intToHex(ord(wideMsg), 4);
end;

function Thandset.decodeHandSet(encodedNo: string): string;
var
i: integer;
ch: char;
begin
result:= encodedNo;
for i:=1 to 6 do begin
ch:= result[i*2];
result[i*2]:= result[i*2-1];
result[i*2-1]:= ch;
end;
result:= copy(result, 1, 11);
end;

function Thandset.decodeMsg(encodedMsg: string):string;
var
i, len: integer;
pw: pWideChar;
begin
len:= length(encodedMsg);
if len = 0 then
exit;

getMem(pw, len+1);

for i:= 0 to length(encodedMsg) div 4 -1 do
pw:= wideChar(strToInt('$'+copy(encodedMsg, i*4+1, 4)));

pw:= wideChar(0);
result:= WideCharToString(pw);

freeMem(pw);
end;
 
TO:lmxcyx
谢谢,让我仔细学习学习!能留下联系方式吗?
 
先发短信长度给MODEM
AT+CMGS=长度+#13
SleepEx(300,true)再停N毫秒
再发短信内容+#26#13;
 
http://www.delphibbs.com/keylife/iblog_show.asp?xid=13241
 
顶部