关于在多线程中使用spcomm的一个问题 ( 积分: 50 )

  • 主题发起人 主题发起人 sad_sand
  • 开始时间 开始时间
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
SendStr:pChar;
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=&quot;'+Send_addr[j]+'&quot;,'+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;
 
由于应用需要,我创建了一个线程,通过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
SendStr:pChar;
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=&quot;'+Send_addr[j]+'&quot;,'+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;
 
可能个人习惯不同,我做的话会这么写:
按键事件:发送第一条短信->启动超时定时器
接收事件:接收前次短信SEND OK->发送下条短信->启动超时定时器
定时器事件:显示接收超时报警
 

Similar threads

I
回复
0
查看
719
import
I
I
回复
0
查看
806
import
I
I
回复
0
查看
687
import
I
I
回复
0
查看
675
import
I
后退
顶部