⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitmain.pas

📁 Inside Demo RFID IC used
💻 PAS
字号:
unit UnitMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, {PanelXMXControl_TLB,} StdCtrls, StrUtils,
  PanelXMX3GControl_TLB, PanelXMXControl_TLB, ExtCtrls;

type
  TFrmMain = class(TForm)
    GpCommands: TGroupBox;
    GpParameters: TGroupBox;
    GpResponse: TGroupBox;
    BtConnect: TButton;
    BtDisconnect: TButton;
    BtSelectChip: TButton;
    BtSelectPage: TButton;
    BtRead: TButton;
    BtWrite: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    CmbKey: TComboBox;
    CmbPage: TComboBox;
    CmbBlock: TComboBox;
    LblConnectionState: TLabel;
    LblSN: TLabel;
    LblConfBlock: TLabel;
    LblDataRead: TLabel;
    btLoadKeys: TButton;
    Edit1: TEdit;
    Timer1: TTimer;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    mxPanel: TPanelXMX;
    procedure FormCreate(Sender: TObject);
    procedure BtConnectClick(Sender: TObject);
    procedure BtDisconnectClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure BtSelectChipClick(Sender: TObject);
    procedure BtSelectPageClick(Sender: TObject);
    procedure BtReadClick(Sender: TObject);
    procedure BtWriteClick(Sender: TObject);
    procedure btLoadKeysClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private    { D閏larations priv閑s }
    bOldCoupler: Boolean;
    FWriteit : boolean; 
  public
    { D閏larations publiques }
  end;

var
  FrmMain: TFrmMain;

implementation

{$R *.dfm}




procedure TFrmMain.FormCreate(Sender: TObject);
var
  Index: Integer;
begin
  // Combo boxes initialization
  CmbKey.AddItem('No Auth', TObject.Create);
  CmbKey.AddItem('Kd', TObject.Create);
  CmbKey.AddItem('Kc', TObject.Create);
  CmbKey.ItemIndex := 0;

  for Index := 0 to 7 do CmbPage.AddItem(IntToStr(Index), TObject.Create);
  CmbPage.ItemIndex := 0;

  for Index := 0 to 31 do CmbBlock.AddItem(IntToStr(Index), TObject.Create);
  CmbBlock.ItemIndex := 6;
  fwriteit := true;
end;

procedure TFrmMain.Timer1Timer(Sender: TObject);
var
  vBlock: OleVariant;
  sData: String;
  ss : string; 
begin
  timer1.Enabled := false;
  if MxPanel.ReadBlock(CmbBlock.ItemIndex, 1, vBlock) then
  begin
    // Update result display
    sData := vBlock;
    LblDataRead.Caption := 'Data : ' + sData;
    edit2.text :=  mxpanel.ConvertHexaToAscii(sdata);


    if FWriteIt  then
    begin
      ss := formatdatetime('nn:ss:zzz',now);

      edit1.text :=   mxpanel.StringConvertAsciiToHexa(ss);

      if MxPanel.WriteBlock(CmbBlock.ItemIndex, 1, edit1.text{'$01$02$03$04$05$06$07$08'}) then
      begin
        LblDataRead.Caption := 'Data : Write complete -> +edit1.text';
        if (cmbblock.ItemIndex) < (cmbblock.Items.Count -1) then
          cmbblock.itemindex := cmbblock.itemindex+1
        else
        begin
          cmbblock.itemindex := 6;
          FwriteIt := false;
        end;

      end
      else
      begin
        ShowMessage('Write error');
        timer1.enabled := false;
        exit;
      end;
    end;

  end;

  timer1.enabled := true;
end;

procedure TFrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  // Disconnect coupler
  MxPanel.Disconnect;
end;

procedure TFrmMain.BtConnectClick(Sender: TObject);
var
  iSwitchValue: Integer;
  vCommandReturn: OleVariant;
  sCommandReturn: String;
begin
  // Connect coupler and test result
  if not MxPanel.Connect then
    ShowMessage('Connection failed')
  else
  begin
    // Test coupler version
    iSwitchValue := MxPanel.ISO7816T0SendCmd($80, $F2, $00, $00, $01, mt0dOut, '', 1, vCommandReturn);
    sCommandReturn := vCommandReturn;
    if (StrToInt(Midstr(sCommandReturn, 1, 3)) and 48) = 0 then
      bOldCoupler := False
    else
      bOldCoupler := True;

    // Update command access and display result
    BtConnect.Enabled := False;
    BtDisconnect.Enabled := True;
    if bOldCoupler then
      LblConnectionState.Caption := 'Coupler state : Connected (Old coupler)'
    else
      LblConnectionState.Caption := 'Coupler state : Connected (New coupler)';
  end;
end;

procedure TFrmMain.BtDisconnectClick(Sender: TObject);
begin
  // Disconnect coupler
  MxPanel.Disconnect;

  // Update command access and display defaults
  BtConnect.Enabled := True;
  BtDisconnect.Enabled := False;
  LblConnectionState.Caption := 'Coupler state : Disconnected';
  LblConfBlock.Caption := 'Page conf block : ';
  LblSN.Caption := 'Serial Number : ';
  LblDataRead.Caption := 'Data : ';
end;

procedure TFrmMain.BtSelectChipClick(Sender: TObject);
var
  vBlock: OleVariant;
  sSN: String;
begin
(*  mxpanel.MxUserProtocol := mupISO_14443A_3;
  mxpanel.SetStatus($03,$6B,$7B);

  if not MxPanel.SelectCard($00, $08, vBlock) then
*)
if not MxPanel.SelectCard($00, $07, vBlock) then
    ShowMessage('Selection error')
  else
  begin
    sSN := vBlock;
    sSN := MidStr(sSN, 4, 26);
    LblSN.Caption := 'Serial number : ' + sSN;
    mxpanel.ReadRecord(0,4,vblock);
    edit2.text := vblock;
  end;
end;

procedure TFrmMain.BtSelectPageClick(Sender: TObject);
var
  vBlock: OleVariant;
  sConfBlock: String;
  Key: TxMxKey;
begin
  // Retreive authentification to use
  case CmbKey.ItemIndex of
    0: Key := mpkNoAuth;
    1: Key := mpkP0Kd;
    2: Key := mpkP0Kc;
  end;

  // Retreive coupler generation
  if bOldCoupler then
  begin
    // Send PAGESEL command to chip
    if MxPanel.SelectAuthPage(mkNoAuth, CmbPage.ItemIndex, vBlock) then
    begin
      // Manual authentification
      if MxPanel.Authentify(Key, vBlock) then
      begin
        // Update result display
        sConfBlock := vBlock;
        LblConfBlock.Caption := 'Page SN : ' + sConfBlock;
      end
      else
        ShowMessage('Page authentification failed');
    end
    else
      ShowMessage('Page selection failed');
  end
  else
  begin
    // Use PAGESEL coupler command (with authentification)
    if MxPanel.SelectPage(Key, CmbPage.ItemIndex, vBlock) then
    begin
      // Update result display
      sConfBlock := vBlock;
      LblConfBlock.Caption := 'Page conf block : ' + sConfBlock;
    end
    else
      ShowMessage('Page selection failed');
  end;
end;



procedure TFrmMain.BtReadClick(Sender: TObject);
var
  vBlock: OleVariant;
  sData: String;
begin
  // READ content of selected block

  if MxPanel.ReadBlock(CmbBlock.ItemIndex, 1, vBlock) then
  begin
    // Update result display
    sData := vBlock;
    LblDataRead.Caption := 'Data : ' + mxpanel.ConvertHexaToAscii(sdata);
  end
  else
    ShowMessage('Read Error');
end;

procedure TFrmMain.BtWriteClick(Sender: TObject);
begin
  // WRITE data in block selected
  edit1.text :=   mxpanel.StringConvertAsciiToHexa(edit1.text);
  
  if MxPanel.WriteBlock(CmbBlock.ItemIndex, 1, edit1.text{'$01$02$03$04$05$06$07$08'}) then
  begin
    // Update result display
    LblDataRead.Caption := 'Data : Write complete -> +edit1.text';
  end
  else
    ShowMessage('Write error');
end;

procedure TFrmMain.Button1Click(Sender: TObject);
begin
  self.CmbBlock.ItemIndex := 6;
  self.FWriteit := true;
  timer1.Enabled := true;
end;

procedure TFrmMain.Button2Click(Sender: TObject);
var
  iSwitchValue: Integer;
  vCommandReturn: OleVariant;
  sCommandReturn: String;
begin
  mxpanel.ISO7816T0SendCmd($80,$A4,$00,$08,$10, mt0dOut, '', 1, vCommandReturn);
  edit2.text := vCommandReturn ;
end;

procedure TFrmMain.Button3Click(Sender: TObject);
var
  vBlock: OleVariant;
  sSN: String;
begin
  mxpanel.MxUserProtocol := mupISO_14443A_3;
  mxpanel.SetStatus($03,$6B,$7B);
//  mxpanel.SelectPage(,5,vblock);

  edit2.text := vblock;


  if not mxpanel.WriteRecord(200,4,mxpanel.ConvertAsciiToHexa(edit1.text)) then
  begin
    showmessage('写.......不进去');
  end;
  
end;

procedure TFrmMain.Button4Click(Sender: TObject);
var
  vBlock: OleVariant;
  sSN: String;
begin
(*
  mxpanel.MxUserProtocol := mupISO_14443A_3;
  mxpanel.SetStatus($03,$6B,$7B);

*)
  edit2.text :='';
  if mxpanel.ReadBlock(self.CmbBlock.ItemIndex,1,vblock) then
    edit2.text := vblock
  else
    showmessage('读不出来....'); 
end;

procedure TFrmMain.btLoadKeysClick(Sender: TObject);
begin
    // Load exchange key
    if not MxPanel.KeyLoading(mpkExchange, mklmXorKo, '$5C$BC$F1$DA$45$D5$FB$5F', '$5C$BC$F1$DA$45$D5$FB$5F') then
    begin
        ShowMessage('Exchange key loading error');
        Exit;
    end;

    // Load Kc and Kd
    if not MxPanel.KeyLoading(mpkP0Kd, mklmXorKe, '$5C$BC$F1$DA$45$D5$FB$5F', '$F0$E1$D2$C3$B4$A5$96$87') then
    begin
        ShowMessage('Debit key loading error');
        Exit;
    end;
    if not MxPanel.KeyLoading(mpkP0Kc, mklmXorKe, '$5C$BC$F1$DA$45$D5$FB$5F', '$76$65$54$43$32$21$10$00') then
    begin
        ShowMessage('Credit key loading error');
        Exit;
    end;

    ShowMessage('Keys loaded successfully');
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -