S
seaoy
Unregistered / Unconfirmed
GUEST, unregistred user!
在线程中处理接收的数据花费的时间较长大概是1000-5000mS,请问怎么解决?
多谢各位!!!
请看以下代码:
TCommThread = Class(TThread)
private
Re:string;
protected
procedure Execute;override;
procedure Receive;
end;
procedure TCommthread.Execute ;
var
dwLength: DWORD;
i,j:integer;
TempByte: Byte;
FirstTickCount:longint;
Change:string;
ChangeV:real;
begin
Re:='';
freeonterminate:=true;
Form1.StatusBar1.Panels[0].Text :='Receiving...';
PurgeComm(CommHandle,(PURGE_RXABORT and PURGE_RXCLEAR and PURGE_TXCLEAR));
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages;
DWLENGTH:= Form1.GetInCount;
IF DWLENGTH >0 then
begin
for i:=1 to dwLengthdo
begin
Form1.ReadData(@TempByte, 1);
re:=re+chr(tempbyte);
FORM1.Memo1.Text :=FORM1.Memo1.Text+' '+inttostr(tempbyte);
end;
end;
until (ORD(re[LENGTH(re)])=13) AND (LENGTH(re)>3);//((GetTickCount-FirstTickCount) >= 5100);
Form1.StatusBar1.Panels[2].Text:=inttostr(GetTickCount-FirstTickCount);
//************************************
FirstTickCount:=GetTickCount;
I:=3;
j:=0;
Change:='';
while I<=Length(Re)do
begin
if ord(re)=13 then
begin
inc(j);
if j<5 then
begin
ChangeV:=strtofloat(Change);
(Form1.GroupBox3.Controls[j-1] as Tedit).text:=currtostr(ChangeV*1000);
end
else
j:=0;
Change:='';
i:=I+3;
end;
Change:=change+re;
inc(i);
end;
// synchronize(Receive);
Form1.StatusBar1.Panels[1].Text:=inttostr(GetTickCount-FirstTickCount);
//****************************************这段须要1000-5000MS不等;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
post_event:=CreateEvent(nil,true,true,nil);
CommThread:=TCommThread.Create(false);
CommThread.Priority :=tpLower;
CommThread.Resume;
SENDSTR('QM 11');
end;
procedure TForm1.Commint(p:string;b:integer);
var
DCB:TDCB;
begin
CommHandle := CreateFile(pchar(p),GENERIC_READ,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL OR FILE_FLAG_OVERLAPPED, 0);
if CommHandle = INVALID_HANDLE_VALUE then
begin
Connected:=false;
Exit;
end;
setupcomm(CommHandle,4096,1024);
GetCommState(CommHandle,DCB);
DCB.BaudRate := b;
DCB.ByteSize := 8;
SetCommState(CommHandle, DCB);
setcommMask(CommHandle,ev_rxchar);
Connected:=true;
end;
procedure TForm1.SendStr(str: String);
var
i:integer;
begin
str := str+Chr(13);
for i:=1 to Length(str)do
TransmitCommChar(commhandle,str);
end;
function TForm1.GetInCount:longint;
var
dwErrorFlags WORD;
ComStat : TComStat;
begin
RESULT:=0;
PurgeComm(CommHandle,(PURGE_RXABORT and PURGE_RXCLEAR));
Clearcommerror(CommHandle,dwErrorFlags,@ComStat);
Result := COMSTAT.cbInQue;
end;
function TForm1.ReadData(pBuffer: PChar;
BufferSize: DWORD): boolean;
var
nRead: DWORD;
begin
Result := ReadFile(CommHandle, pBuffer^, BufferSize, nRead, Nil);
end;
多谢各位!!!
请看以下代码:
TCommThread = Class(TThread)
private
Re:string;
protected
procedure Execute;override;
procedure Receive;
end;
procedure TCommthread.Execute ;
var
dwLength: DWORD;
i,j:integer;
TempByte: Byte;
FirstTickCount:longint;
Change:string;
ChangeV:real;
begin
Re:='';
freeonterminate:=true;
Form1.StatusBar1.Panels[0].Text :='Receiving...';
PurgeComm(CommHandle,(PURGE_RXABORT and PURGE_RXCLEAR and PURGE_TXCLEAR));
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages;
DWLENGTH:= Form1.GetInCount;
IF DWLENGTH >0 then
begin
for i:=1 to dwLengthdo
begin
Form1.ReadData(@TempByte, 1);
re:=re+chr(tempbyte);
FORM1.Memo1.Text :=FORM1.Memo1.Text+' '+inttostr(tempbyte);
end;
end;
until (ORD(re[LENGTH(re)])=13) AND (LENGTH(re)>3);//((GetTickCount-FirstTickCount) >= 5100);
Form1.StatusBar1.Panels[2].Text:=inttostr(GetTickCount-FirstTickCount);
//************************************
FirstTickCount:=GetTickCount;
I:=3;
j:=0;
Change:='';
while I<=Length(Re)do
begin
if ord(re)=13 then
begin
inc(j);
if j<5 then
begin
ChangeV:=strtofloat(Change);
(Form1.GroupBox3.Controls[j-1] as Tedit).text:=currtostr(ChangeV*1000);
end
else
j:=0;
Change:='';
i:=I+3;
end;
Change:=change+re;
inc(i);
end;
// synchronize(Receive);
Form1.StatusBar1.Panels[1].Text:=inttostr(GetTickCount-FirstTickCount);
//****************************************这段须要1000-5000MS不等;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
post_event:=CreateEvent(nil,true,true,nil);
CommThread:=TCommThread.Create(false);
CommThread.Priority :=tpLower;
CommThread.Resume;
SENDSTR('QM 11');
end;
procedure TForm1.Commint(p:string;b:integer);
var
DCB:TDCB;
begin
CommHandle := CreateFile(pchar(p),GENERIC_READ,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL OR FILE_FLAG_OVERLAPPED, 0);
if CommHandle = INVALID_HANDLE_VALUE then
begin
Connected:=false;
Exit;
end;
setupcomm(CommHandle,4096,1024);
GetCommState(CommHandle,DCB);
DCB.BaudRate := b;
DCB.ByteSize := 8;
SetCommState(CommHandle, DCB);
setcommMask(CommHandle,ev_rxchar);
Connected:=true;
end;
procedure TForm1.SendStr(str: String);
var
i:integer;
begin
str := str+Chr(13);
for i:=1 to Length(str)do
TransmitCommChar(commhandle,str);
end;
function TForm1.GetInCount:longint;
var
dwErrorFlags WORD;
ComStat : TComStat;
begin
RESULT:=0;
PurgeComm(CommHandle,(PURGE_RXABORT and PURGE_RXCLEAR));
Clearcommerror(CommHandle,dwErrorFlags,@ComStat);
Result := COMSTAT.cbInQue;
end;
function TForm1.ReadData(pBuffer: PChar;
BufferSize: DWORD): boolean;
var
nRead: DWORD;
begin
Result := ReadFile(CommHandle, pBuffer^, BufferSize, nRead, Nil);
end;