unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    btnConnect: TButton;
    btnWriteData: TButton;
    btnVerifyCard: TButton;
    btnReadData: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    edtCardNo: TEdit;
    edtBuilding: TEdit;
    edtRoom: TEdit;
    edtArrival: TEdit;
    edtDeparture: TEdit;
    edtCommdoors: TEdit;
    Label7: TLabel;
    Button1: TButton;
    Label8: TLabel;
    edtnew: TEdit;
    Label9: TLabel;
    Edtsuspendnum: TEdit;
    Label11: TLabel;
    Label1: TLabel;
    edtCardID: TEdit;
    Label10: TLabel;
    edtData: TEdit;
    procedure btnConnectClick(Sender: TObject);
    procedure btnVerifyCardClick(Sender: TObject);
    procedure btnReadDataClick(Sender: TObject);
    procedure btnWriteDataClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  dev;

{$R *.dfm}

procedure TForm1.btnConnectClick(Sender: TObject);
var
  vResult: smallint;
begin
  if btnConnect.Caption = 'Connect' then
  begin
    vResult := dv_connect(1);
    if vResult = 0 then
    begin
      Application.MessageBox(pchar('Connect ok!'), 'hint');
      btnConnect.Caption := 'Disconnect';
    end
    else
    begin
      Application.MessageBox(pchar('fail'+vResult.ToString), 'hint');
    end;
  end
  else if btnConnect.Caption = 'Disconnect' then
  begin
    dv_disconnect;
    btnConnect.Caption := 'Connect';
  end;
end;


procedure TForm1.btnReadDataClick(Sender: TObject);
var
  cardno: array[0..5] of AnsiChar;
  building: array[0..2] of AnsiChar;
  room: array[0..4] of AnsiChar;
  commdoors: array[0..1] of AnsiChar;
  arrival: array[0..19] of AnsiChar;
  departure: array[0..19] of AnsiChar;
  cardID: array[0..7] of AnsiChar;
  data11: array[0..32] of AnsiChar;
  vResult: smallint;
begin
  vResult := dv_read_card(cardno, building, room, commdoors, arrival, departure,cardID,data11);
  if vResult = 0 then
  begin
    edtCardno.Text := cardno;
    edtBuilding.Text := building;
    edtRoom.Text := room;
    edtCommdoors.Text := commdoors;
    edtArrival.Text := arrival;
    edtDeparture.Text := departure;
    edtCardID.Text := cardID;
    edtData.Text := data11;
  end
  else
  begin
    ShowMessage('Fail'+vResult.ToString);
  end;
end;

procedure TForm1.btnVerifyCardClick(Sender: TObject);
var
  cardtype: smallint;
  vResult: smallint;
begin
  vResult := dv_verify_card(cardtype);
  if vResult = 0 then
  begin
    Application.MessageBox(pchar('Verify card ok! Card type: ' + inttostr(cardtype)), 'hint');
  end
  else
  begin
    Application.MessageBox(pchar('Verify card failed'), 'hint');
  end;
end;

procedure TForm1.btnWriteDataClick(Sender: TObject);
var
  building: pAnsiChar;
  room: pAnsiChar;
  commdoors: pAnsiChar;
  arrival: pAnsiChar;
  departure: pAnsiChar;
  vResult: smallint;
  suspendnum: pAnsiChar;
  intnew: Integer;
  cardID: pAnsiChar;
  data11: pAnsiChar;
begin
  building := pAnsiChar(AnsiString(edtBuilding.Text));
  room := pAnsiChar(AnsiString(edtRoom.Text));
  commdoors := pAnsiChar(AnsiString(edtCommdoors.Text));
  arrival := pAnsiChar(AnsiString(edtArrival.Text));
  departure := pAnsiChar(AnsiString(edtDeparture.Text));
  intnew := StrToInt(edtnew.Text);       //新卡1可清除以前的房卡,0复制卡
  suspendnum := pAnsiChar(AnsiString(edtsuspendnum.Text));
  cardID := PAnsiChar(AnsiString(edtCardID.Text));
  data11 := PAnsiChar(AnsiString(edtData.Text));
  vResult := dv_write_card(building, room, commdoors, arrival, departure, suspendnum, intnew,data11,cardID);
  if vResult = 0 then
  begin
    ShowMessage('Write data ok!')
  end
  else
  begin
    ShowMessage('fail'+vResult.ToString);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  room: array[0..4] of AnsiChar;
  vResult: smallint;
begin
  vResult := dv_delete_card(room);
  if vResult = 0 then
  begin
    edtRoom.Text := room;
  end;

end;

end.

