1. 程式人生 > >華視cvr-100UC 二代身份證讀卡通用函式

華視cvr-100UC 二代身份證讀卡通用函式

本方法只針對的是華視cvr-100UC 二代身份證讀卡。

只是寫了主要的部分,可以把函式等放入公共檔案中,引用,呼叫即可。剩下的自己琢磨吧!

先定義個機構:

  TPsnMsg = record
    PeoName:           string;    //姓名
    PeoSex:            string;    //性別
    PeoFolk:           string;    //
    PeoBirthday:       string;    //生日
    PeoAddress:        string;    //地址
    PeoIDNumber:       string;    //身份證號
    PeoDepartment:     string;    //發證機關
    PeoStartDate:      string;    //起止日期
    PeoEndDate:        string;
    PeoReserve:        string;
    paddaddress1:      string;
    m_szphoto:         array [0..1023]             of char;
  end;

以下是函式:

function GetPsnMsg_100UC(iPort:integer):TPsnMsg;

var
  iRet, iTimeOut :integer;
  TimeCurr, TimeDelay:int64;

  iFileHandle,iFileLength: Integer;
  Buffer: PWideChar;
    
  sWZ :WideString;
  sDisp, iniFileName:string;
  ChPeople:array[0..255] of char;

  ID: Cardinal    ;
 
  szGet :array[0..70] of char;
  iLenGet:integer;
begin
   
    iRet:=CVR_InitComm(iPort);     //初始化連線; iport=1001 為USB介面
    if iRet<>1  then
    begin
        showmessage('開啟串口出錯');
        CVR_CloseComm();            //
        exit;
    end
    else
    begin
        GetManuID(@ID);    //20080610 Add By Kim
        //ShowMessage('請放卡');
    end;

    TimeCurr := GetTickCount();
    TimeDelay := iTimeOut * 1000;

    iRet := CVR_Authenticate();    // 卡認證;

    if NOT iRet=1 then EXIT;
    Delay(200);

    iRet := CVR_Read_Content(1);      //讀卡操作
    CVR_CloseComm();                  //關閉連線;
    if iRet<>1 then
    begin
        //showmessage('讀基本資訊出錯');
        exit;
    end;

    //------------------------------------------------------------------顯示資料

    try
      iniFileName:=ExtractFilePath(Application.ExeName) + 'ICINIT.INI';
      iFileHandle := FileOpen(ExtractFilePath(Application.ExeName) +'wz.txt', fmOpenRead);
      iFileLength := FileSeek(iFileHandle,0,2);
      FileSeek(iFileHandle,0,0);
      Buffer := PWideChar(AllocMem(iFileLength +2));
      FileRead(iFileHandle, Buffer^, iFileLength);
      FileClose(iFileHandle);
      sWZ:=WideChartostring(buffer);

       PsnMsg.PeoName:=copy(sWZ,1,15);  //姓名

       delete(sWZ,1,15);
       if (copy(sWZ,1,1)='1') then     //性別
          PsnMsg.PeoSex:='男'
       else
          PsnMsg.PeoSex:='女';

       delete(sWZ,1,1);                //民族
       sDisp:='';
       sDisp:=copy(sWZ,1,2);
       PsnMsg.PeoFolk:= GetMinZhuToName(sDisp);

       delete(sWZ,1,2);                //出生
       sDisp:='';
       PsnMsg.PeoBirthday:=copy(sWZ,1,4)+'-'+copy(sWZ,5,2)+'-'+copy(sWZ,7,2);

       delete(sWZ,1,8);               //住址
       PsnMsg.PeoAddress:=copy(sWZ,1,35);

       delete(sWZ,1,35);              //身份證號
       PsnMsg.PeoIDNumber:=copy(sWZ,1,18);

       delete(sWZ,1,18);              //簽發機關
       PsnMsg.PeoDepartment:=copy(sWZ,1,15);

       sDisp:='';                     //有效期限
       delete(sWZ,1,15);
       PsnMsg.PeoStartDate:=copy(sWZ,1,4)+'-'+copy(sWZ,5,2)+'-'+copy(sWZ,7,2);;

       sDisp:='';                     //有效期限
       delete(sWZ,1,8);
       PsnMsg.PeoEndDate:=copy(sWZ,1,4)+'-'+copy(sWZ,5,2)+'-'+copy(sWZ,7,2);;

       delete(sWZ,1,8);              //最新住址
       PsnMsg.PeoReserve:=copy(sWZ,1,35);
                                     //顯示照片
       //Image2.Picture.LoadFromFile(ExtractFilePath(Application.ExeName) +'zp.bmp');
       //Image2.Visible := TRUE;

       windows.Beep(2800,200);
       Result := PsnMsg;
    finally
       FreeMem(Buffer);
    end;


end;