W
wjhx
Unregistered / Unconfirmed
GUEST, unregistred user!
近期做了一个短信收发的小软件,尽管软件不大,但对于刚刚接触这一行业的新手,困难自知。现将其中的编码部分列出,希望能对正在徘徊的兄弟姐妹们有所帮助(格式规定,请自行搜索),先将砖放下了,有玉的请尽管扔
硬件使用WAVECOM与串口相连,串口控件使用APRO,使用其数据打包控件直接将数据取出,代码如下
%%%%%%%%%%%
发送部分
%%%%%%%%%%%
{-----------------------------------------------------------------------------
Procedure: Tdm.CombinationData
Author: administrator
Date: 22-四月-2005
Arguments: CenterNum, DesiNum, ContentText: string
将输入的中心号码及目标号码/内容合并为可用数据
Result: string
-----------------------------------------------------------------------------}
function Tdm.CombinationData(CenterNum, DesiNum,
ContentText: string): string;
var sendStr:string;
nText:string;
begin
// sendStr:='AT+CMGS=';
sendStr:='';
sendStr:=sendStr+'0891'+Self.ExchangeNumber(centerNum);
sendStr:=sendStr+'11000D91';
sendStr:=sendStr+Self.ExchangeNumber(DesiNum);
sendStr:=sendStr+'000800';
nText:=Self.ConvertText(ContentText);
sendStr:=sendStr+Format('%2.2x',[ord(Length(Trim(nText))div 2)]);
sendStr:=sendStr+Trim(nText);
Result:=sendStr;
end;
{-----------------------------------------------------------------------------
Procedure: Tdm.ConvertText
Author: administrator
Date: 22-四月-2005
Arguments: value: string 对内容进行编码
Result: string
-----------------------------------------------------------------------------}
function Tdm.ConvertText(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;
{-----------------------------------------------------------------------------
Procedure: Tdm.ExchangeNumber
Author: administrator
Date: 22-四月-2005
Arguments: value: string 号码进行对换
Result: string
-----------------------------------------------------------------------------}
function Tdm.ExchangeNumber(value: string): string;
var rValue:string;
I: Integer;
begin
rValue:='';
i:=2;
if(Copy(Value,1, 2)<>'86')then
Value:='86'+trim(Value);
while i<=length(value) do
begin
rValue:=rValue+value;
rValue:=rValue+Value[i-1];
i:=i+2;
end; // while
if(odd(Length(Value)))then
begin
rValue:=rValue+'F';
rValue:=rValue+Value[Length(Value)];
end;
Result:=Trim(rValue);
end;
%%%%%%%%%%%%%%%%%
接收部分
%%%%%%%%%%%%%%%%%
{-----------------------------------------------------------------------------
Procedure: Tdm.ReadContent
Author: administrator
Date: 22-四月-2005
Arguments: s: string 转换短信的内容
Result: string
-----------------------------------------------------------------------------}
function Tdm.ReadContent(s: string): string;
function codetocontent(s1:string):string;
var rs:string;
kpos: Integer;
begin
rs:='';
kpos:=1;
while kpos<=length(s1) do
begin
rs:=rs+widechar(StrToInt('$'+copy(s1,kpos,4)));
kpos:=kpos+4;
end; // while
Result:=rs;
end;
var rValue:string;
dataLen: Integer;
ipos: Integer;
begin
rValue:='';
ipos:=pos('0D9168',s);
if(ipos>=0)then //含有0D9168
begin
if(length(s)>ipos+38)then //可以取出数据的长度
begin
dataLen:=StrToInt('$'+copy(s,ipos+36, 2))*2; //数据的长度
if(length(s)>(ipos+38+dataLen))then
begin
rValue:=copy(s,ipos+38,dataLen);
rValue:=codetocontent(rValue);
end;
end;
end;
Result:=rValue;
end;
{-----------------------------------------------------------------------------
Procedure: Tdm.ReadDesiNumber
Author: administrator
Date: 22-四月-2005
Arguments: s: string 取出短信的手机号
Result: string
-----------------------------------------------------------------------------}
function Tdm.ReadDesiNumber(s: string): string;
function r_code(s1:string):string;
var
r_number: string;
cpos: Integer;
begin
cpos:=2;
while cpos<=length(s1) do
begin
r_number:=r_number+s1[cpos]+s1[cpos-1];
cpos:=cpos+2;
end; // while
Result:=r_number;
end;
var
cur_tcode: string;
ipos: Integer;
rValue: string;
begin
rValue:='';
ipos:=pos('0D9168',s);
if(ipos>=0)then
begin
if(length(s)>=(ipos+18))then
begin
cur_tcode:=copy(s,ipos+6,12);
rValue:=r_code(cur_tcode);
end;
end;
Result:=copy(rValue,1,11);
end;
硬件使用WAVECOM与串口相连,串口控件使用APRO,使用其数据打包控件直接将数据取出,代码如下
%%%%%%%%%%%
发送部分
%%%%%%%%%%%
{-----------------------------------------------------------------------------
Procedure: Tdm.CombinationData
Author: administrator
Date: 22-四月-2005
Arguments: CenterNum, DesiNum, ContentText: string
将输入的中心号码及目标号码/内容合并为可用数据
Result: string
-----------------------------------------------------------------------------}
function Tdm.CombinationData(CenterNum, DesiNum,
ContentText: string): string;
var sendStr:string;
nText:string;
begin
// sendStr:='AT+CMGS=';
sendStr:='';
sendStr:=sendStr+'0891'+Self.ExchangeNumber(centerNum);
sendStr:=sendStr+'11000D91';
sendStr:=sendStr+Self.ExchangeNumber(DesiNum);
sendStr:=sendStr+'000800';
nText:=Self.ConvertText(ContentText);
sendStr:=sendStr+Format('%2.2x',[ord(Length(Trim(nText))div 2)]);
sendStr:=sendStr+Trim(nText);
Result:=sendStr;
end;
{-----------------------------------------------------------------------------
Procedure: Tdm.ConvertText
Author: administrator
Date: 22-四月-2005
Arguments: value: string 对内容进行编码
Result: string
-----------------------------------------------------------------------------}
function Tdm.ConvertText(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;
{-----------------------------------------------------------------------------
Procedure: Tdm.ExchangeNumber
Author: administrator
Date: 22-四月-2005
Arguments: value: string 号码进行对换
Result: string
-----------------------------------------------------------------------------}
function Tdm.ExchangeNumber(value: string): string;
var rValue:string;
I: Integer;
begin
rValue:='';
i:=2;
if(Copy(Value,1, 2)<>'86')then
Value:='86'+trim(Value);
while i<=length(value) do
begin
rValue:=rValue+value;
rValue:=rValue+Value[i-1];
i:=i+2;
end; // while
if(odd(Length(Value)))then
begin
rValue:=rValue+'F';
rValue:=rValue+Value[Length(Value)];
end;
Result:=Trim(rValue);
end;
%%%%%%%%%%%%%%%%%
接收部分
%%%%%%%%%%%%%%%%%
{-----------------------------------------------------------------------------
Procedure: Tdm.ReadContent
Author: administrator
Date: 22-四月-2005
Arguments: s: string 转换短信的内容
Result: string
-----------------------------------------------------------------------------}
function Tdm.ReadContent(s: string): string;
function codetocontent(s1:string):string;
var rs:string;
kpos: Integer;
begin
rs:='';
kpos:=1;
while kpos<=length(s1) do
begin
rs:=rs+widechar(StrToInt('$'+copy(s1,kpos,4)));
kpos:=kpos+4;
end; // while
Result:=rs;
end;
var rValue:string;
dataLen: Integer;
ipos: Integer;
begin
rValue:='';
ipos:=pos('0D9168',s);
if(ipos>=0)then //含有0D9168
begin
if(length(s)>ipos+38)then //可以取出数据的长度
begin
dataLen:=StrToInt('$'+copy(s,ipos+36, 2))*2; //数据的长度
if(length(s)>(ipos+38+dataLen))then
begin
rValue:=copy(s,ipos+38,dataLen);
rValue:=codetocontent(rValue);
end;
end;
end;
Result:=rValue;
end;
{-----------------------------------------------------------------------------
Procedure: Tdm.ReadDesiNumber
Author: administrator
Date: 22-四月-2005
Arguments: s: string 取出短信的手机号
Result: string
-----------------------------------------------------------------------------}
function Tdm.ReadDesiNumber(s: string): string;
function r_code(s1:string):string;
var
r_number: string;
cpos: Integer;
begin
cpos:=2;
while cpos<=length(s1) do
begin
r_number:=r_number+s1[cpos]+s1[cpos-1];
cpos:=cpos+2;
end; // while
Result:=r_number;
end;
var
cur_tcode: string;
ipos: Integer;
rValue: string;
begin
rValue:='';
ipos:=pos('0D9168',s);
if(ipos>=0)then
begin
if(length(s)>=(ipos+18))then
begin
cur_tcode:=copy(s,ipos+6,12);
rValue:=r_code(cur_tcode);
end;
end;
Result:=copy(rValue,1,11);
end;