源程序提供如下:
unit comcode;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
const
Wm_commNotify=Wm_user+12;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure comminitialize;
procedure msgcommprocess(var message:tmessage);message wm_commnotify;
{ Private declarations }
public
{ Public declarations }
end;
Tcomm=class(tthread)
protected
procedure Execute;override;
end;
var
Form1: TForm1;
hcom,post_event:Thandle;
lpol
overlapped;
implementation
{$R *.DFM}
procedure Tcomm.Execute;
var
dwevtmask:dword;
wait:boolean;
begin
fillchar(lpol,sizeof(toverlapped),0);
while true do begin
dwevtmask:=0;
wait:=waitcommevent(hcom,dwevtmask,lpol);
if wait then begin
waitforsingleobject(post_event,infinite);
resetevent(post_event);
postmessage(form1.handle,wm_commnotify,0,0);
end;
end;
end;
procedure Tform1.comminitialize;
var
lpdcb:tdcb;
begin
hcom:=createfile('com2',generic_read or generic_write,0,nil,open_existing,
file_attribute_normal or file_flag_overlapped,0);
if hcom=invalid_handle_value then
else
setupcomm(hcom,4096,4096);
getcommstate(hcom,lpdcb);
lpdcb.BaudRate:=2400;
lpdcb.StopBits:=1;
lpdcb.ByteSize:=8;
lpdcb.Parity:=noparity;
setcommstate(hcom,lpdcb);
setcommmask(hcom,ev_rxchar);
end;
procedure TForm1.msgcommprocess(var message: tmessage);
var
clear:boolean;
coms:tcomstat;
readnumber,lperrors:dword;
cbnum:integer;
read_buffer:array[1..100]of char;
begin
clear:=clearcommerror(hcom,lperrors,@coms);
if clear then begin
cbnum:=coms.cbInQue;
readfile(hcom,read_buffer,cbnum,readnumber,lpol);
setevent(post_event);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
comminitialize;
post_event:=createevent(nil,true,true,nil);
tcomm.create(false);
end;
end.
请各位高手各抒己见,不吝赐教,谢谢。。。。。