Avatar billede koonz Nybegynder
03. september 2004 - 09:55 Der er 2 kommentarer og
1 løsning

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;
Avatar billede koonz Nybegynder
03. september 2004 - 09:56 #1
Skal måske lige sige, at det drejer sig om de 2 events:

FOnComConnected(self);
FOnComDisconnected(self);
Avatar billede hrc Mester
03. september 2004 - 12:51 #2
Kunne næsten sige - Find Holger... Flot og gennemkommenteret kode - med to smuttere. Den ene er, at kommentarer er en plage når de forklarer det selvfølgelige:

{ private section }
private
  ..

Det er p.... irriterende at læse - så det har jeg fjernet. Dernæst har jeg rykket lidt ud og ind på koden - men ikke meget. Jeg har også fjernet en masse unødvendige Set og Get procedurer (du har vist arbejdet med C++, hva?) - igen en masse unødvendig støj.

Sidste fejlene: Endelig har jeg sat events'ene til nil og sørget for at de to steder hvor du kaldte dem direkte, nu kører gennem de to metoder som du også har lavet.

Men det var flot kodet - har selv præsteret at lave et program hvor kommentarerne fyldte mere end mine kodelinier.

Har ikke testet det men tror det virker:

type
  TSaproCommunication = class(TComponent)
  private
    FBaudrate : String;
    FComPort : TComPorts;
    FConnected : Boolean;
    FConnection : TConnections;
    FIP : String;
    FParity : String;
    FStopbits : String;
    FWord : String;
    FOnComConnected : TNotifyEvent;
    FOnComDisconnected : TNotifyEvent;
    procedure AutoInitialize;
    procedure AutoDestroy;
  protected
    procedure ComConnected(Sender : TObject); virtual;
    procedure ComDisconnected(Sender : TObject); virtual;
    procedure Loaded; override;
    procedure SetConnected(Value : Boolean);
  public
    Adr : String;
    Conf : ISaproConf;
    InterfaceType : String;
    OConf : ISaproConf;
    OLoad : IRainbowLoader;
    OObjH : IRainbowObjectHandler;
    OSapro : ISaproSerSrv;
    OTask : IRainbowState;
    Tmp : Variant;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function Execute : Boolean;
  published
    property OnComConnected : TNotifyEvent read FOnComConnected write FOnComConnected;
    property OnComDisconnected : TNotifyEvent read FOnComDisconnected write FOnComDisconnected;
    property Baudrate : String read fBaudrate write fBaudrate;
    property ComPort : TComPorts read fComPort write fComPort default Com1;
    property Connected : Boolean read fConnected write SetConnected default False;
    property Connection : TConnections read FConnection write FConnection;
    property IP : String read FIP write FIP;
    property Parity : String read fParity write fParity;
    property Stopbits : String read fStopbits write fStopbits;
    property Word : String read fWord write fWord;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Sapro tools', [TSaproCommunication]);
end;

procedure TSaproCommunication.AutoInitialize;
begin
  FBaudrate := '57600';
  FComPort := Com1;
  FConnected := False;
  FIP := '192.168.10.20';
  FParity := 'N';
  FStopbits := '1';
  FWord := '8';
end;

procedure TSaproCommunication.AutoDestroy;
begin
end;

procedure TSaproCommunication.SetConnected(Value : Boolean);
var
  InterfaceTyp: String;
begin
  FConnected := Value;
  If FConnected 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);
          ComConnected(self);
        except
          Showmessage('Sapro Comm - Settting interface failed');
          oSapro := Nil;
          oTask  := Nil;
          oLoad  := Nil;
          conf.SetInterfacePar('');
        end;
      end;
    end;
  end
  else
  begin //Disconnect
    oSapro := Nil;
    oTask  := Nil;
    oLoad  := Nil;
    conf.SetInterfacePar('');
    ComDisConnected(self);
  end;
end;

procedure TSaproCommunication.ComConnected(Sender : TObject);
begin
  if Assigned(FOnComConnected) then
    FOnComConnected(Sender);
end;

procedure TSaproCommunication.ComDisconnected(Sender : TObject);
begin
  if Assigned(FOnComDisconnected) then
    FOnComDisconnected(Sender);
end;

constructor TSaproCommunication.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FOnComConnected := nil;
  FOnComDisconnected := nil;

  { 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
  try
    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.                                                  }
  finally
    inherited Destroy;
  end;
end;

function TSaproCommunication.Execute : Boolean;
begin
  { Perform the component operation }
  { Return True upon success, otherwise false }
  Result := True;
end;

procedure TSaproCommunication.Loaded;
begin
  inherited Loaded;
  { Perform any component setup that depends on the property values having been set }
end;
Avatar billede koonz Nybegynder
03. september 2004 - 14:28 #3
Takker for hjælpen og den fine ros ;)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester