Hvordan udnyttes en event fra en underklasse?
TComport er installeret i VCL og foretager RS232 kommunikation, i dette tilfælde til en afstandsmåler.OnRxFlag eventen ønskes ydnyttet i den vedlagte Unit1. OnRxFlag bliver kaldt når der modtages en bestemt karakter over Com1 porten.
Hvordan gøres dette?
TComPort = class(TComponent)
private
{..}
FOnRxChar: TRxCharEvent;
{..}
procedure DoOnRxChar;
{..}
protected
public
published
property OnRxFlag: TNotifyEvent read FOnRxFlag write FOnRxFlag;
end;
(*****************************************)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Comms;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TLaser = class(TComport)
private
Korrektion : Integer;
public
Constructor Init(Nr : Integer);
function SkrivVar : Integer;
published
end;
var
Form1: TForm1;
Laser: array [1..2] of TLaser;
implementation
{$R *.DFM}
Constructor TLaser.Init(Nr : Integer);
begin
Korrektion:=1000;
Create(Laser[Nr]);
if Nr=1 then Port:=COM1 else port:=COM2;
BaudRate:=br9600;
DataBits:=dbEight;
EventChar:=10;
end;
function TLaser.SkrivVar : Integer;
begin
Result:=Korrektion;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Laser[1]:=TLaser.Init(1);
Edit1.Text:=IntToStr(Laser[1].SkrivVar);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Laser[1].Open;
Laser[1].WriteString('g'+chr(13),True);
end;
end.