unit OBID_Thread;
interface
uses Windows,Classes,FECOM,FERWA;
Type
T_2=Array [0..1]
of Char;
T_256=Array [0..255]
of Char;
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:Pchar;Delay:Integer);
Destructor Destroy;
override;
end;
var
ObidThread:TOBID_Thr;
implementation
Uses Dialogs;
Constructor TOBID_Thr.Create(CallBack:TCallBackFunc;Port:Pchar;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);
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;
FreeOnTerminate:=true;
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('
OBID Tagreader funktioniert in aktueller Konfiguration nicht einwandfrei und wird deaktiviert',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.