读书人

请教有没有人用spcomm控件来做手机短信

发布时间: 2012-02-27 10:00:22 作者: rapoo

请问有没有人用spcomm控件来做手机短信收发的呀?
请贴一段简单的发送短信的例子代码.
(注:请发广告贴的不要跟,不要影响大家阅读我的贴子)

非常感谢.


[解决办法]
procedure TForm1.Sendinfo(StrPhone:String;StrInfo:string);//发送短信息
Var
r,s,s2,s3,s4,s5:string;
cmdlong,tmp:integer;
//msgs:WideString;
Stmp:string;
strcmdlong:string;
begin
// SysDelay(5000); ///延时
try
//s:= '0031000D9168 ';
s:= '0891683108200405F011000D9168 ';
//PDU编码属性,这种方法是不需要设置短信中心号码的,因为现的手机SIM卡已经写好了
s2:=SEncodeMobNO(StrPhone);//对手机号码进行PDU编码
//s3:= '0008A7 ';
s3:= '000800 ';
s4:= ' ';
s5:=EnCodeChinese(StrInfo);
tmp:=length(s5)div 2;
s4:=format( '%X ',[tmp]);
if length(s4) <2 then
s4:= '0 '+s4;
//计算PDU编码长度
r:=s+s2+s3+s4+s5+^Z;
strcmdlong:= '0031000D9168 '+s2+s3+s4+s5+^Z;
cmdlong:=(length(strcmdlong)-2) div 2;
Stmp:= 'AT+CMGF=0 '#13;//设置Modem为PDU模式

Comm1.WriteCommData(pchar(Stmp),length(Stmp));
SysDelay(7); ///延时
Stmp:= 'AT+CMGS= '+inttostr(cmdlong)+#13;//设置信息长度,这里应为PDU编码长度的1/2.
Comm1.WriteCommData(pchar(Stmp),length(Stmp));
SysDelay(7); ///延时
Comm1.WriteCommData(pchar(r),length(r));//发送短信。
SysDelay(10); ///延时
send_ok:=1;
Ssend_ok:= '发送成功 ';
// Application.MessageBox( '发送成功! ', '提示 ',mb_ok+mb_iconinformation);
except
send_ok:=0;
Ssend_ok:= '发送失败 ';
// Application.MessageBox( '发送失败! ', '提示 ',mb_ok+mb_iconinformation);
end;
end;


Sendinfo( '13889825689 ', 'dddd ');///发送信息
[解决办法]
function TForm1.EncodeChinese(Input: WideString): string;
var
i: Integer;
begin
Result := ' ';
for i := 1 to Length(Input) do
Result := Result + Format( '%4.4X ', [ord(Input[i])]);

end;

function TForm1.SEncodeMobNO(SmobNO: string): string;
//要想发送中文短信必须使用Modem的PDU方式。这个函数是将手机号码进行PDU编码。
var
TempPchar: Pchar;
i: integer;
Str: string;
begin
if (copy(smobno, 1, 1) = '+ ') then //判断是否包含国家编码
SmobNO := copy(smobno, 2, length(smobno) - 1); //去掉手机号码中的’+’

if ((length(SmobNO) mod 2) = 1) then
SmobNO := SmobNO + 'F ';

TempPchar := Pchar(SmobNO); //将字符串 Char数组化

i := 0;
Str := ' ';
while i < length(TempPchar) do begin
Str := Str + TempPchar[i + 1] + TempPchar[i];
i := i + 2;
end;
result := Str;

end;

读书人网 >.NET

热点推荐