unit OBIT_Thread;
interface
uses Windows, Classes, FECOM, FERWA;
Type
T_2 =
Array [0 .. 1]
of AnsiChar;
T_256 =
Array [0 .. 255]
of AnsiChar;
TTagStatus =
Record
Key: Double;
Schrank: Integer;
Locked: Boolean;
Valid: Boolean;
Batterie: Integer;
end;
TPollBuffer =
Record
cPollAction: T_2;
cTrAdr: T_2;
cSendPollData: T_256;
cPollValid: T_2;
cRecPollData: T_256;
end;
TCallBackFunc =
Procedure(Status: TTagStatus);
Type
TOBID_Thr =
Class(TThread)
private
PollBuffer: TPollBuffer;
FCallBackFunc: TCallBackFunc;
FPortHandle, FReader: Integer;
FDelay: Integer;
TagStatus: TTagStatus;
FLastKey: Double;
protected
procedure CallCallBack;
Function ReadTag: Boolean;
procedure Debug;
public
procedure Execute;
override;
constructor Create(CallBack: TCallBackFunc; Port: PAnsichar;
Delay: Integer);
Destructor Destroy;
override;
end;
var
ObidThread: TOBID_Thr;
implementation
Uses Dialogs, ConstsAndGlobals;
Constructor TOBID_Thr.Create(CallBack: TCallBackFunc; Port: PAnsichar;
Delay: Integer);
begin
FDelay := Delay;
FPortHandle := FECOM_OpenPort(Port);
FCallBackFunc := CallBack;
if FPortHandle > 0
then
begin
FReader := FERWA_NewReader(FPortHandle);
end;
FLastKey := -1;
inherited Create(False);
FreeOnTerminate:=true;
end;
Destructor TOBID_Thr.Destroy;
begin
ObidThread :=
nil;
Try
if FPortHandle > 0
then
begin
FERWA_DeleteReader(FReader);
FECOM_ClosePort(FPortHandle);
end;
except
end;
inherited Destroy;
end;
Procedure TOBID_Thr.Execute;
var
I: Integer;
begin
I := 0;
While not Terminated
do
begin
try
If ReadTag
then
begin
if FLastKey = -1
then
begin
FLastKey := TagStatus.Key;
Synchronize(CallCallBack);
end;
end
else
FLastKey := -1;
except
inc(I);
if I > 10
then
begin
I := 0;
Synchronize(Debug)
end;
end;
Sleep(FDelay);
end;
end;
procedure TOBID_Thr.Debug;
begin
MessageDLG
(C_OBITNOWORK, mtWarning, [mbok], 0);
Terminate;
end;
procedure TOBID_Thr.CallCallBack;
begin
FCallBackFunc(TagStatus);
end;
Function TOBID_Thr.ReadTag: Boolean;
var
iDataType: Integer;
I: Integer;
d: Double;
begin
PollBuffer.cPollAction := #0#2;
PollBuffer.cTrAdr := #0#2;
I := FERWA_0x01_MultiJobPollAndState(FReader, 0, PollBuffer.cPollAction,
PollBuffer.cTrAdr, PollBuffer.cSendPollData, PollBuffer.cPollValid,
PollBuffer.cRecPollData, 0);
TagStatus.Valid := I = 0;
Result := TagStatus.Valid;
if TagStatus.Valid
then
begin
TagStatus.Key := 0;
d := 1;
For I := 1
to 5
do
begin
TagStatus.Key := TagStatus.Key + Ord(PollBuffer.cRecPollData[6 - I]) * d;
d := d * 256;
end;
PollBuffer.cTrAdr := #0#0;
PollBuffer.cPollAction := #0#2;
I := FERWA_0x03_AdvMultiJobPollAndState(FReader, 0, PollBuffer.cPollAction,
PollBuffer.cTrAdr, PollBuffer.cSendPollData, PollBuffer.cPollValid,
PollBuffer.cRecPollData, 0);
TagStatus.Valid := I = 0;
Result := TagStatus.Valid;
TagStatus.Schrank := Ord(PollBuffer.cRecPollData[2]) * 256 +
Ord(PollBuffer.cRecPollData[3]);
TagStatus.Locked := PollBuffer.cRecPollData[5] = #3;
TagStatus.Batterie := Integer(Ord(PollBuffer.cRecPollData[6]));
FERWA_0x75_SetOutput(FReader, 0, $10, $10, $2, $0);
// BEEP
end;
end;
end.