Y
yz_wxd
Unregistered / Unconfirmed
GUEST, unregistred user!
小弟初学用spcom写了个从COM口接收数据的例子,但在memo中却收不到数据,请各
位专家帮小弟改改,代码如下:
unit test1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, SPComm;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Timer1: TTimer;
Comm1: TComm;
ComboBox1: TComboBox;
Button3: TButton;
SaveDialog1: TSaveDialog;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
// procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
s: string;
begin
SetLength(S, BufferLength);
Move(Buffer^, PChar(S)^, BufferLength);
Memo1.Lines.Add(S);
Memo1.Invalidate;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
comm1.CommName:=combobox1.Text;
comm1.StartComm;
button1.Enabled:=false;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
comm1.StopComm;
savedialog1.Execute;
memo1.Lines.SaveToFile(savedialog1.filename);
memo1.Clear;
button1.Enabled:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Clear;
end;
end.
位专家帮小弟改改,代码如下:
unit test1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, SPComm;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Timer1: TTimer;
Comm1: TComm;
ComboBox1: TComboBox;
Button3: TButton;
SaveDialog1: TSaveDialog;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
// procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
s: string;
begin
SetLength(S, BufferLength);
Move(Buffer^, PChar(S)^, BufferLength);
Memo1.Lines.Add(S);
Memo1.Invalidate;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
comm1.CommName:=combobox1.Text;
comm1.StartComm;
button1.Enabled:=false;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
comm1.StopComm;
savedialog1.Execute;
memo1.Lines.SaveToFile(savedialog1.filename);
memo1.Clear;
button1.Enabled:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Clear;
end;
end.