我用多线程做了个串口通讯的东东,用tccom做的。不知怎么搞的,运行的时候特慢,好象很耗内存,高手请指点指点啊。
主form代码:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, TComm1,Unit2;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
rdcom: TRadioGroup;
Button2: TButton;
Memo2: TMemo;
Button3: TButton;
Comm1: TComm;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
implementation{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
case rdcom.ItemIndex of
0: comm1.CommPort:=pncom1;
1: comm1.CommPort:=pncom2;end;
comm1.portopen:=true;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
comm1.OutputString(memo1.text);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
readthread:=treadthread.create(false);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
readthread.Free;
end;end.线程的代码:
unit Unit2;interfaceuses
Classes;type
treadthread = class(TThread)
private
{ Private declarations }
procedure readport;
protected
procedure Execute; override;
end;
var
readthread:treadthread;implementationuses unit1;procedure treadthread.Execute;
begin
while not terminated do
begin
synchronize(readport);
end;
end;procedure treadthread.readport;
var
temp:string;
begin
temp:=form1.Comm1.Input;
form1.Memo2.Text:=temp;
end;
end.
主form代码:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, TComm1,Unit2;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
rdcom: TRadioGroup;
Button2: TButton;
Memo2: TMemo;
Button3: TButton;
Comm1: TComm;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
implementation{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
case rdcom.ItemIndex of
0: comm1.CommPort:=pncom1;
1: comm1.CommPort:=pncom2;end;
comm1.portopen:=true;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
comm1.OutputString(memo1.text);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
readthread:=treadthread.create(false);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
readthread.Free;
end;end.线程的代码:
unit Unit2;interfaceuses
Classes;type
treadthread = class(TThread)
private
{ Private declarations }
procedure readport;
protected
procedure Execute; override;
end;
var
readthread:treadthread;implementationuses unit1;procedure treadthread.Execute;
begin
while not terminated do
begin
synchronize(readport);
end;
end;procedure treadthread.readport;
var
temp:string;
begin
temp:=form1.Comm1.Input;
form1.Memo2.Text:=temp;
end;
end.
Classes, TComm1;type
treadthread = class(TThread)
private
{ Private declarations }
procedure readport;
temp:string;
Comm1 : TComm;
protected
procedure Execute; override;
end;
var
readthread:treadthread;implementationuses unit1;procedure treadthread.Execute;
begin
Comm1 := TComm.Create(nil);
try
while not terminated do
begin
temp := Comm1.Input;
synchronize(readport);
end;
finally
Comm1.Free;
end;
end;procedure treadthread.readport;
begin
form1.Memo2.Text:=temp;
end;
end.