1. 程式人生 > 實用技巧 >華視身份證閱讀器100UC HTTP模式二次開發

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

一:前言

   裝置已經安裝好100UC驅動,裝置能正常工作

二:獲取資料介紹

   通過http請求,需要安裝華視的網站服務,我們的資料也來自這個網站的get請求。迴圈get請求來獲取資料,經過我的測試

三:程式碼模組介紹

   通過事件註冊,服務端迴圈get請求資料,一旦資料和上次不同就通知呼叫我模組的客戶端。客戶端拿到的身份證資訊實時更新。

四:獲取身份證模組程式碼

    public class IDCardEvent 
    {
        public static bool OpenDevice()
        {
            //string openUrl = ConfigurationManager.AppSettings["OpenDevice"] as string;
var client = new RestClient("http://127.0.0.1:19196/OpenDevice"); client.Timeout = -1; var request = new RestRequest(Method.GET); IRestResponse response = client.Execute(request); //Console.WriteLine(response.Content); return
true; } public static string ReadCard() { //string readUrl = ConfigurationManager.AppSettings["readcard"].ToString(); var client = new RestClient("http://127.0.0.1:19196/readcard"); client.Timeout = -1; var request = new RestRequest(Method.GET); IRestResponse response
= client.Execute(request); return response.Content; } public static void CloseDevice() { //string closeUrl = ConfigurationManager.AppSettings["CloseDevice"]; var client = new RestClient("http://127.0.0.1:19196/CloseDevice"); client.Timeout = -1; var request = new RestRequest(Method.GET); IRestResponse response = client.Execute(request); //Console.WriteLine(response.Content); } public event EventHandler<MyEventArgs> IDCardChangeNotify; static IDCardMessage idcard = new IDCardMessage() { certNumber = "" }; public void GetIDCard() { if (OpenDevice()) { CancellationTokenSource cancelTokenSource = new CancellationTokenSource(); Task.Factory.StartNew(() => { while (!cancelTokenSource.IsCancellationRequested) { IDCardMessage message = JsonConvert.DeserializeObject<IDCardMessage>(ReadCard()); if (idcard.certNumber != message.certNumber) { idcard = message; IDCardChangeNotify(this, new MyEventArgs(idcard)); } Thread.Sleep(2000); } }); } } public class MyEventArgs : EventArgs { IDCardMessage _msg; public MyEventArgs(IDCardMessage msg) { this._msg = msg; } public IDCardMessage Msg { get { return _msg; } } } public class IDCardMessage { public string bornDay { get; set; } public string certAddress { get; set; } public string certNumber { get; set; } public string certOrg { get; set; } public string certType { get; set; } public string effDate { get; set; } public string errorMsg { get; set; } public string expDate { get; set; } public string gender { get; set; } public string identityPic { get; set; } public string nation { get; set; } public string nationCode { get; set; } public string partyName { get; set; } public string resultFlag { get; set; } } ~IDCardEvent() { CloseDevice(); } }

五:客戶端呼叫我的模組

        static void Main(string[] args)
        {
            IDCardEvent Evet = new IDCardEvent();
            Evet.IDCardChangeNotify += IDCardCallBack;         

            Evet.GetIDCard();     
            Console.ReadKey();        
        }   
        private static void IDCardCallBack(object sender, IDCardEvent.MyEventArgs e)  
        {
            Console.WriteLine("狀態改變:"+e.Msg.certNumber.ToString());      
        }