unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Edit1: TEdit;
Button3: TButton;
Button4: TButton;
Label2: TLabel;
Memo1: TMemo;
ComboBox1: TComboBox;
Label3: TLabel;
Button5: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hcom,Post_Event:Thandle;
lpol:_overlapped;
lpdcb:Tdcb;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
closehandle(hcom);
close;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Closehandle(hcom);
hcom:=createfile(Pchar(combobox1.Text),generic_read or generic_write,0,nil,open_existing,file_attribute_normal,0);
getcommstate(hcom,lpdcb);
lpdcb.baudrate:=9600;
lpdcb.ByteSize:=8;
lpdcb.Parity:=MarkParity;
lpdcb.StopBits:=OneStopBit;
setcommstate(hcom,lpdcb);
showmessage('连接成功!');
button1.Enabled:=false;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
dwErrorFlags: DWORD;
ComStat : TComStat;
num,i:integer;
rebuf:byte;
begin
PurgeComm(hcom,(PURGE_RXABORT and PURGE_RXCLEAR));
ClearCommError(hcom,dwErrorFlags,@ComStat);
if comstat.cbInQue>0 then
begin
for i:=0 to comstat.cbInQue-1 do
begin
ReadFile(hcom,reBuf,1,dwErrorFlags,@lpol);
num:=rebuf;
memo1.Text:=memo1.Text+inttostr(num)+' ';
end;
end
else
begin
showmessage('接收已经完成或者没有数据接收!');
end;
edit1.SetFocus;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
lrc
WORD;
ch:integer;
begin
ch:=strtoint(edit1.Text);
writefile(hcom,ch,1,lrc,nil);
edit1.SetFocus;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key<>#13 then
begin
if (not (Key in ['0'..'9'])) then
begin
key:=#0;
end;
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
Closehandle(hcom);
hcom:=createfile(Pchar(combobox1.Text),generic_read or generic_write,0,nil,open_existing,file_attribute_normal,0);
getcommstate(hcom,lpdcb);
lpdcb.baudrate:=9600;
lpdcb.ByteSize:=8;
lpdcb.Parity:=SpaceParity;
lpdcb.StopBits:=OneStopBit;
setcommstate(hcom,lpdcb);
end;
end.
送你个简单的代码,看对你有没帮助,在delphi 7调试通过。