Pessoal,
Bom dia!
Sou novo no lazarus e estou me aventurando em programação para WinCE, até o momento está tudo indo bem, mas estou tentando organizar meu código.
Na verdade tenho um terminal que roda WinCE e se comunica com um equipamento por porta serial, se eu colocar tudo em somente uma Unit funciona mas, para organizar meu código, criei uma Unit PortaSerial e estou recebendo o seguinte erro quando compilo:
"portaserial.pas(92,25) Error: constructors, destructors and class operators must be methods"
Segue meu código da Unit portaserial
unit PortaSerial;
{$mode objfpc}{$H+}
interface
procedure OpenPort(ComPort:String;BaudRate,ByteSize,Parity,StopBits:integer);
procedure StartListening();
implementation
uses
Windows, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, EditBtn, Calendar, Spin, ComCtrls, ExtDlgs,
MaskEdit, DbCtrls,Unit1;
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;
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.