Sorry I´m new to PASCAL and I´m using Lazarus to program for Windows CE

I dont know why I´m getting the error Error: constructors, destructors and class operators must be methods

I have my Unit1 where I´m declaring my form procedures as bellow

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  PortaSerial;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure OnRx(str : string);

  //  procedure PortaSerial.OpenPort('COM5:',9600,8,0,0);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
      PortaSerial.OpenPort('COM5:',9600,8,0,0);
      PortaSerial.StartListening();
end;

procedure TForm1.OnRx(str : string);
var posShell, posMsg, i : integer;
    macro : string;
begin

      Memo1.Caption := Memo1.Caption + str;
end;

end.   

And I created another Unit to declare my serial port procedures and thread

unit PortaSerial;

{$mode objfpc}{$H+}

interface
procedure OpenPort(ComPort:String;BaudRate,ByteSize,Parity,StopBits:integer);
procedure StartListening();

implementation
uses
    Windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
    StdCtrls, Buttons, ExtCtrls, EditBtn, Calendar, Spin, ComCtrls, ExtDlgs,
    MaskEdit, DbCtrls,Unit1;
type
    { TMyThread }

  TRxCOMThread = class(TThread)
  private
    procedure ReceiveBytes;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: boolean);
  end;

  procedure OpenPort(ComPort:String;BaudRate,ByteSize,Parity,StopBits:integer);
  procedure StartListening();



var
   Connected : boolean = false;
   hComPort: THandle;
   RxCOMThread : TRxCOMThread;


//===================== Abre porta serial ====================================
//=================================================================== OPEN PORT
procedure OpenPort(ComPort:String;BaudRate,ByteSize,Parity,StopBits:integer);
var
  cc:TCOMMCONFIG;
  SWide:WideString;
  Port:LPCWSTR;

  begin
    SWide:=ComPort;
    Port:=PWideChar(SWide);
    if (true) then
    begin
      Connected:=False;
      hComPort:=CreateFile(Port, GENERIC_READ or GENERIC_WRITE,0, nil,OPEN_EXISTING,0,0);
      if (hComPort = INVALID_HANDLE_VALUE) then
      begin
        ShowMessage('Fail to Open');
        exit;
      end;
      GetCommState(hComPort,cc.dcb);
      cc.dcb.BaudRate:=BaudRate;
      cc.dcb.ByteSize:=ByteSize;
      cc.dcb.Parity:=Parity;
      cc.dcb.StopBits:=StopBits;
      if not SetCommState(hComPort, cc.dcb) then
      begin
        ShowMessage('SetCommState Error!');
        CloseHandle(hComPort);
        exit;
      end;
      Connected:=True;
    end;
  end;
//==============================================================================
//============================ END ================================== OPEN PORT

//================================== START LISTENING ===========================
//==============================================================================
procedure StartListening();
begin
  RxCOMThread := TRxCOMThread.Create(True); // True = doesn't start automatically
  if Assigned(RxCOMThread.FatalException) then
    raise RxCOMThread.FatalException;
  // Inicializa a Thread
  RxCOMThread.Resume;
end;
//==============================================================================
//============================ END =========================== START LISTENING



{ TMyThread }                                                     // RX THREAD

//=====================================================================
constructor TRxCOMThread.Create(CreateSuspended: boolean);
begin
  FreeOnTerminate := True;
  inherited Create(CreateSuspended);
end;
//==============================================================================
//=====================================================================
procedure TRxCOMThread.ReceiveBytes;
// this method is only called by Synchronize(@ShowStatus) and therefore
// executed by the main thread
// The main thread can access GUI elements, for example Form1.Caption.
var
  rxChar : char;
  dwBytesTransferred : DWORD;
  texto : string = '';
  ticks : DWORD;
begin
  repeat
  begin
    ReadFile (hComPort, &rxChar, 1, &dwBytesTransferred, nil);
    if (dwBytesTransferred = 1) then
      begin
        texto := texto + rxChar;
        ticks := GetTickCount;
      end;
  end;
  until (dwBytesTransferred <> 1) and ( (GetTickCount - ticks) > 100 );
 Form1.OnRx(texto);    //retorna o texto

end;
//==============================================================================
//=====================================================================
procedure TRxCOMThread.Execute;
var
  newStatus : string;
  mask: DWORD;
begin
  SetCommMask( hComPort, EV_RXCHAR);
  while (not Terminated) and (Connected) do begin
    WaitCommEvent(  hComPort,  &mask,  nil);
    if mask = EV_RXCHAR then Synchronize(@ReceiveBytes);
  end;
end;
//==============================================================================


end.

When I compile I got the error "portaserial.pas(92,25) Error: constructors, destructors and class operators must be methods"

If everything is in only one Unit it works.

Regards

My freepascal isn't that good. The error message is described here and basically states that the method definition should include class. I'll try later to reproduce this. On what line does it show this message (line number above)?

pritaeas,

Hmm I´m sorry, I´m new to pascal, isnt my class constructor the bellow code?

type
    { TMyThread }

  TRxCOMThread = class(TThread)
  private
    procedure ReceiveBytes;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: boolean);
  end;                

Sorry,

I didnt understand you.

I just tried and it returned

Error: Class constructors can't have parameters

I copied your code to two units, and got the same error when compiling. The only way I got rid of it was to delete all methods and functions, except the constructor, move the class definition to the interface section, and then recreate the removed functions/methods.

I´m sorry I still couldnt compile can you please copy how your interface and type was?

Thanks!

The problem is that if I move the class definition TRxCOMThread = class(TThread) to the Interface section I got this "Error: Identifier not found "TThread""

I know that this is because this is declared before my "Uses".

I´m sorry I´m new to Pascal. =/

same error =(

unit PortaSerial;

{$mode objfpc}{$H+}



interface
uses Classes;
procedure OpenPort(ComPort:String;BaudRate,ByteSize,Parity,StopBits:integer);
procedure StartListening();
type
    { TMyThread }

  TRxCOMThread = class(TThread)
  public
    procedure ReceiveBytes;
  public
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: boolean);
  end;   

Ok!

Problem solved =)

unit PortaSerial;

{$mode objfpc}{$H+}

interface

uses
    Windows, Classes,SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
    StdCtrls, Buttons, ExtCtrls, EditBtn, Calendar, Spin, ComCtrls, ExtDlgs,
    MaskEdit, DbCtrls;
type
  { TMyThread }

TRxCOMThread = class(TThread)
public
  procedure ReceiveBytes;
public
  procedure Execute; override;
public
  constructor Create(CreateSuspended: boolean);
end;

var
 Connected : boolean = false;
 hComPort: THandle;
 RxCOMThread : TRxCOMThread;

procedure OpenPort(ComPort:String;BaudRate,ByteSize,Parity,StopBits:integer);
procedure StartListening();
procedure SendString(str:String);


implementation
This question has already been answered. Start a new discussion instead.