華視身份證閱讀器100UC SDK模式二次開發


一:前言

   設備已經安裝好100UC的USB驅動,設備能正常工作

二:獲取數據介紹

   通過調取sdk接口獲取數據,sdk同http模式不同,初始化完成以后,然后使用 CVR_Authenticate 來確認是否存在身份證,如果存在則去獲取身份證信息通過事件反饋給客戶端上層,程序讀取完一次后 sdk 關閉了數據獲取,需要手動拿起身份證卡片后才能再次確認身份證是否存在,限制了讀卡次數。開發包還提供和 http 相同的模式,初始化完成以后,使用 CVR_AuthenticateForNoJudge 關閉身份證驗證,則可以一直讀取身份證信息,取消讀卡次數,無需拿起再放下。

三:反饋客戶端身份證信息 IDCardNetSDK 類模塊代碼包含下面三部分

  1.先用C#翻譯C++的接口

 public  class ClientInterfance 
    { 
        /// <summary>
        /// 用於第二代居民身份證閱讀器連接
        /// </summary>
        /// <param name="Port">端口編號:連接串口(com1~com16)或者usb口(1001~1016)</param>
        /// <returns>1:正確,2:端口打開失敗,-1:未知錯誤,-2:動態庫加載失敗</returns> 
        [DllImport("termb.dll")]
        public static extern int CVR_InitComm(int Port);
        /// <summary>
        /// 讀卡器和卡片之間的合法身份確認,卡循環間隔大於300ms。若卡片放置后認證失敗,重新放置。
        /// </summary>
        /// <returns>1:卡片認證成功,2:尋卡失敗,3:選卡失敗,4:未連接讀卡器,0:動態庫未加載</returns>
        [DllImport("termb.dll")]
        public static extern int CVR_Authenticate();
        /// <summary>
        /// 關閉卡片身份確認,使卡片可以一直讀,此函數只返回1.
        /// </summary>
        /// <returns></returns>
        [DllImport("termb.dll")]
        public static extern int CVR_AuthenticateForNoJudge();

        /// <summary>
        /// 通過閱讀器從第二代居民身份證讀取信息。卡認證成功一候才可讀卡操作,讀卡完畢若繼續讀卡應移走二代證卡片重新放置做卡認證
        /// </summary>
        /// <returns>1:正確,0:錯誤,讀取失敗,4:讀卡器未連接,99:動態庫未加載</returns>
        [DllImport("termb.dll")]
        public static extern int CVR_Read_FPContent();   
        /// <summary>
        /// 關閉pc到閱讀器的連接
        /// </summary>
        /// <returns>1:關閉成功,0:端口號不合法,-1:端口已經關閉,-2動態庫加載失敗</returns>
        [DllImport("termb.dll")]  
        public static extern int CVR_CloseComm();
        /// <summary>
        /// 得到姓名信息 
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        public static extern int GetPeopleName(ref byte strTmp,ref int strLen);
        /// <summary>
        /// 得到性別信息
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        public static extern int GetPeopleSex(ref byte strTmp, ref int strLen);
        /// <summary>
        /// //得到民族信息
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>

        [DllImport("termb.dll")]
        public static extern int GetPeopleNation(ref byte strTmp, ref int strLen);
        /// <summary>
        /// 得到出生日期
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>

        [DllImport("termb.dll")]
        public static extern int GetPeopleBirthday(ref byte strTmp, ref int strLen);
        /// <summary>
        /// 得到地址信息
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>

        [DllImport("termb.dll")]
        public static extern int GetPeopleAddress(ref byte strTmp, ref int strLen);
        /// <summary>
        /// 得到卡號信息
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        public static extern int GetPeopleIDCode(ref byte strTmp, ref int strLen);
        /// <summary>
        /// 得到發證機關信息
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        public static extern int GetDepartment(ref byte strTmp, ref int strLen);
        /// <summary>
        /// 得到有效開始日期
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        public static extern int GetStartDate(ref byte strTmp, ref int strLen);
        /// <summary>
        /// 得到有效截止日期  
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="strLen"></param>
        /// <returns></returns>
        [DllImport("termb.dll")]
        public static extern int GetEndDate(ref byte strTmp, ref int strLen);
    }

  2.設備初始化、檢查卡片合法性、是否讀取卡片、讀取卡片 

 public bool isIniCoom()
        {
            for (int i = 1001; i <= 1016; i++)
            {
                if(ClientInterfance.CVR_InitComm(i) == 1)  
                {
                    return true; 
                }
            }
            return false; 
        } 
        public bool isAuthen()
        {
            if (ClientInterfance.CVR_Authenticate() == 1)
            {
                return true;
            }
            return false; 
        }
        public bool isReadCont()
        {
            if (ClientInterfance.CVR_Read_FPContent() == 1)
            {
                return true;
            }
            return false; 
        }
        public void isReaded()
        {
            IDCardMessage idcard = new IDCardMessage();

            byte[] name = new byte[30];
            int length = 30;
            ClientInterfance.GetPeopleName(ref name[0], ref length);
            idcard.Name = Encoding.GetEncoding("GB2312").GetString(name).Replace("\0", "").Trim();

            byte[] sex = new byte[30];
            length = 3;
            ClientInterfance.GetPeopleSex(ref sex[0], ref length);
            idcard.Sex = Encoding.GetEncoding("GB2312").GetString(sex).Replace("\0", "").Trim();

            byte[] people = new byte[30];
            length = 3;
            ClientInterfance.GetPeopleNation(ref people[0], ref length);
            idcard.National = Encoding.GetEncoding("GB2312").GetString(people).Replace("\0", "").Trim();

            byte[] birthday = new byte[30];
            length = 16;
            ClientInterfance.GetPeopleBirthday(ref birthday[0], ref length);
            idcard.BirthDay = Encoding.GetEncoding("GB2312").GetString(birthday).Replace("\0", "").Trim();

            byte[] address = new byte[30];
            length = 70;
            ClientInterfance.GetPeopleAddress(ref address[0], ref length);
            idcard.Address = Encoding.GetEncoding("GB2312").GetString(address).Replace("\0", "").Trim();

            byte[] number = new byte[30];
            length = 36;
            ClientInterfance.GetPeopleIDCode(ref number[0], ref length);
            idcard.IdentityId = Encoding.GetEncoding("GB2312").GetString(number).Replace("\0", "").Trim();

            byte[] signdate = new byte[30];
            length = 30;
            ClientInterfance.GetDepartment(ref signdate[0], ref length);
            idcard.Gov = Encoding.GetEncoding("GB2312").GetString(signdate).Replace("\0", "").Trim();

            byte[] validtermOfStart = new byte[30];
            length = 16;
            ClientInterfance.GetStartDate(ref validtermOfStart[0], ref length);
            idcard.StartDate = Encoding.GetEncoding("GB2312").GetString(validtermOfStart).Replace("\0", "").Trim();

            byte[] validtermOfEnd = new byte[30];
            length = 16;
            ClientInterfance.GetEndDate(ref validtermOfEnd[0], ref length);
            idcard.EndDate = Encoding.GetEncoding("GB2312").GetString(validtermOfEnd).Replace("\0", "").Trim();

            IDCardChangeNotify(this, new MyEventArgs(idcard));
        }

  3.事件反饋

public event EventHandler<MyEventArgs> IDCardChangeNotify;   
        public class MyEventArgs : EventArgs
        {
            IDCardMessage _msg;
            public MyEventArgs(IDCardMessage msg)
            {
                this._msg = msg;
            }
            public IDCardMessage Msg
            {
                get { return _msg; }
            }
        }
        public class IDCardMessage
        {
            public string Name { get; set; }         
            public string Sex { get; set; }
            public string National { get; set; }         
            public string BirthDay { get; set; }
            public string Address { get; set; }
            public string IdentityId { get; set; }
            public string Gov { get; set; }                             
            public string StartDate { get; set; }
            public string EndDate { get; set; }   
        }

  4.開啟任務獲取數據方法

    public void GetIDCard()
        {
            CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
            cancelTokenSource.Token.Register(()=> { ClientInterfance.CVR_CloseComm(); });
            Task.Factory.StartNew(() => {
                while (!cancelTokenSource.IsCancellationRequested)
                {
                    Console.WriteLine("1.即將初始化設備");
                    if(isIniCoom()) 
                    {
                        Console.WriteLine("2.即將檢查卡片合法性"); 
                        if (isAuthen())
                        {
                            Console.WriteLine("3.即將准備讀取");
                            if (isReadCont())  
                            {
                                isReaded(); 
                            }
                            else
                            {
                                ClientInterfance.CVR_CloseComm();
                                Console.WriteLine("3.讀取卡片失敗,已關閉接口調用"); 
                            }
                        }
                        else
                        {
                            ClientInterfance.CVR_CloseComm(); 
                            Console.WriteLine("2.檢查卡片合法性失敗,已關閉接口調用");
                        }                     
                    }
                    else
                    {                         
                        ClientInterfance.CVR_CloseComm();
                        Console.WriteLine("初始化失敗,已關閉接口調用");
                    } 
                     
                    Thread.Sleep(500);                                      
                }                                         
             }, cancelTokenSource.Token);
        }

四:客戶端調用我的模塊

 static void Main(string[] args)
 {
      IDCardNetSDK Evet = new IDCardNetSDK();
      Evet.IDCardChangeNotify += IDCardSDKCallBack;
      Evet.GetIDCard();
      Console.ReadKey();         
 }
 private static void IDCardSDKCallBack(object sender, IDCardNetSDK.MyEventArgs e)
 {
      Console.WriteLine("狀態改變:" + e.Msg.IdentityId.ToString()); 
 }

PS: 如果我們在使用的過程中插拔設備,而我們的代碼在 初始化設備 、檢查卡片合法性、讀卡等動作中失敗了,一定要加一句 CVR_CloseCommn 關閉設備。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM