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