ID卡读取方法(用于区分ID卡读取出来的数据和一般人手录入的数据)
ID卡的读取方式其实是模拟一个键盘的录入而已,很简单,现在这个ID卡读取方法是为了在任何情况下把它正确读取出来的方法,例如你在tedit控件里面,当读取了ID卡时,会和其它数据混在一起,而我这种方法,能正确地读取出ID卡的正确数据,如果你同时快速按键盘又同时读取ID卡时,它是不会读出数据的,这样是为了防止读出来的数据不正确。不多说,下面是代码,还有测试用的代码一起的,我就不改了,大家可参考一下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TIDCheck=class( TObject)
protected
m_INTCheck:array of integer;//结果的检测用
m_TimeCheck:array of integer;//时间的检测用
m_INT:array of integer; //真实保存的结果(没用)
m_Time:array of integer; //真实保存的时间间隔(没用)
m_Begin:integer;//检测录入的字符第一个开始时间。
m_MaxTime:Smallint;//超出该时间证明不是ID的录入时间范围
m_Length:integer;//读取长度
m_LastResult:string;//上一次读取的结果
procedure DoClear;//当检测到符合清空条件的,则还原所有变量
function CheckID:Boolean;
function GetIDResult:string;
procedure DoAddKey(var key:word);//符合条件的进行处理。
public
aForm:TForm;
function KeyDownCheck(var key:word):string;
property IDResult:string read GetIDResult;
property LastResult:string read m_LastResult;
constructor Create(MaxTime:integer=30;Length:integer=10);
destructor Destroy; override;
end;
TForm1 = class(TForm)
Timer1: TTimer;
Memo1: TMemo;
Edit1: TEdit;
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
m_End,m_first:integer;
IDTest:TIDCheck;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TIDCheck.CheckID: Boolean;
begin
if m_INTCheck[m_Length-1]<0 then
result:=false
else
result:=true;
end;
constructor TIDCheck.Create(MaxTime: integer;Length:integer);
begin
m_MaxTime:=MaxTime;
m_Length:=Length;
SetLength(m_INTCheck,m_Length);
SetLength(m_TimeCheck,m_Length);
SetLength(m_INT,m_Length);
SetLength(m_Time,m_Length);
DoClear;
end;
destructor TIDCheck.Destroy;
begin
Finalize(m_INTCheck);
Finalize(m_TimeCheck);
Finalize(m_INT);
Finalize(m_Time);
inherited;
end;
procedure TIDCheck.DoAddKey(var key: word);
var
i:Smallint;
begin
for i:=0 to m_Length-1 do
if m_INTCheck[i]<0 then
begin
m_INTCheck[i]:=key;
m_TimeCheck[i]:=GetTickCount-m_Begin;
m_Begin:=GetTickCount;
break;
end;
if i=m_Length then//没有空间记录,把所有数据前移并把新的放在最后一位。
begin
for i:=1 to m_Length-1 do
begin
m_INTCheck[i-1]:=m_INTCheck[i];
m_TimeCheck[i-1]:=m_TimeCheck[i];
end;
m_INTCheck[m_Length]:=key;
m_TimeCheck[m_Length]:=GetTickCount-m_Begin;
m_Begin:=GetTickCount;
end;
if (m_TimeCheck[i]>=m_MaxTime) and (i>0) then //超出ID卡读取范围则清空
DoClear;
end;
procedure TIDCheck.DoClear;
var
i:integer;
begin
for i:=0 to m_Length-1 do
begin
m_INTCheck[i]:=-1;
m_INT[i]:=-1;
m_TimeCheck[i]:=-1;
m_Time[i]:=-1;
end;
m_Begin:=0;
aForm.Caption:=aForm.Caption+'clear';
end;
function TIDCheck.GetIDResult: string;
var
i:Shortint;
begin
if CheckID then
begin
for i:=0 to m_Length-1 do
result:=result+char(m_INTCheck[i]);
end;
end;
function TIDCheck.KeyDownCheck(var key:word):string;
begin
result:='';
case key of
48..57:DoAddKey(key);
13:begin
if checkid then //检测到结果则把结果返回
begin
result:=GetIDResult;
m_LastResult:=result;
DoClear;//清空重新处理。
end
else
DoClear;//清空重新处理。
end;
else//非数字的要清空并重新处理
DoClear;
end;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
aStr:string;
begin
{
if key=13 then
begin
m_end:=GetTickCount;
memo1.Lines.Add('结束:'+inttostr(m_end-m_first));
m_first:=0;
end
else
begin
if m_first=0 then
begin
m_first:=GetTickCount;
memo1.Lines.Add('开始');
end;
end;
if key=13 then
begin
m_first:=0;
exit;
end;
if m_first=0 then
begin
m_first:=GetTickCount;
end
else
begin
memo1.Lines.Add('Key:'+char(key)+'keyCode'+inttostr(key)+'时间:'+inttostr(GetTickCount-m_first));
m_first:=GetTickCount;
end;
}
aStr:=IDTest.KeyDownCheck(key);
if length(aStr)>0 then
begin
// memo1.Lines.Add(aStr);
key:=0;
end;
{
if self.ActiveControl is TEdit then
TEdit(self.ActiveControl).Text:=;
}
end;
procedure TForm1.FormShow(Sender: TObject);
begin
memo1.Clear;
m_End:=0;
m_first:=0;
IDTest:=TIDCheck.Create;
IDTest.aForm:=self;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TIDCheck=class( TObject)
protected
m_INTCheck:array of integer;//结果的检测用
m_TimeCheck:array of integer;//时间的检测用
m_INT:array of integer; //真实保存的结果(没用)
m_Time:array of integer; //真实保存的时间间隔(没用)
m_Begin:integer;//检测录入的字符第一个开始时间。
m_MaxTime:Smallint;//超出该时间证明不是ID的录入时间范围
m_Length:integer;//读取长度
m_LastResult:string;//上一次读取的结果
procedure DoClear;//当检测到符合清空条件的,则还原所有变量
function CheckID:Boolean;
function GetIDResult:string;
procedure DoAddKey(var key:word);//符合条件的进行处理。
public
aForm:TForm;
function KeyDownCheck(var key:word):string;
property IDResult:string read GetIDResult;
property LastResult:string read m_LastResult;
constructor Create(MaxTime:integer=30;Length:integer=10);
destructor Destroy; override;
end;
TForm1 = class(TForm)
Timer1: TTimer;
Memo1: TMemo;
Edit1: TEdit;
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
m_End,m_first:integer;
IDTest:TIDCheck;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TIDCheck.CheckID: Boolean;
begin
if m_INTCheck[m_Length-1]<0 then
result:=false
else
result:=true;
end;
constructor TIDCheck.Create(MaxTime: integer;Length:integer);
begin
m_MaxTime:=MaxTime;
m_Length:=Length;
SetLength(m_INTCheck,m_Length);
SetLength(m_TimeCheck,m_Length);
SetLength(m_INT,m_Length);
SetLength(m_Time,m_Length);
DoClear;
end;
destructor TIDCheck.Destroy;
begin
Finalize(m_INTCheck);
Finalize(m_TimeCheck);
Finalize(m_INT);
Finalize(m_Time);
inherited;
end;
procedure TIDCheck.DoAddKey(var key: word);
var
i:Smallint;
begin
for i:=0 to m_Length-1 do
if m_INTCheck[i]<0 then
begin
m_INTCheck[i]:=key;
m_TimeCheck[i]:=GetTickCount-m_Begin;
m_Begin:=GetTickCount;
break;
end;
if i=m_Length then//没有空间记录,把所有数据前移并把新的放在最后一位。
begin
for i:=1 to m_Length-1 do
begin
m_INTCheck[i-1]:=m_INTCheck[i];
m_TimeCheck[i-1]:=m_TimeCheck[i];
end;
m_INTCheck[m_Length]:=key;
m_TimeCheck[m_Length]:=GetTickCount-m_Begin;
m_Begin:=GetTickCount;
end;
if (m_TimeCheck[i]>=m_MaxTime) and (i>0) then //超出ID卡读取范围则清空
DoClear;
end;
procedure TIDCheck.DoClear;
var
i:integer;
begin
for i:=0 to m_Length-1 do
begin
m_INTCheck[i]:=-1;
m_INT[i]:=-1;
m_TimeCheck[i]:=-1;
m_Time[i]:=-1;
end;
m_Begin:=0;
aForm.Caption:=aForm.Caption+'clear';
end;
function TIDCheck.GetIDResult: string;
var
i:Shortint;
begin
if CheckID then
begin
for i:=0 to m_Length-1 do
result:=result+char(m_INTCheck[i]);
end;
end;
function TIDCheck.KeyDownCheck(var key:word):string;
begin
result:='';
case key of
48..57:DoAddKey(key);
13:begin
if checkid then //检测到结果则把结果返回
begin
result:=GetIDResult;
m_LastResult:=result;
DoClear;//清空重新处理。
end
else
DoClear;//清空重新处理。
end;
else//非数字的要清空并重新处理
DoClear;
end;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
aStr:string;
begin
{
if key=13 then
begin
m_end:=GetTickCount;
memo1.Lines.Add('结束:'+inttostr(m_end-m_first));
m_first:=0;
end
else
begin
if m_first=0 then
begin
m_first:=GetTickCount;
memo1.Lines.Add('开始');
end;
end;
if key=13 then
begin
m_first:=0;
exit;
end;
if m_first=0 then
begin
m_first:=GetTickCount;
end
else
begin
memo1.Lines.Add('Key:'+char(key)+'keyCode'+inttostr(key)+'时间:'+inttostr(GetTickCount-m_first));
m_first:=GetTickCount;
end;
}
aStr:=IDTest.KeyDownCheck(key);
if length(aStr)>0 then
begin
// memo1.Lines.Add(aStr);
key:=0;
end;
{
if self.ActiveControl is TEdit then
TEdit(self.ActiveControl).Text:=;
}
end;
procedure TForm1.FormShow(Sender: TObject);
begin
memo1.Clear;
m_End:=0;
m_first:=0;
IDTest:=TIDCheck.Create;
IDTest.aForm:=self;
end;
end.
原创作品出自努力偷懒,转载请说明文章出处:http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/