Problemer med events
Hej *Jeg har aldrig rigtig rodet med events før. Jeg har nu lavet en komponent, der skal fyre en event af når den connecter/disconnecter.
Jeg har prøvet at lave nedstående, men den giver access violation omkring mine events.
Hvad gør jeg galt ?
type
TSaproCommunication = class(TComponent)
private
{ Private fields of TSaproCommunication }
{ Storage for property Baudrate }
FBaudrate : String;
{ Storage for property ComPort }
FComPort : TComPorts;
{ Storage for property Connected }
FConnected : Boolean;
{ Storage for property Connection }
FConnection : TConnections;
{ Storage for property IP }
FIP : String;
{ Storage for property Parity }
FParity : String;
{ Storage for property Stopbits }
FStopbits : String;
{ Storage for property Word }
FWord : String;
{ Pointer to application's OnComConnected handler, if any }
FOnComConnected : TNotifyEvent;
{ Pointer to application's OnComDisconnected handler, if any }
FOnComDisconnected : TNotifyEvent;
{ Private methods of TSaproCommunication }
{ Method to set variable and property values and create objects }
procedure AutoInitialize;
{ Method to free any objects created by AutoInitialize }
procedure AutoDestroy;
{ Read method for property Baudrate }
function GetBaudrate : String;
{ Write method for property Baudrate }
procedure SetBaudrate(Value : String);
{ Read method for property ComPort }
function GetComPort : TComPorts;
{ Write method for property ComPort }
procedure SetComPort(Value : TComPorts);
{ Read method for property Connected }
function GetConnected : Boolean;
{ Write method for property Connected }
procedure SetConnected(Value : Boolean);
{ Read method for property Parity }
function GetParity : String;
{ Write method for property Parity }
procedure SetParity(Value : String);
{ Read method for property Stopbits }
function GetStopbits : String;
{ Write method for property Stopbits }
procedure SetStopbits(Value : String);
{ Read method for property Word }
function GetWord : String;
{ Write method for property Word }
procedure SetWord(Value : String);
protected
{ Protected fields of TSaproCommunication }
{ Protected methods of TSaproCommunication }
{ Method to generate OnComConnected event }
procedure ComConnected(Sender : TObject); virtual;
{ Method to generate OnComDisconnected event }
procedure ComDisconnected(Sender : TObject); virtual;
procedure Loaded; override;
public
{ Public fields and properties of TSaproCommunication }
Adr : String;
Conf : ISaproConf;
InterfaceType : String;
OConf : ISaproConf;
OLoad : IRainbowLoader;
OObjH : IRainbowObjectHandler;
OSapro : ISaproSerSrv;
OTask : IRainbowState;
Tmp : Variant;
{ Public methods of TSaproCommunication }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function Execute : Boolean;
published
{ Published properties of TSaproCommunication }
property OnComConnected : TNotifyEvent read FOnComConnected write FOnComConnected;
property OnComDisconnected : TNotifyEvent read FOnComDisconnected write FOnComDisconnected;
property Baudrate : String read GetBaudrate write SetBaudrate;
property ComPort : TComPorts
read GetComPort write SetComPort
default Com1;
property Connected : Boolean
read GetConnected write SetConnected
default False;
property Connection : TConnections read FConnection write FConnection;
property IP : String read FIP write FIP;
property Parity : String read GetParity write SetParity;
property Stopbits : String read GetStopbits write SetStopbits;
property Word : String read GetWord write SetWord;
end;
procedure Register;
implementation
procedure Register;
begin
{ Register TSaproCommunication with Sapro tools as its
default page on the Delphi component palette }
RegisterComponents('Sapro tools', [TSaproCommunication]);
end;
{ Method to set variable and property values and create objects }
procedure TSaproCommunication.AutoInitialize;
begin
FBaudrate := '57600';
FComPort := Com1;
FConnected := False;
FIP := '192.168.10.20';
FParity := 'N';
FStopbits := '1';
FWord := '8';
end; { of AutoInitialize }
{ Method to free any objects created by AutoInitialize }
procedure TSaproCommunication.AutoDestroy;
begin
{ No objects from AutoInitialize to free }
end; { of AutoDestroy }
{ Read method for property Baudrate }
function TSaproCommunication.GetBaudrate : String;
begin
Result := FBaudrate;
end;
{ Write method for property Baudrate }
procedure TSaproCommunication.SetBaudrate(Value : String);
begin
FBaudrate := Value;
end;
{ Read method for property ComPort }
function TSaproCommunication.GetComPort : TComPorts;
begin
Result := FComPort;
end;
{ Write method for property ComPort }
procedure TSaproCommunication.SetComPort(Value : TComPorts);
begin
FComPort := Value;
end;
{ Read method for property Connected }
function TSaproCommunication.GetConnected : Boolean;
begin
Result := FConnected;
end;
{ Write method for property Connected }
procedure TSaproCommunication.SetConnected(Value : Boolean);
var
InterfaceTyp: String;
begin
FConnected := Value;
If FConnected = true then
begin
oSapro := CoRainbowSrv.Create;
oTask := CoRainbowState.Create;
oLoad := CoRainbowLoader.Create;
oOBJH := CoRainbowObjectHandler.Create;
If FConnection = Seriel then
begin
if FAILED(oSapro.QueryInterface(IID_ISaproConf, conf)) then
Showmessage('Sapro Comm - QueryInterface failed')
else
begin
If FComPort = Com1 Then
InterfaceTyp := 'RainbowSerSrv.CommModule01';
If FComPort = Com2 Then
InterfaceTyp := 'RainbowSerSrv.CommModule02';
Try
conf.SetInterfaceType(InterfaceTyp);
conf.SetInterfacePar('');
conf.SetInterfacePar(BaudRate+','+Word+',2,'+Parity+','+StopBits);
conf.SetTargetType(6);
oTask.SetInterface(oSapro);
oLoad.SetInterface(oSapro);
oObJH.SetInterface(oSapro,0);
FOnComConnected(self);
except
Begin
Showmessage('Sapro Comm - Settting interface failed');
oSapro := Nil;
oTask := Nil;
oLoad := Nil;
conf.SetInterfacePar('');
end;
end;
end;
end;
end
else
begin
//Disconnect
oSapro := Nil;
oTask := Nil;
oLoad := Nil;
conf.SetInterfacePar('');
FOnComDisConnected(self);
end;
end;
{ Read method for property Parity }
function TSaproCommunication.GetParity : String;
begin
Result := FParity;
end;
{ Write method for property Parity }
procedure TSaproCommunication.SetParity(Value : String);
begin
FParity := Value;
end;
{ Read method for property Stopbits }
function TSaproCommunication.GetStopbits : String;
begin
Result := FStopbits;
end;
{ Write method for property Stopbits }
procedure TSaproCommunication.SetStopbits(Value : String);
begin
FStopbits := Value;
end;
{ Read method for property Word }
function TSaproCommunication.GetWord : String;
begin
Result := FWord;
end;
{ Write method for property Word }
procedure TSaproCommunication.SetWord(Value : String);
begin
FWord := Value;
end;
{ Method to generate OnComConnected event }
procedure TSaproCommunication.ComConnected(Sender : TObject);
begin
{ Has the application assigned a method to the event, whether
via the Object Inspector or a run-time assignment? If so,
execute that method }
if Assigned(FOnComConnected) then
FOnComConnected(Sender);
end;
{ Method to generate OnComDisconnected event }
procedure TSaproCommunication.ComDisconnected(Sender : TObject);
begin
{ Has the application assigned a method to the event, whether
via the Object Inspector or a run-time assignment? If so,
execute that method }
if Assigned(FOnComDisconnected) then
FOnComDisconnected(Sender);
end;
constructor TSaproCommunication.Create(AOwner: TComponent);
begin
{ Call the Create method of the parent class }
inherited Create(AOwner);
{ AutoInitialize sets the initial values of variables and }
{ properties; also, it creates objects for properties of }
{ standard Delphi object types (e.g., TFont, TTimer, }
{ TPicture) and for any variables marked as objects. }
{ AutoInitialize method is generated by Component Create. }
AutoInitialize;
{ Code to perform other tasks when the component is created }
end;
destructor TSaproCommunication.Destroy;
begin
{ AutoDestroy, which is generated by Component Create, frees any }
{ objects created by AutoInitialize. }
AutoDestroy;
{ Here, free any other dynamic objects that the component methods }
{ created but have not yet freed. Also perform any other clean-up }
{ operations needed before the component is destroyed. }
{ Last, free the component by calling the Destroy method of the }
{ parent class. }
inherited Destroy;
end;
function TSaproCommunication.Execute : Boolean;
begin
{ Perform the component operation }
{ Return True if the operation was successful, False otherwise }
Result := True
end;
procedure TSaproCommunication.Loaded;
begin
inherited Loaded;
{ Perform any component setup that depends on the property
values having been set }
end;
