高分求救~!串口线程问题!
线程已经定义好了,关键是如何使用这个线程。
自己创建的单元如下:
unit ComThread;
interface
uses
Classes, OleCtrls, MSCommLib_TLB,Windows,StdCtrls,QForms,SysUtils;
type
TComThread = class(TThread)
private
{ Private declarations }
Com:TMscomm; ME:TMemo; procedure ComCRTro;
protected
procedure Execute; override;
published
constructor CreateIt(MSCOMM:TMSCOMM;Memo:Tmemo);
end;
implementation
uses ConThread_Test;
var strcall:String;
constructor TComThread.CreateIt(MSCOMM:TMSCOMM;Memo:Tmemo);
begin
inherited Create(True);
Priority:=TThreadPriority(tpTimeCritical);
FreeOnTerminate:=True;
Com:=MSCOMM;
ME:=Memo;
Suspended:=False;
end;
procedure TComThread.ComCRTro;
var t:Dword;
s,a:string;
i,v:Integer;
begin
t:=GetTickCount();
while GetTickCount-t<200 do
Application.ProcessMessages;
a:=Com.Input; //读入数据
s:=Trim(a);
if s<>'' then
ME.Lines.Add(s);
if pos('Income',s)<>0 then //如果收到有短消息返回指令
begin
Com.Output:='read';
end
else if pos('READ',s)<>0 then
begin
Com.Output:='delect';
end;
end;
procedure TComThread.Execute;
begin
Synchronize(ComCRTro);
end;
end.
为了能使用这个线程,我的应用程序如下:
在mscomm的onComm事件添加代码:
procedure TForm1.MSComm1Comm(Sender: TObject);
begin
if MSComm1.CommEvent=2 then
if Test=nil then
Test:=TComThread.CreateIt(mscomm1,memo1);
end;
我的目的是将每个来自串口的数据都add到memo1上去。但实验发现我错了
可能是使用现成的错误,可我错在哪里啊? 请大家帮忙!!