S
sad_sand
Unregistered / Unconfirmed
GUEST, unregistred user!
由于应用需要,我创建了一个线程,通过spcomm来发送短信,但最后一条短信总发不出去,最后的回执‘OK’总要在下一条短信的第一条指令发出去后才能收到。不知是什么原因,请大家帮我看看。谢谢啦!(附部分代码)
type
TCDMAThread = class(TThread) //CDMA发信息线程
private
{ Private declarations }
aSend_addr:array of string;
aContext:string;
aifencode:Boolean;
aComm:TComm;
protected
procedure Execute; override;
public
Constructor Create(Send_addr:array of string;Context:string;ifencode:Boolean;Comm:TComm);
end;
procedure Send_CDMAinfo(Send_addr:array of string; Context:string; ifencode:Boolean;CDMAComm:TComm);
var
SendStrChar;
charlist:array[0..200]of char;
ch:array of Char;
CommFlag:boolean; //标志发送AT指令是否成功
f:integer;
ach:char;
tmpstr,data,s1,s2,s3:string;
str:array of string;
i,j,g,strlen,tick:integer;
k,n:boolean;
begin
CommFlag:=true;
data:='';
if ifencode=true then
begin
//加密
data:=des_encode(Context);
strlen:=length(data); //内容长度计算(data的长度是8的倍数)
// data:=DivHex(data); //测试解密用
// s3:=des_decode(data);
end
else
begin
tmpstr:=UnicodeHex(Context);
data:=DivHex(tmpstr);
strlen:=length(data)+length(tmpstr);
end;
setlength(str,length(Send_addr)*2);
i:=0;
j:=0;
while i<length(Send_addr)*2-1 do
begin
str:='AT+CMGS="'+Send_addr[j]+'",'+inttostr(strlen)+#13;
setlength(ch,length(str));
CopyMemory(@ch[0], @str[1], Length(str));
if not CDMAcomm.WriteCommData(pchar(ch),Length(str)) then
begin
Commflag:=false;
application.MessageBox(PChar('Send' + SendStr+ 'error!'),'失败',MB_OK);
exit;
end;
Application.ProcessMessages;
sleep(500);
str[i+1]:=data+divhex(#13+#26);
setlength(ch,length(str[i+1]));
CopyMemory(@ch[0], @str[i+1][1], Length(str[i+1]));
if not CDMAcomm.WriteCommData(pchar(ch),Length(str[i+1])) then
begin
Commflag:=false;
application.MessageBox(PChar('Send' + SendStr+ 'error!'),'失败',MB_OK);
exit;
end;
Application.ProcessMessages;
sleep(1000);
i:=i+2;
j:=j+1;
end;
end;
end;
procedure TMainForm.CDMACommReceiveData(Sender: TObject; Buffer: Pointer; //CDMA接收信息
BufferLength: Word);
var
i,k,iMsgIndex,WordPos,CharPos:integer; //WordPos将接收信息分成语句;CharPos分析句子成分
// ReStr,s,sord,sread,sPhone,sInitTime,sEndTime,sContent,TmpStr:string;
ReStr,s,sOrd,sSend_Phone,sSend_Date,sSend_Time,sRe_Time,sIfread,sContent,TmpStr,len:string;
str:array of string;
stop:boolean; //true:标志拆分语句结束
begin
ReStr:='';
s:='';
Setlength(ReStr,BufferLength);
Move(Buffer^,PChar(ReStr)^,BufferLength);
Restr:=deletenullchar(Restr);
// 信息处理 i:=0;
end;
type
TCDMAThread = class(TThread) //CDMA发信息线程
private
{ Private declarations }
aSend_addr:array of string;
aContext:string;
aifencode:Boolean;
aComm:TComm;
protected
procedure Execute; override;
public
Constructor Create(Send_addr:array of string;Context:string;ifencode:Boolean;Comm:TComm);
end;
procedure Send_CDMAinfo(Send_addr:array of string; Context:string; ifencode:Boolean;CDMAComm:TComm);
var
SendStrChar;
charlist:array[0..200]of char;
ch:array of Char;
CommFlag:boolean; //标志发送AT指令是否成功
f:integer;
ach:char;
tmpstr,data,s1,s2,s3:string;
str:array of string;
i,j,g,strlen,tick:integer;
k,n:boolean;
begin
CommFlag:=true;
data:='';
if ifencode=true then
begin
//加密
data:=des_encode(Context);
strlen:=length(data); //内容长度计算(data的长度是8的倍数)
// data:=DivHex(data); //测试解密用
// s3:=des_decode(data);
end
else
begin
tmpstr:=UnicodeHex(Context);
data:=DivHex(tmpstr);
strlen:=length(data)+length(tmpstr);
end;
setlength(str,length(Send_addr)*2);
i:=0;
j:=0;
while i<length(Send_addr)*2-1 do
begin
str:='AT+CMGS="'+Send_addr[j]+'",'+inttostr(strlen)+#13;
setlength(ch,length(str));
CopyMemory(@ch[0], @str[1], Length(str));
if not CDMAcomm.WriteCommData(pchar(ch),Length(str)) then
begin
Commflag:=false;
application.MessageBox(PChar('Send' + SendStr+ 'error!'),'失败',MB_OK);
exit;
end;
Application.ProcessMessages;
sleep(500);
str[i+1]:=data+divhex(#13+#26);
setlength(ch,length(str[i+1]));
CopyMemory(@ch[0], @str[i+1][1], Length(str[i+1]));
if not CDMAcomm.WriteCommData(pchar(ch),Length(str[i+1])) then
begin
Commflag:=false;
application.MessageBox(PChar('Send' + SendStr+ 'error!'),'失败',MB_OK);
exit;
end;
Application.ProcessMessages;
sleep(1000);
i:=i+2;
j:=j+1;
end;
end;
end;
procedure TMainForm.CDMACommReceiveData(Sender: TObject; Buffer: Pointer; //CDMA接收信息
BufferLength: Word);
var
i,k,iMsgIndex,WordPos,CharPos:integer; //WordPos将接收信息分成语句;CharPos分析句子成分
// ReStr,s,sord,sread,sPhone,sInitTime,sEndTime,sContent,TmpStr:string;
ReStr,s,sOrd,sSend_Phone,sSend_Date,sSend_Time,sRe_Time,sIfread,sContent,TmpStr,len:string;
str:array of string;
stop:boolean; //true:标志拆分语句结束
begin
ReStr:='';
s:='';
Setlength(ReStr,BufferLength);
Move(Buffer^,PChar(ReStr)^,BufferLength);
Restr:=deletenullchar(Restr);
// 信息处理 i:=0;
end;