IDCert.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using System.Web;
  7. namespace EmisTerminal.EIF
  8. {
  9. public class IDCert
  10. {
  11. [DllImport("termb.dll")]
  12. public static extern int InitComm(int Port);
  13. [DllImport("termb.dll")]
  14. public static extern int CloseComm();
  15. [DllImport("termb.dll")]
  16. public static extern int Authenticate();
  17. [DllImport("termb.dll")]
  18. public static extern int Read_Content(int active);
  19. [DllImport("termb.dll")]
  20. public static extern int Read_Content_Path(string cPath, int active);
  21. [DllImport("termb.dll")]
  22. public static extern IntPtr GetIDNo();
  23. [DllImport("termb.dll", CharSet = CharSet.Auto)]
  24. public static extern bool GetIDNo2(ref byte sBuf, ref int nLen);
  25. [DllImport("termb.dll")]
  26. public static extern int GetSAMID(IntPtr c);
  27. [DllImport("termb.dll")]
  28. public static extern int ResetCard(int nReaderSlot, ref string pRecvDatas, ref int nRecvLen);
  29. public static bool isStop = false;
  30. public static int InitCommExt()
  31. {
  32. for (int i = 1001; i <= 1016; i++)
  33. {
  34. if (InitComm(i) == 0)
  35. {
  36. return i;
  37. }
  38. }
  39. for (int i = 1; i <= 16; i++)
  40. {
  41. if (InitComm(i) == 0)
  42. {
  43. return i;
  44. }
  45. }
  46. if (InitComm(2401) == 0)
  47. {
  48. return 2401;
  49. }
  50. return -6;
  51. }
  52. public static ResultMessage<String> Check()
  53. {
  54. try
  55. {
  56. isStop = true;
  57. Thread.Sleep(500);
  58. isStop = false;
  59. int port = IDCert.InitCommExt();
  60. if (port >= 0)
  61. {
  62. while (!isStop)
  63. {
  64. IntPtr c = Marshal.AllocHGlobal(100);
  65. int samRet = IDCert.GetSAMID(c);
  66. int ret = IDCert.Authenticate();
  67. if (ret == 0)
  68. {
  69. //int readRet = IDCert.Read_Content(1);
  70. int readRet = IDCert.Read_Content_Path(HttpContext.Current.Server.MapPath("~/"), 1);
  71. if (readRet == 0)
  72. {
  73. ResultMessage<String> result = new ResultMessage<String>();
  74. result.IsSuccess = true;
  75. result.Message = "成功";
  76. byte[] arrTmp = new byte[100];
  77. int nLen = 100;
  78. IDCert.GetIDNo2(ref arrTmp[0], ref nLen);
  79. result.Data = System.Text.Encoding.GetEncoding("GB2312").GetString(arrTmp.Take(nLen).ToArray());
  80. return result;
  81. }
  82. else
  83. {
  84. IDCert.CloseComm();
  85. IDCert.InitComm(port);
  86. }
  87. }
  88. else if (ret == -12)
  89. {
  90. IDCert.CloseComm();
  91. IDCert.InitComm(port);
  92. }
  93. Thread.Sleep(300);
  94. }
  95. ResultMessage<String> stopResult = new ResultMessage<String>();
  96. stopResult.IsSuccess = false;
  97. stopResult.Message = "正常退出。";
  98. return stopResult;
  99. }
  100. else
  101. {
  102. ResultMessage<String> result = new ResultMessage<String>();
  103. result.IsSuccess = false;
  104. result.Message = "找不到读卡器的端口,请确认是否已经安装了身份证读卡器硬件。";
  105. return result;
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. ResultMessage<String> result = new ResultMessage<String>();
  111. result.IsSuccess = false;
  112. result.Message = ex.Message;
  113. return result;
  114. }
  115. finally
  116. {
  117. IDCert.CloseComm();
  118. }
  119. }
  120. }
  121. }