WINDOWS95与NT下微机与智能化仪的串行通信设计*下*
图2 3构件源程序 ......
图 2
3 构件源程序
unit comm32;
interface
uses
Windows,Messages,SysUtils,Classes, Graphics, Controls, Forms, Dialogs;
const
WMCOMMNOTIFY = WMUSER + 1;
Type{定义属性用梅举类型}
TParity = ( None, Odd, Even, Mark, Space );
TStopBits = (1, 15, 2 );
TOncommMode = (evchar,evflag);
TComPorts=( com1,com2,com3,com4);
ECommsError = class( Exception );
TOncommEvent = procedure(Sender: TObject;Buffer:Pointer;BufferLength: Word) of
object;{触发事件对像}
Type{创建监视线程类}
TMyCommWacth = class(TThread)
private
PostEvent: Integer;
{ Private declarations }
本文有[www.0574-laser.com]提供,请及时关注[www.0574-laser.com]提供的内容
protected
procedure Execute; override;
Public
hCommFile: THandle;{串口句柄}
hCloseEvent: THandle; {事件句柄}
hComm32Window:THandle;{消息窗句柄}
Lpoverlapped:TOVERLAPPED;
ConStructor Create;{构造函数}
end;
type{创建构件对象}
Tcomm32 = class(TComponent)
Private{定义属性的私有变量}
MyComThread: TMyCommWacth;
BaudRates: Integer;
comName: TComPorts;
parity: TParity ;
Stopbits : TStopBits ;
DataBits : Byte;
InPutbuffers: Integer;
OutPutbuffers: Integer;
commMode: TOncommMode;
OnCommMsg: TOnCommEvent;
procedure CommWndProc( var msg: TMessage );message WMCOMMNOTIFY;
{ Private declarations }
protected
procedure OnCommData(Buffer: PChar; BufferLength: Word);
{ Protected declarations }
public{运行属性}
hCommFile: THandle;
hCloseEvent: THandle;
hComm32Window:THandle;
Function ComPortOpen : Thandle;
Function ComPortClose : Boolean;
procedure CloseMyComThread;
Constructor
Create(Aowner:TComponent);override;
destructor Destroy; override;
{ Public declarations }
published{可视属性及事件}
property comParity: TParity read Parity Write Parity default None;
property ComPortName:TComPorts read comName Write comName default com2;
property BaudRate:Integer read BaudRates Write BaudRates default 9600 ;
property Stopbit:TStopBits read Stopbits Write Stopbits default1;
property ByteDataBit:Byte read DataBits Write DataBits default 8;
property InBuffersize: Integer read InPutbuffers Write InPutbuffers default 1024;
property OutBuffersize:Integer read OutPutbuffers Write OutPutbuffers default 1024;
property SetComMode:TOncommMode read commMode Write commMode default evChar;
property OnComm:TOnCommEvent read OnCommMsg write OnCommMsg;
end;
procedure Register;
implementation
TMyCommWacth.Create();{监视线程创建}
begin
inherited Create(False);
FreeOnTerminate:=True;
end;
{监视线程执行}
procedure TMyCommWacth.Execute;
Var DwTransfer,DwEvtMask:Integer;
begin
if Comm32.SetComMode = Evchar then
begin
if not SetCommMask(hCommFile,
EVRXCHAR) then Exit;
While( true) do
begin
DwEvtMask:=0;
WaitCommEvent(hCommFile,
DwEvtMask,@Lpoverlapped);
if((DwEvtMaskandEVRXCHAR)
=EVRXCHAR) then
begin
WaitForSingleObject(PostEvent, 1000000);
ResetEVent(PostEvent);
PostMessage(hComm32Window ,WMCOMMNOTIFY,hcommfile,0);
end;
end;
end else
begin
if not setCommMask(hCommFile,
EVRXFLAG) then Exit;
While( true) do
begin
DwEvtMask:=0;
WaitCommEvent(hCommFile,DwEvtMask,@comm32.Lpoverlapped);
if ((DwEvtMask and EVRXFLAG)
=EVRXFLAG) then
begin
WaitForSingleObject(comm32.PostEvent,1000000);
ResetEVent(comm32.PostEvent);
PostMessage(hComm32Window,WMCOMMNOTIFY,hCommFile,NULL);
end;
end;
end;
end; {监视线程结束}
{建立通信构件}
Tcomm32.Create(Aowner:Tcomponent);
begin
inherited Create(aOwner);
MyComThread:= nil;
hCommFile := 0;
hCloseEvent := 0;
Parity:=None;
ComName:=com2;
BaudRates:=9600;
Stopbits:=1;
DataBits:=8;
InPutBuffers:=1024;
OutPutBuffers:=1024;
CommMode:=Evchar;
end;
destructor TComm32.Destroy;{构件析构函数}
begin
if not(csDesigning in ComponentState)then
DeallocateHWnd(hComm32Window);
inherited Destroy;
end;
procedure Register;{构件注册}
begin
本文有[www.0574-laser.com]提供,请及时关注[www.0574-laser.com]提供的内容
RegisterComponents(’Sample’, [Tcomm32]);
end;
procedure TComm32.OnCommData(Buffer: PChar; BufferLength: Word);
begin
if Assigned(OnCommMsg) then
OnCommMsg( self , Buffer, BufferLength);
end;
{构件端口打开方法}
Function TComm32.comPortOpen : Thandle;
var dcbPort:TDCB;
ComBuff:BOOlean;
StrCom:string;