用GSM模块发中文短讯,必须以PDU包的形式发送。我将它写成了Dll,直接调用SMSEncode函数即可得到追后的编码。现将原码公开: unit EnCodeCompnent;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; function SMSEncode(s:WideString;Result_Code:pchar):Bool;Stdcall;export; function SMSDecode(Input_Code:pchar;Result_Str:pchar):Bool;Stdcall;export; function MakePDU(DestNumber:pchar;Content:pchar;Result_String:pchar):Bool;stdcall;export; function ConvertPhoneNum(strNum:string):string; function Transposition(instr:pchar;Result_str:pchar):Bool;stdcall;export;//将一个字符串两两交换位置 { Public declarations }implementation function SMSEncode(s:WideString;Result_Code:pchar):Bool; var i,len:Integer; cur:Integer; t,temp:String; begin if length(s)>0 then begin len:=Length(s); i:=1; while i<=len do begin cur:=ord(s[i]); //BCD convert FmtStr(t,'%4.4X',[cur]); temp:=temp+t; inc(i); end; StrCopy(Result_Code,pchar(temp)); Result:=true; end else Result:=false; end;function SMSDecode(Input_Code:pchar;Result_Str:pchar):Bool; var str:string; i:integer; temp:string; A_PWideChar:array[0..300] of widechar; R_String:string; A_integer:integer; begin str:=string(Input_Code); R_String:=''; if length(str)<=0 then begin Result:=false; exit; end; i:=0; while i<length(str) do begin temp:=copy(str,i+1,4); A_integer:=strtoint('$'+temp); move(A_integer,A_PWideChar[i div 4],sizeof(A_PWideChar[i div 4])); inc(i,4); end; A_PWideChar[length(str) div 4]:=#0; R_String:=WideCharToString(@A_PWideChar); StrCopy(Result_Str,pchar(R_String)); Result:=true; end; function MakePDU(DestNumber:pchar;Content:pchar;Result_String:pchar):Bool; const lenSmsInfo='00'; firstOctet='11'; TPMessageReference='00'; TypeOfAddress='91'; TpId='00'; TpDcs='08';//"00" is 7 bit encode "08" is 8 bit encode; TPValidityPeriod='AA';//aa is 4 day var DestPhoneNum:string; AddressLength:string; TpUserData:string; TPUserDataLength:string; temp:WideString; Tmp_Pchar:pchar; Rlt_str:string; begin if (DestNumber='') or (Content='') then begin Result:=false; exit; end else begin DestPhoneNum:=ConvertPhoneNum(DestNumber); AddressLength:=format('%2.2X',[length(DestNumber)]); temp:=Content; Tmp_Pchar:=StrAlloc(1000); if SMSEncode(temp,Tmp_Pchar) then TpUserData:=string(Tmp_Pchar); if TpDcs='08' then TPUserDataLength:=format('%2.2X',[length(TpUserData) div 2]) else TPUserDataLength:=format('%2.2X',[((length(TpUserData) div 2)*8) div 7]); Rlt_str:=lenSmsInfo +firstOctet +TPMessageReference +AddressLength +TypeOfAddress +DestPhoneNum +TpId +TpDcs +TPValidityPeriod +TPUserDataLength +TpUserData;//TpUserData have to Encoded; StrCopy(Result_String,pchar(Rlt_str)); Result:=true; end; end;function ConvertPhoneNum(strNum:string):string; var i:integer; str:string; A_Pchar:pchar; begin str:=strNum; i:=length(str); if odd(i) then begin str:=str+'F'; A_Pchar:=StrAlloc(40); if Transposition(pchar(str),A_pchar) then result:=string(A_Pchar) else result:='Null'; end else begin A_Pchar:=StrAlloc(40); if Transposition(pchar(str),A_pchar) then result:=string(A_Pchar) else result:='Null'; end; end;function Transposition(instr:pchar;Result_str:pchar):Bool; var i:integer; temp:string; destStr:string; In_String:string; begin if length(instr)>0 then begin destStr:=''; In_String:=string(instr); setlength(In_String,length(In_String)); i:=1; while i<length(In_String) do begin temp:=In_String[i+1]+In_String[i]; destStr:=destStr+temp; inc(i,2); end; StrCopy(Result_Str,pchar(destStr)); result:=true; end else begin result:=false; end; end; end.
unit EnCodeCompnent;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; function SMSEncode(s:WideString;Result_Code:pchar):Bool;Stdcall;export;
function SMSDecode(Input_Code:pchar;Result_Str:pchar):Bool;Stdcall;export;
function MakePDU(DestNumber:pchar;Content:pchar;Result_String:pchar):Bool;stdcall;export;
function ConvertPhoneNum(strNum:string):string;
function Transposition(instr:pchar;Result_str:pchar):Bool;stdcall;export;//将一个字符串两两交换位置
{ Public declarations }implementation
function SMSEncode(s:WideString;Result_Code:pchar):Bool;
var
i,len:Integer;
cur:Integer;
t,temp:String;
begin
if length(s)>0 then
begin
len:=Length(s);
i:=1;
while i<=len do
begin
cur:=ord(s[i]);
//BCD convert
FmtStr(t,'%4.4X',[cur]);
temp:=temp+t;
inc(i);
end;
StrCopy(Result_Code,pchar(temp));
Result:=true;
end
else
Result:=false;
end;function SMSDecode(Input_Code:pchar;Result_Str:pchar):Bool;
var
str:string;
i:integer;
temp:string;
A_PWideChar:array[0..300] of widechar;
R_String:string;
A_integer:integer;
begin
str:=string(Input_Code);
R_String:='';
if length(str)<=0 then
begin
Result:=false;
exit;
end;
i:=0;
while i<length(str) do
begin
temp:=copy(str,i+1,4);
A_integer:=strtoint('$'+temp);
move(A_integer,A_PWideChar[i div 4],sizeof(A_PWideChar[i div 4]));
inc(i,4);
end;
A_PWideChar[length(str) div 4]:=#0;
R_String:=WideCharToString(@A_PWideChar);
StrCopy(Result_Str,pchar(R_String));
Result:=true;
end;
function MakePDU(DestNumber:pchar;Content:pchar;Result_String:pchar):Bool;
const
lenSmsInfo='00';
firstOctet='11';
TPMessageReference='00';
TypeOfAddress='91';
TpId='00';
TpDcs='08';//"00" is 7 bit encode "08" is 8 bit encode;
TPValidityPeriod='AA';//aa is 4 day
var
DestPhoneNum:string;
AddressLength:string;
TpUserData:string;
TPUserDataLength:string;
temp:WideString;
Tmp_Pchar:pchar;
Rlt_str:string;
begin
if (DestNumber='') or (Content='') then
begin
Result:=false;
exit;
end
else
begin
DestPhoneNum:=ConvertPhoneNum(DestNumber);
AddressLength:=format('%2.2X',[length(DestNumber)]);
temp:=Content;
Tmp_Pchar:=StrAlloc(1000);
if SMSEncode(temp,Tmp_Pchar) then
TpUserData:=string(Tmp_Pchar);
if TpDcs='08' then
TPUserDataLength:=format('%2.2X',[length(TpUserData) div 2])
else
TPUserDataLength:=format('%2.2X',[((length(TpUserData) div 2)*8) div 7]);
Rlt_str:=lenSmsInfo
+firstOctet
+TPMessageReference
+AddressLength
+TypeOfAddress
+DestPhoneNum
+TpId
+TpDcs
+TPValidityPeriod
+TPUserDataLength
+TpUserData;//TpUserData have to Encoded;
StrCopy(Result_String,pchar(Rlt_str));
Result:=true;
end;
end;function ConvertPhoneNum(strNum:string):string;
var
i:integer;
str:string;
A_Pchar:pchar;
begin
str:=strNum;
i:=length(str);
if odd(i) then
begin
str:=str+'F';
A_Pchar:=StrAlloc(40);
if Transposition(pchar(str),A_pchar) then
result:=string(A_Pchar)
else
result:='Null';
end
else
begin
A_Pchar:=StrAlloc(40);
if Transposition(pchar(str),A_pchar) then
result:=string(A_Pchar)
else
result:='Null';
end;
end;function Transposition(instr:pchar;Result_str:pchar):Bool;
var
i:integer;
temp:string;
destStr:string;
In_String:string;
begin
if length(instr)>0 then
begin
destStr:='';
In_String:=string(instr);
setlength(In_String,length(In_String));
i:=1;
while i<length(In_String) do
begin
temp:=In_String[i+1]+In_String[i];
destStr:=destStr+temp;
inc(i,2);
end;
StrCopy(Result_Str,pchar(destStr));
result:=true;
end
else
begin
result:=false;
end;
end;
end.