华视身份证阅读器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 关闭设备。

 

posted @ 2020-12-03 17:24  超级驼鹿  阅读(3310)  评论(0编辑  收藏  举报
/*