unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,declaredll,strutils;

type
  TForm1 = class(TForm)
    le_xlh: TLabeledEdit;
    le_akey: TLabeledEdit;
    cb_kh: TComboBox;
    Label2: TLabel;
    le_sj: TLabeledEdit;
    BtnSearchCard: TButton;
    BtnReadCard: TButton;
    BtnWriteCard: TButton;
    BtnHaltCard: TButton;
    lb_info: TLabel;
    Label3: TLabel;
    cb_port: TComboBox;
    Label4: TLabel;
    cb_baud: TComboBox;
    rg_key: TRadioGroup;
    BtnConnect: TButton;
    procedure BtnSearchCardClick(Sender: TObject);
    procedure BtnHaltCardClick(Sender: TObject);
    procedure BtnReadCardClick(Sender: TObject);
    procedure BtnWriteCardClick(Sender: TObject);
    procedure BtnConnectClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  port,baud:integer;
  bConnect:integer;
  icdev:word;
implementation

{$R *.dfm}

//寻卡
procedure TForm1.BtnSearchCardClick(Sender: TObject);
var
   i:integer;w1:word; buf1:array[0..200] of byte;s1:string;
begin
   //寻卡
   i:=rf_request(icdev,$52,w1);
   if(i<>0)then
   begin
       lb_info.Caption:=' Search Card Failed!,Err Code：'+inttostr(i);
       exit;
   end;
   //防冲撞，buf1 保存卡片序号
   i:=rf_anticollCLn(icdev, 1, buf1[0]);
   if(i<>0)then
   begin
       lb_info.Caption:='Atticollision failed,Err Code：'+inttostr(i);
       exit;
   end;
   s1:='';
   for i:=0 to 3 do begin
       s1:=s1+inttohex(buf1[i],2);
   end;

   le_xlh.Text:=s1;
   lb_info.Caption:='Anticollision success';
   i:=rf_selectCLn(icdev, 1, buf1[0], buf1[10]);
   if(i<>0)then
   begin
       lb_info.Caption:='Select Failed!,Err code:'+inttostr(i);
       exit;
   end;
   lb_info.Caption:='Seach card success';
end;

//卡片休眠
procedure TForm1.BtnHaltCardClick(Sender: TObject);
var
    i:integer;
begin
   i:=rf_halt(icdev);
   if(i<>0)then
   begin
       lb_info.Caption:=' Halt failed,Err Code：'+inttostr(i);
       exit;
   end;
   lb_info.Caption:='Halt success';
end;

//读卡
procedure TForm1.BtnReadCardClick(Sender: TObject);
var
   i,j:integer;b1,b_kh:byte; buf1,buf2:array[0..200] of byte;s1:string;
begin
   b_kh:=cb_kh.ItemIndex;
   s1:=trim(le_akey.Text );
   if(length(s1)<>12)then
   begin
       lb_info.Caption:='Key length Err!';
       le_akey.SetFocus;
       exit;
   end;
   for i:=0 to 5 do begin
       val('$'+midstr(s1,i*2+1,2),buf2[i],j);
   end;
   //判断密钥类型
   if(rg_key.ItemIndex =0)  then
   begin
      b1:=$60;
   end
   else
   begin
      b1:=$61;
   end;
   //密钥认证
   i:=rf_M1_authentication2(icdev,b1,b_kh,buf2[0]);
   if(i<>0)then
   begin
       lb_info.Caption:=' Authentication Failed, Err Code:'+inttostr(i);
       exit;
   end;
   //读卡失败
   i:=rf_M1_read(icdev,b_kh,buf1[0],b1);
   if(i<>0)then
   begin
       lb_info.Caption:='Read Failed, Err Code:'+inttostr(i);
       exit;
   end;
   s1:='';
   for i:=0 to b1-1 do begin
       s1:=s1+inttohex(buf1[i],2);
   end;

   le_sj.Text:=s1;
   lb_info.Caption:='Read Success';
end;

//写卡
procedure TForm1.BtnWriteCardClick(Sender: TObject);
var
   i,j:integer;b1,b_kh:byte; buf1,buf2:array[0..200] of byte;s1:string;
begin
   b_kh:=cb_kh.ItemIndex;
   //判断密钥类型
   s1:=trim(le_akey.Text );
   if(length(s1)<>12)then
   begin
       lb_info.Caption:=' Key Length Err! ';
       le_akey.SetFocus;
       exit;
   end;
   for i:=0 to 5 do begin
       val('$'+midstr(s1,i*2+1,2),buf2[i],j);
   end;

   s1:=trim(le_sj.Text );
   if(length(s1)<>32)then
   begin
       lb_info.Caption:=' Data length err! ';
       le_sj.SetFocus;
       exit;
   end;
   for i:=0 to 15 do begin
       val('$'+midstr(s1,i*2+1,2),buf1[i],j);
   end;


   if(rg_key.ItemIndex =0)  then
   begin
      b1:=$60;
   end
   else
   begin
      b1:=$61;
   end;
   //密钥认证
   i:=rf_M1_authentication2(icdev,b1,b_kh,buf2[0]);
   if(i<>0)then
   begin
       lb_info.Caption:=' Authentication Failed, Err Code:'+inttostr(i);
       exit;
   end;
   //写卡失败
   i:=rf_M1_write(icdev,b_kh,buf1[0]);
   if(i<>0)then
   begin
       lb_info.Caption:=' Write Failed, Err Code:'+inttostr(i);
       exit;
   end;
   
   lb_info.Caption:='Write Success!';

end;

//连接读卡器
procedure TForm1.BtnConnectClick(Sender: TObject);
begin
  if(bConnect = 0)then
  begin
    port:=strtoint(trim(cb_port.Text ));
    baud:=strtoint(trim(cb_baud.Text ));

    icdev:= rf_init_com(port,baud);
    if(icdev<>0)then
    begin
      lb_info.Caption:=' Serial port Initialization Failed, Err Code: '+inttostr(icdev);
    end
    else
    begin
      BtnConnect.Caption := 'Disconnect';
      bConnect := 1;
      rf_init_type(0,$41);
      lb_info.Caption:=' Serial port Initialization success. ';
    end;
  end
  else
    begin
      BtnConnect.Caption := 'Connect';
      bConnect := 0;
      rf_ClosePort(icdev);
      lb_info.Caption:='  ';
    end;
end;

end.
