欧美日韩精品在线-无码成人精品区一级毛片-中文一级片-jizzjizz亚洲-综合伊人久久-国产精品一区在线播放-日韩国产综合-国产精品国产三级国产aⅴ原创-亚洲夜夜操-日本亚洲视频-亚州一级-www久久精品-国产精品第-午夜时刻免费入口-欧美亚洲精品在线观看-97高清国语自产拍-超碰女-欧美暧暧视频-99福利在线观看-草草影院在线-成人高潮视频-夜夜摸夜夜操-搞av.com-亚洲开心激情网-黄色网页免费在线观看

寧波市鄞州首南恒宇激光雕刻廠

WINDOWS95與NT下微機與智能化儀的串行通信設(shè)計*下*
分享到:

圖2 3構(gòu)件源程序 ......





圖 2
3 構(gòu)件源程序
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;{觸發(fā)事件對像}
   Type{創(chuàng)建監(jiān)視線程類}
  TMyCommWacth = class(TThread)
  private
   PostEvent: Integer;
   { Private declarations }
本文有[www.0574-laser.com]提供,請及時關(guān)注[www.0574-laser.com]提供的內(nèi)容
  protected
   procedure Execute; override;
  Public
  hCommFile: THandle;{串口句柄}
  hCloseEvent: THandle; {事件句柄}
  hComm32Window:THandle;{消息窗句柄}
   Lpoverlapped:TOVERLAPPED;
   ConStructor Create;{構(gòu)造函數(shù)}
  end;
type{創(chuàng)建構(gòu)件對象}
  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();{監(jiān)視線程創(chuàng)建}
begin
   inherited Create(False);
   FreeOnTerminate:=True;
end;
{監(jiān)視線程執(zhí)行}
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; {監(jiān)視線程結(jié)束}
{建立通信構(gòu)件}
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;{構(gòu)件析構(gòu)函數(shù)}
begin
   if not(csDesigning in ComponentState)then
   DeallocateHWnd(hComm32Window);
   inherited Destroy;
end;
procedure Register;{構(gòu)件注冊}
begin
本文有[www.0574-laser.com]提供,請及時關(guān)注[www.0574-laser.com]提供的內(nèi)容
  RegisterComponents(’Sample’, [Tcomm32]);
end;
procedure TComm32.OnCommData(Buffer: PChar; BufferLength: Word);
begin
   if Assigned(OnCommMsg) then
     OnCommMsg( self , Buffer, BufferLength);
end;
{構(gòu)件端口打開方法}
Function TComm32.comPortOpen : Thandle;
var dcbPort:TDCB;
   ComBuff:BOOlean;
   StrCom:string;

聯(lián)系我們

地址: 浙江省.寧波市鄞州區(qū)寧姜公路(九曲小區(qū)二期旁)

郵編: 315040

聯(lián)系人: 盛立峰

電話: 0574-87139378

傳真: 0574-87139378

手機: 13867861670

郵箱: 85400329@qq.com

聯(lián)系我們