Q
qiuqiu
Unregistered / Unconfirmed
GUEST, unregistred user!
我用以下完整程序从串口接收数据(从单片机发出:2400波特,1位STOP,8位数据位,无校验)却根本收不到数据,EDIT1能显示OK,即COM1可以打开;EDIT2却无法显示YES,即根本无RECEIVEDATA事件,再接着就是死机,只有重启动,请各位大虾帮忙调试,看看问题究竟出在哪里。(硬件绝对正确,因为我用汇编调试过,可以收到数据)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Comm;
type
TForm1 = class(TForm)
Comm1: TComm;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
try
comm1.StopComm;
Comm1.startcomm;//创建窗体时,将comm1控件打开。
//Sendtimer.Enabled :=true;
edit1.Text:='ok';
except
showmessage('wrong');
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
viewstring:string;
i:integer;
receivebuffer:array [0..200] of char;
begin
edit2.Text:='yes';
Move(Buffer^,pchar(@ReceiveBuffer)^, 20);
viewstring:='';
for i:=0 to 19 do
viewstring:=viewstring+inttohex(strtoint(ReceiveBuffer),2)+' ';
viewstring:='接收'+viewstring;
Memo1.Lines.Add(viewstring);
Memo1.Lines.Add('');
Memo1.Invalidate;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Comm;
type
TForm1 = class(TForm)
Comm1: TComm;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
try
comm1.StopComm;
Comm1.startcomm;//创建窗体时,将comm1控件打开。
//Sendtimer.Enabled :=true;
edit1.Text:='ok';
except
showmessage('wrong');
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
viewstring:string;
i:integer;
receivebuffer:array [0..200] of char;
begin
edit2.Text:='yes';
Move(Buffer^,pchar(@ReceiveBuffer)^, 20);
viewstring:='';
for i:=0 to 19 do
viewstring:=viewstring+inttohex(strtoint(ReceiveBuffer),2)+' ';
viewstring:='接收'+viewstring;
Memo1.Lines.Add(viewstring);
Memo1.Lines.Add('');
Memo1.Invalidate;
end;
end.