unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,FileCtrl;
type
  Pint=^integer; //定义指针类型
  pbyte=^byte;
  Puint=^DWORD;
  TIDCardData=record
    name:array[0..31] of char;
    sex:array[0..5] of char;
    nation:array[0..19] of char;
    born:array[0..17] of char;
    address:array[0..71] of char;
    IDcardno:array[0..37] of char;
    grantdept:array[0..31] of char;
    UserLifeBegin:array[0..17] of char;
    UserLifeEnd:array[0..17] of char;
    reserved:array[0..37] of char;
    PhotoFileName:array[0..254] of char;
  end;


type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    GroupBox2: TGroupBox;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    GroupBox3: TGroupBox;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    GroupBox4: TGroupBox;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Button13: TButton;
    Button14: TButton;
    Button15: TButton;
    Button16: TButton;
    Button17: TButton;
    Button18: TButton;
    Button19: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button19Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure Button14Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure Button16Click(Sender: TObject);
    procedure Button17Click(Sender: TObject);
  private
    { Private declarations }
    PROCEDURE LOADDLL();
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
VAR
  Moudlegm:longint;
Syn_SetMaxRFByte:function (iPort:integer;ucByte:Byte;bIfOpen:integer):integer;stdcall;
Syn_GetCOMBaud:function (iPort:integer;puiBaudRate:Puint):integer;stdcall;
Syn_SetCOMBaud:function (iPort:integer;uiCurrBaud:DWORD;uiSetBaud:DWORD):integer;stdcall;
Syn_OpenPort: function (iPort:integer):integer;stdcall;
Syn_ClosePort: function (iPort:integer):integer;stdcall;

Syn_ResetSAM:function (iPort:integer;iIfOpen:integer):integer;stdcall;
Syn_GetSAMStatus:function (iPort:integer;iIfOpen:integer):integer;stdcall;
Syn_GetSAMID:function (iPort:integer;pucSAMID:pbyte;iIfOpen:integer):integer;stdcall;
Syn_GetSAMIDToStr:function (iPort:integer;pcSAMID:PCHAR;iIfOpen:integer):integer;stdcall;

Syn_StartFindIDCard: function (iPort:integer;pucManaInfo:PCHAR;iIfOpen:integer):integer;stdcall;
Syn_SelectIDCard: function (iPort:integer;pucManaMsg:pbyte;iIfOpen:integer):integer;stdcall;
Syn_ReadBaseMsg: function (iPort:integer;pucCHMsg:pbyte;puiCHMsgLen:Puint;pucPHMsg:pbyte;puiPHMsgLen:Puint;iIfOpen:integer):integer;stdcall;
Syn_ReadIINSNDN: function (iPort:integer;pucIINSNDN:pbyte;iIfOpen:integer):integer;stdcall;
Syn_ReadBaseMsgToFile: function (iPort:integer;pcCHMsgFileName:PCHAR;puiCHMsgFileLen:Puint;pcPHMsgFileName:PCHAR;puiPHMsgFileLen:Puint;iIfOpen:integer):integer;stdcall;
Syn_ReadIINSNDNToASCII: function (iPort:integer;pucIINSNDN:PCHAR;iIfOpen:integer):integer;stdcall;
Syn_ReadNewAppMsg: function (iPort:integer;pucAppMsg:pbyte;puiAppMsgLen:Puint;iIfOpen:integer):integer;stdcall;
Syn_GetBmp: function (iPort:integer;Wlt_File:PCHAR):integer;stdcall;
Syn_ReadMsg: function (iPort:integer;iIfOpen:integer;var pIDCardData:TIDCardData):integer;stdcall;
Syn_FindReader:function ():integer;stdcall;

Syn_SetPhotoPath: function (iOption:integer;cPhotoPath:PCHAR):integer;stdcall;
Syn_SetPhotoType: function (iType:integer):integer;stdcall;
Syn_SetPhotoName: function (iType:integer):integer;stdcall;
Syn_SetSexType: function (iType:integer):integer;stdcall;
Syn_SetNationType: function (iType:integer):integer;stdcall;
Syn_SetBornType: function (iType:integer):integer;stdcall;
Syn_SetUserLifeBType: function (iType:integer):integer;stdcall;
Syn_SetUserLifeEType: function (iType:integer;iOption:integer):integer;stdcall;
procedure TForm1.LOADDLL();
begin
    Moudlegm := Loadlibrary('SynIDCardAPI.dll');
    if (Moudlegm>32) then
      begin
        @Syn_SetMaxRFByte:=GetProcAddress(Moudlegm,'Syn_SetMaxRFByte');
        @Syn_GetCOMBaud:=GetProcAddress(Moudlegm,'Syn_GetCOMBaud');
        @Syn_SetCOMBaud:=GetProcAddress(Moudlegm,'Syn_SetCOMBaud');
        @Syn_OpenPort:=GetProcAddress(Moudlegm,'Syn_OpenPort');
        @Syn_ClosePort:=GetProcAddress(Moudlegm,'Syn_ClosePort');

        @Syn_ResetSAM:=GetProcAddress(Moudlegm,'Syn_ResetSAM');
        @Syn_GetSAMStatus:=GetProcAddress(Moudlegm,'Syn_GetSAMStatus');
        @Syn_GetSAMID:=GetProcAddress(Moudlegm,'Syn_GetSAMID');
        @Syn_GetSAMIDToStr:=GetProcAddress(Moudlegm,'Syn_GetSAMIDToStr');

        @Syn_StartFindIDCard:=GetProcAddress(Moudlegm,'Syn_StartFindIDCard');
        @Syn_SelectIDCard:=GetProcAddress(Moudlegm,'Syn_SelectIDCard');
        @Syn_ReadBaseMsg:=GetProcAddress(Moudlegm,'Syn_ReadBaseMsg');
        @Syn_ReadIINSNDN:=GetProcAddress(Moudlegm,'Syn_ReadIINSNDN');
        @Syn_ReadIINSNDNToASCII:=GetProcAddress(Moudlegm,'Syn_ReadIINSNDNToASCII');
        @Syn_ReadBaseMsgToFile:=GetProcAddress(Moudlegm,'Syn_ReadBaseMsgToFile');
        @Syn_ReadNewAppMsg:=GetProcAddress(Moudlegm,'Syn_ReadNewAppMsg');
        @Syn_GetBmp:=GetProcAddress(Moudlegm,'Syn_GetBmp');
        @Syn_ReadMsg:=GetProcAddress(Moudlegm,'Syn_ReadMsg');
        @Syn_FindReader:=GetProcAddress(Moudlegm,'Syn_FindReader');

        @Syn_SetPhotoPath:=GetProcAddress(Moudlegm,'Syn_SetPhotoPath');
        @Syn_SetPhotoType:=GetProcAddress(Moudlegm,'Syn_SetPhotoType');
        @Syn_SetPhotoName:=GetProcAddress(Moudlegm,'Syn_SetPhotoName');
        @Syn_SetSexType:=GetProcAddress(Moudlegm,'Syn_SetSexType');
        @Syn_SetNationType:=GetProcAddress(Moudlegm,'Syn_SetNationType');
        @Syn_SetBornType:=GetProcAddress(Moudlegm,'Syn_SetBornType');
        @Syn_SetUserLifeBType:=GetProcAddress(Moudlegm,'Syn_SetUserLifeBType');
        @Syn_SetUserLifeEType:=GetProcAddress(Moudlegm,'Syn_SetUserLifeEType');
      end
end;


procedure TForm1.Button1Click(Sender: TObject);
  var
  nPort:integer;
  nCurBaud:DWORD;
begin
  nPort := Syn_FindReader();
  if nPort = 0 then begin
    ListBox1.Items.Add( DateTimeToStr(now())+ '  没有找到读卡器' );
  end else begin
    Windows.Beep(4000,200);
    if nPort>1000 then begin
      ListBox1.Items.Add( DateTimeToStr(Now())+'  读卡器连接在USB ' + IntToStr(nPort) + ' 上');
      Edit1.Text := IntToStr(nPort);
      Edit3.Enabled := False;
      Button3.Enabled := False;
    end else begin
      Syn_GetCOMBaud(nPort,@nCurBaud);
      ListBox1.Items.Add( DateTimeToStr(Now())+'  读卡器连接在COM ' + IntToStr(nPort) + ' 上');
      Edit1.Text := IntToStr(nPort);
      Edit3.Enabled := True;
      Edit3.Text := IntToStr(nCurBaud);
      Button3.Enabled := True;
    end;
  end;
end;

procedure TForm1.Button19Click(Sender: TObject);
begin
  Application.Terminate();
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  LOADDLL();
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  nRet:Integer;
  nPort:Integer;
  nMaxByte:Byte; 
begin
  nPort := StrToInt(Edit1.Text);
  nMaxByte := StrToInt(Edit2.Text);
  nRet := Syn_OpenPort(nPort);
  if nRet = 0 then begin
    if Syn_GetSAMStatus(nPort,0) = 0 then begin
      if Syn_SetMaxRFByte(nPort,nMaxByte,0) = 0 then begin
        ListBox1.Items.Add(DateTimeToStr(Now()) + '  设置最大通讯字节数成功,最大通讯字节数为  ' + Edit2.Text);
      end else begin
        ListBox1.Items.Add(DateTimeToStr(Now()) + '  设置最大通讯字节数失败!');
      end;
    end else begin
      ListBox1.Items.Add(DateTimeToStr(Now())+ '  端口'+Edit1.Text+'没有连接读卡器');
    end;
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+ '  打开端口失败');
  end;
  Syn_ClosePort(nPort);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  nPort:Integer;
  nRet:Integer;
  nCurBaud:DWORD;
  nSetBaud:DWORD;
begin
  nPort := StrToInt(Edit1.Text);
  nSetBaud := StrToInt(Edit3.Text);
  nRet := Syn_GetCOMBaud(nPort,@nCurBaud);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  获得读卡器当前波特率失败');
    exit;
  end;
  Syn_ClosePort(nPort);
  if Syn_SetCOMBaud(nPort,nCurBaud,nSetBaud) = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+ '  设置读卡器波特率成功,当前波特率为' + Edit3.Text);
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+ '  设置读卡器波特率失败');
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  nRet:Integer;
  cPath:array[0..255] of char;
begin
  nRet := Syn_SetPhotoPath(0,cPath);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存路径设置为C:,nRet = '+IntToStr(nRet) );
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存路径设置失败:,nRet = '+IntToStr(nRet) );
  end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  nRet:Integer;
  cPath:array[0..255] of char;
begin
  nRet := Syn_SetPhotoPath(1,cPath);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存路径设置当前路径:,nRet = '+IntToStr(nRet) );
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存路径设置当前路径:,nRet = '+IntToStr(nRet) );
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
  sPath:string;
  nRet:Integer;
  cPath:array[0..255] of char;
begin
  //sPath:='D:\IconWorkshop';
  if SelectDirectory('选择路径：','',sPath)  then begin
    StrPCopy(cPath,sPath);
  end else begin
    StrPCopy(cPath,'C:\');
  end;
  nRet := Syn_SetPhotoPath(2,cPath);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存路径设置为:'+ cPath+' ,nRet = '+IntToStr(nRet) );
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存路径设置错误:,nRet = '+IntToStr(nRet) );
  end;
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  nRet:Integer;
begin
  nRet := Syn_SetPhotoType(0);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存格式为Bmp,nRet = ' + IntToStr(nRet));
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存格式设置失败,nRet = ' + IntToStr(nRet));
  end;
end;

procedure TForm1.Button8Click(Sender: TObject);
var
  nRet:Integer;
begin
  nRet := Syn_SetPhotoType(1);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存格式为Jpeg,nRet = ' + IntToStr(nRet));
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存格式设置失败,nRet = ' + IntToStr(nRet));
  end;
end;

procedure TForm1.Button9Click(Sender: TObject);
var
  nRet:Integer;
begin
  nRet := Syn_SetPhotoType(2);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存格式为Base64,nRet = ' + IntToStr(nRet));
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存格式设置失败,nRet = ' + IntToStr(nRet));
  end;
end;

procedure TForm1.Button10Click(Sender: TObject);
var
  nRet:Integer;
begin
  nRet := Syn_SetPhotoName(0);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存文件名格式为tmp,nRet = ' + IntToStr(nRet));
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存文件名格式设置失败,nRet = ' + IntToStr(nRet));
  end;
end;

procedure TForm1.Button11Click(Sender: TObject);
var
  nRet:Integer;
begin
  nRet := Syn_SetPhotoName(1);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存文件名格式为姓名,nRet = ' + IntToStr(nRet));
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存文件名格式设置失败,nRet = ' + IntToStr(nRet));
  end;
end;

procedure TForm1.Button12Click(Sender: TObject);
var
  nRet:Integer;
begin
  nRet := Syn_SetPhotoName(2);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存文件名格式为身份证号,nRet = ' + IntToStr(nRet));
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存文件名格式设置失败,nRet = ' + IntToStr(nRet));
  end;
end;

procedure TForm1.Button13Click(Sender: TObject);
var
  nRet:Integer;
begin
  nRet := Syn_SetPhotoName(3);
  if nRet = 0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存文件名格式为姓名_身份证号,nRet = ' + IntToStr(nRet));
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片保存文件名格式设置失败,nRet = ' + IntToStr(nRet));
  end;
end;

procedure TForm1.Button14Click(Sender: TObject);
var
  nRet:Integer;
  nPort:Integer;
begin
  nPort:=StrToInt(Edit1.Text);
  nRet := Syn_OpenPort(nPort);
  if nRet = 0 then begin
    if Syn_ResetSAM(nPort,0)=0 then begin
      ListBox1.Items.Add(DateTimeToStr(Now())+'  复位SAM模块成功');
    end else begin
      ListBox1.Items.Add(DateTimeToStr(Now())+'  复位SAM模块失败');
    end;
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  打开端口失败');
  end;
  Syn_ClosePort(nPort);
end;

procedure TForm1.Button15Click(Sender: TObject);
var
  nRet:Integer;
  nPort:Integer;
begin
  nPort:=StrToInt(Edit1.Text);
  nRet := Syn_OpenPort(nPort);
  if nRet = 0 then begin
    if Syn_GetSAMStatus(nPort,0)=0 then begin
      ListBox1.Items.Add(DateTimeToStr(Now())+'  SAM模块状态正常');
    end else begin
      ListBox1.Items.Add(DateTimeToStr(Now())+'  SAM模块不正常');
    end;
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  打开端口失败');
  end;
  Syn_ClosePort(nPort);
end;

procedure TForm1.Button16Click(Sender: TObject);
var
  nRet:Integer;
  nPort:Integer;
  uSAMID:array[0..127] of Char;
begin
  nPort:=StrToInt(Edit1.Text);
  nRet := Syn_OpenPort(nPort);
  if nRet = 0 then begin
    if Syn_GetSAMIDToStr(nPort,@uSAMID,0)=0 then begin
      ListBox1.Items.Add(DateTimeToStr(Now())+'  SAM模块ID为:'+ StrPas(uSAMID));
    end else begin
      ListBox1.Items.Add(DateTimeToStr(Now())+'  读SAM模块ID失败');
    end;
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  打开端口失败');
  end;
  Syn_ClosePort(nPort);
end;

procedure TForm1.Button17Click(Sender: TObject);
var
  nRet:Integer;
  nPort:Integer;
  pucIIN:array[0..3] of byte;
  pucSN:array[0..7] of byte;
  CardMsg:TIDCardData;
begin
  nPort:=StrToInt(Edit1.Text);
  nRet := Syn_OpenPort(nPort);
	Syn_SetSexType(1);
	Syn_SetNationType(1);
	Syn_SetBornType(2);
	Syn_SetUserLifeBType(3);
	Syn_SetUserLifeEType(4,1);
  if nRet = 0 then begin
	  Syn_SetMaxRFByte( nPort ,80 , 0);
	  Syn_StartFindIDCard( nPort , @pucIIN , 0 );
	  Syn_SelectIDCard( nPort , @pucSN , 0 );
    if Syn_ReadMsg( nPort , 0 , CardMsg)=0 then begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  读取身份证信息成功');
    ListBox1.Items.Add(DateTimeToStr(Now())+'  姓名：'+trim(CardMsg.Name));
    ListBox1.Items.Add(DateTimeToStr(Now())+'  性别：'+trim(CardMsg.sex));
    ListBox1.Items.Add(DateTimeToStr(Now())+'  民族：'+trim(CardMsg.Nation));
    ListBox1.Items.Add(DateTimeToStr(Now())+'  出生日期：'+trim(CardMsg.Born));
    ListBox1.Items.Add(DateTimeToStr(Now())+'  地址：'+trim(CardMsg.address));
    ListBox1.Items.Add(DateTimeToStr(Now())+'  身份证号：'+trim(CardMsg.Idcardno));
    ListBox1.Items.Add(DateTimeToStr(Now())+'  有效期开始：'+trim(CardMsg.UserLifeBegin));
    ListBox1.Items.Add(DateTimeToStr(Now())+'  有效期结束：'+trim(CardMsg.UserLifeEnd));
    ListBox1.Items.Add(DateTimeToStr(Now())+'  发证机关：'+trim(CardMsg.grantdept));
    ListBox1.Items.Add(DateTimeToStr(Now())+'  照片文件名：'+trim(CardMsg.photofilename));

    end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  读取身份证信息错误');
    end;
  end else begin
    ListBox1.Items.Add(DateTimeToStr(Now())+'  打开端口失败');
  end;
  Syn_ClosePort(nPort);
end;

end.
