📄 gatherrecthread.pas
字号:
unit GatherRecThread;
interface
uses
SysUtils, Windows, Classes, DB, ADODB, ComObj, Forms,
StrUtils, ActiveX, Dialogs, DateUtils, IniFiles;
type
TGatherRecThread = class(TThread)
private
bComConn,Moudel,BoolAdd: Boolean;
ComInt,RecCount,SleepTime,ReBrushTime,RoadTime,CheckTime: integer;
devIDarr: TStrings;
Record_Data: PChar;
procedure Shark;
procedure show;
procedure AddRec(sCardNo: string; dOpDate: string; sReaderId: string);
procedure AddRecAry(var Brushary: array of String);
procedure CardRec(readid: string; BOOL: boolean);
function Checkday: Integer;
function CreateDB(fn: string): boolean;
function CheckBrushTime(s: string): boolean;
function getalgorism(hexString: String): String;
function DevidHand(devid: integer; Bool: boolean): boolean;
function getCurrentDayFileName: string;
function getnewreadid(readid: string; bushdatetime: TDateTime): string;
protected
procedure Execute; override;
destructor Destroy;
end;
function OpenComm(ComPort: Integer): Integer; stdcall; external 'dascomm.dll';
function CloseComm( ComInt: Integer): Integer; stdcall; external 'dascomm.dll';
function Set_NodeTime(ComInt: Integer; DateTime: PChar): Integer; stdcall; external 'dascomm.dll';
function Hand_Shake(ComInt: Integer; Node_Addr: Integer; Password: PChar): Integer; stdcall; external 'dascomm.dll';
function Get_Curr_Record(ComInt: Integer; Node_Addr: PInteger; Record_Data: PChar): Integer; stdcall; external 'dascomm.dll';
function Get_Next_Record(ComInt: Integer; Node_Addr: PInteger; Record_Data: PChar): Integer; stdcall; external 'dascomm.dll';
function Set_IDAccess_ID(ComInt: Integer; SH: Integer; JiHao: Integer): Integer; stdcall; external 'dascomm.dll';
implementation
uses Unit4;
var
showstr: string;
procedure TGatherRecThread.Execute;
begin
inherited;
FreeOnTerminate := True;
Record_Data := StrAlloc(20 * SizeOf(Char)); //分配 字符空间
Coinitialize(nil);
if ComInt > 0 then
Exit;
Coinitialize(nil);
Unit4.ISWork := true;
Unit4.ISOK := True;
Shark;
end;
procedure TGatherRecThread.Shark; //每天都应该修改inouttime
var
devid,devidstr: string;
i,ComPort: Integer;
BOOL: boolean;
begin
if ComInt <= 0 then
begin
ComPort := StrToInt(IniFile.ReadString('Com', 'value', ''));
ComInt := OpenComm(ComPort - 1);
if ComInt < 0 then
begin
bComConn := false;
Form4.StatusBar1.Panels[1].Text := '串口打开失败!请查看 COM' + IntToStr(ComPort) + ' 是否被其他程序己占用!';
Exit;
end else
bComConn := true;
Form4.StatusBar1.Panels[1].Text := '串口 COM' + IntToStr(ComPort) + ' 连接成功! ';
devidstr := IniFile.ReadString('devIDs', 'value', ''); //机号列表4
SleepTime := StrToInt(IniFile.ReadString('Sleep', 'value', '0')); //取下一条休息多少毫秒
ReBrushTime := StrToInt(IniFile.ReadString('Card.Interval', 'value', '0')); //重复刷卡时间间隔
RoadTime := StrToInt(IniFile.ReadString('Road.Interval', 'value', '10000')); //无回执数据上传时间间隔
CheckTime := StrToInt(IniFile.ReadString('CheckState','value','2000'));
if Inifile.ReadString('Style', 'value', '') = 'T' then RecCount := StrToInt(IniFile.ReadString('Count', 'value', '20')); //保存数据的模式 音箱/动态数组
if IniFile.ReadString('Style', 'value', 'T') = 'T' then Moudel := true else Moudel := false;
devIDarr := TStringList.Create;
try
begin
devIDarr.CommaText := devidstr;
BOOL := false;
while Unit4.ISWork do
begin
if Unit4.ISOK then
begin
for i := 0 to devIDarr.Count-1 do
begin
devid := devIDarr[i];
if not BOOL then
CardRec(devid,true)
else
CardRec(devid,false);
if ComInt <= 0 then
break;
end;
BOOL := true;
Sleep(RoadTime);
end;
Sleep(CheckTime);
end;
end;
finally
freeandnil(devidstr);
end;
end;
end;
procedure TGatherRecThread.show;
begin
Form4.Memo1.Lines.Add(showstr);
end;
procedure TGatherRecThread.AddRecAry(var Brushary: array of String);
var
i: integer;
fn: string;
ADOQuery1: TADOQuery;
begin
fn := getCurrentDayFileName();
if not FileExists(fn) then
begin
if not CreateDB(fn) then exit;
end;
ADOQuery1 := TADOQuery.Create(nil);
try
ADOQuery1.ConnectionString := 'DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=' + fn + '; Mode=ReadWrite' + '; Persist Security Info=False';
for i := Low(Brushary) to High(Brushary) -1 do
begin
with ADOQuery1 do
begin
SQL.Clear;
SQL.Add('select * from infolog where cardno = ''' + Copy(Brushary[i], 1, 10) + ''' and opdate between' + ' #' + FormatDateTime('yyyy-mm-dd hh::mm::ss', StrToDateTime(Copy(Brushary[i], 11, 19)) - ReBrushTime/(60*24)) + '# and #' + FormatDateTime('yyyy-mm-dd hh::MM::SS', StrToDateTime(Copy(Brushary[i], 11, 19))) + '#');
Open;
if RecordCount = 0 then
begin
SQL.Clear;
SQL.Text := 'insert into infolog (cardno, readerid, opdate, activestate, recadddate) values (''' + Copy(Brushary[i], 1, 10) + ''', ''' + Copy(Brushary[i], 30, 41) + ''', ''' + Copy(Brushary[i], 11, 19) + ''', ''' + '1' + ''',''' + DateTimeToStr(Now) + '''' + ')';
ExecSQL;
end;
end;
end;
finally
FreeAndNil(ADOQuery1);
end;
end;
procedure TGatherRecThread.CardRec(readid: string; BOOL: boolean);
var
Cardno,BrushTime,StrTime,newdevid,BrushStr,BrushSrc: string;
st,devid,j,aryTestCount: integer;
aryTest : array of String;
begin
j := 1;
aryTestCount:=0;
if not Unit4.ISOK then exit;
devid := strtoint(RightStr(readid, 5));
if Moudel then SetLength(aryTest, 1);
if DevidHand(devid, BOOL) then //握手成功
try
begin
st := Get_Curr_Record(ComInt,@devid,Record_Data);
if st = 0 then
begin
BrushSrc := getalgorism(Record_Data);
Cardno := Copy(BrushSrc, 1, 10);
BrushTime := Copy(BrushSrc, 11, 19);
StrTime := Rightstr(BrushSrc, 14);
if CheckBrushTime(StrTime) then
begin
newdevid := getnewreadid(readid, StrToDateTime(BrushTime));
if BoolAdd then
begin
if Form4.Visible then
begin
showstr :='取下一条记录成功:cardno:' + Cardno + ' datetime:' + BrushTime + ' devid:' + newdevid;
synchronize(show);
end;
if Moudel then
begin
BrushStr := Cardno + BrushTime +newdevid;
aryTest[(High(aryTest))] := BrushStr;
Inc(j);
SetLength(aryTest, j);
end else
AddRec(Cardno,BrushTime,newdevid);
end;
end;
st := Get_Next_Record(ComInt,@devid,Record_Data); //读取下一条
if (st < 0) and (st <> -9) then
begin
st := Get_Curr_Record(ComInt,@devid,Record_Data); //读当前记录
end;
end;
while st<> -9 do
begin
if not Unit4.ISOK then exit;
if st = 0 then
begin
BrushSrc := getalgorism(Record_Data);
Cardno := Copy(BrushSrc, 1, 10);
BrushTime := Copy(BrushSrc, 11, 19);
StrTime := Rightstr(BrushSrc, 14);
if CheckBrushTime(StrTime) then
begin
newdevid := getnewreadid(readid, StrToDateTime(BrushTime));
if BoolAdd then
begin
if Form4.Visible then
begin
showstr :='取下一条记录成功:cardno:' + Cardno + ' datetime:' + BrushTime + ' devid:' + newdevid;
synchronize(show);
end;
if Moudel then
begin
BrushStr := Cardno + BrushTime +newdevid;
aryTest[(High(aryTest))] := BrushStr;
Inc(j);
SetLength(aryTest, j);
if High(aryTest) = RecCount then
begin
AddRecAry(aryTest);
SetLength(aryTest, 1);
j := 1;
end;
end else
AddRec(Cardno,BrushTime,newdevid);
end;
end else
break;
st := Get_Next_Record(ComInt,@devid,Record_Data); //读取下一条数据
end else begin
st := Get_Curr_Record(ComInt,@devid,Record_Data); //返回不等于0,再读一次当前记录
if st = -5 then break;
end;
if SleepTime > 0 then Sleep(SleepTime);
end;
aryTestCount := High(aryTest);
if Moudel and (aryTestCount > 0) then AddRecAry(aryTest);
end;
finally
if Moudel then SetLength(aryTest, 0);
end;
end;
destructor TGatherRecThread.Destroy;
begin
CoUnInitialize;
inherited Destroy;
end;
function TGatherRecThread.DevidHand(devid: integer; Bool: boolean): boolean;
var
Datetime: string;
Node: integer;
begin
Node := 0;
Datetime := FormatDateTime('YYYYMMDD',now) + FormatDateTime('HHMMSS',now);
if Hand_Shake(ComInt, devid, PChar('19550930')) = 0 then
begin
if Bool then
begin
if (Set_NodeTime(ComInt, PChar(Datetime)) = 0) then
begin
if (Hand_Shake(ComInt, devid, PChar('19550930')) = 0) then
begin
showstr := '设备号:' + inttostr(devid) + '握手成功!' + '设置当前系统时间为: ' + Datetime;
synchronize(show);
result := true;
end else begin
showstr :='设备号:' + inttostr(devid) + '再次握手失败!';
synchronize(show);
result := false;
end;
end else
begin
showstr :='设备号:' + inttostr(devid) + '握手成功!' + '设置时间失败!';
synchronize(show);
result := false;
end;
end else
result := true;
end else begin
while True do
begin
Inc(Node);
if Hand_Shake(ComInt, devid, PChar('19550930')) = 0 then
begin
if Bool then
begin
showstr :='设备号:' + inttostr(devid) + '握手成功!' + '设置当前系统时间为: ' + Datetime;
synchronize(show);
result := true;
exit;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -