华视身份证阅读器100UC HTTP模式二次开发
一:前言
设备已经安装好100UC的USB驱动,设备能正常工作
二:获取数据介绍
通过http请求,需要安装华视的网站服务,因此我会在请求服务之前检查改服务是否存在,我们的数据也来自这个网站的get请求。循环get请求来获取数据,经过我的测试2秒一次循环比较合适。
三:代码模块介绍
服务端循环get请求数据,通过事件注册,一旦数据和上次不同就通知调用我模块的客户端,也就是发生取身份证动作和放身份证动作时及时发聩给客户端。客户端拿到的身份证信息进行实时更新。
四:获取身份证模块代码
public class IDCardNetHttp { public bool OpenDevice() { //string openUrl = ConfigurationManager.AppSettings["OpenDevice"]; 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 string.IsNullOrEmpty(response.Content) ? false : true; } public string ReadCard() { //string readUrl = ConfigurationManager.AppSettings["readcard"]; 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 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); } private bool IsExistServer(string serviceName) { bool result = false; ServiceController[] controllers = ServiceController.GetServices(); int nNum = controllers.Length; try { for (int i = 0; i <= (nNum - 1); i++) { if ((controllers[i].ServiceName.ToUpper() == serviceName.ToUpper())) { result = true; break; } } } catch (Exception e) { Console.WriteLine(e.Message); result = false; } return result; } public event EventHandler<MyEventArgs> IDCardChangeNotify; public void GetIDCard() { if (IsExistServer("华视电子身份证读卡器服务")) { if (OpenDevice()) { IDCardMessage idcard = new IDCardMessage() { certNumber = "" }; Task.Factory.StartNew(() => { CancellationTokenSource cancelTokenSource = new CancellationTokenSource(); 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; } } ~IDCardNetHttp() { CloseDevice(); } }
五:客户端调用我的模块
static void Main(string[] args) { IDCardNetHttp Evet = new IDCardNetHttp(); Evet.IDCardChangeNotify += IDCardCallBack; Evet.GetIDCard(); Console.ReadKey(); } private static void IDCardCallBack(object sender, IDCardEvent.MyEventArgs e) { Console.WriteLine("状态改变:"+e.Msg.certNumber.ToString()); }