123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using System;
- using System.Globalization;
- using System.IO;
- using System.Text;
- namespace Vinno.IdentificationCardApp
- {
- public class IDCardManager
- {
- private IDCardInfo _idCardInfo = new IDCardInfo();
- private const int IfOpen = 0;
- private int _portID;
- private bool _isUsbPort = true;
- private bool _portIsOpened;
- private readonly string _tempPath;
- public IDCardManager()
- {
- _tempPath = Path.Combine(Path.GetTempPath(), "IDCardApp");
- if (Directory.Exists(_tempPath)==false)
- {
- Directory.CreateDirectory(_tempPath);
- }
- }
- public IDCardInfo IDCardInfo {
- get { return _idCardInfo; }
- }
- public bool PortIsOpened
- {
- get { return _portIsOpened; }
- }
- public bool OpenPort()
- {
- _isUsbPort = false;
- int openPortReturnCode = 0;
- for (var iPort = 1001; iPort <= 1016; iPort++)
- {
- openPortReturnCode = SdtApi.SDT_OpenPort(iPort);
- if (openPortReturnCode == 0x90)
- {
- _portID = iPort;
- _isUsbPort = true;
- break;
- }
- }
- if (openPortReturnCode != 0x90)
- {
- _portIsOpened = false;
- return false;
- }
- _portIsOpened = true;
- return true;
- }
- public bool ClosePort()
- {
- var returnCode = SdtApi.SDT_ClosePort(_portID);
- if (returnCode == 0x90)
- {
- _portIsOpened = false;
- return true;
- }
- return false;
- }
- public ReadCardReturnCodeEnum ReadIcCard()
- {
- int pucIin = 0;
- int pucSn = 0;
- int puiChMsgLen = 0;
- int puiPhMsgLen = 0;
- var zpBmpFile = Path.Combine(_tempPath, "zp.bmp");
- var zpWltFile = Path.Combine(_tempPath, "zp.wlt");
- ReadCardReturnCodeEnum readCardCode = ReadCardReturnCodeEnum.Ok;
-
- var returnCode = SdtApi.SDT_StartFindIDCard(_portID, ref pucIin, IfOpen);
- if (returnCode == 0x80)
- {
- return ReadCardReturnCodeEnum.FindCardFailed;
- }
- if (returnCode != 0x80 && returnCode != 0x9F)
- {
- return ReadCardReturnCodeEnum.DeviceConnectionError;
- }
-
- returnCode = SdtApi.SDT_SelectIDCard(_portID, ref pucSn, IfOpen);
- if (returnCode == 0x81)
- {
- return ReadCardReturnCodeEnum.SelectCardFailed;
- }
- if (returnCode != 0x81 && returnCode != 0x90)
- {
- return ReadCardReturnCodeEnum.DeviceConnectionError;
- }
- try
- {
- var objFile = new FileInfo(zpBmpFile);
- if (objFile.Exists)
- {
- objFile.Attributes = FileAttributes.Normal;
- objFile.Delete();
- }
- objFile = new FileInfo(zpWltFile);
- if (objFile.Exists)
- {
- objFile.Attributes = FileAttributes.Normal;
- objFile.Delete();
- }
- }
- catch (Exception ex)
- {
- Logger.Error("ReadIcCard", ex);
- return ReadCardReturnCodeEnum.ClearTempFileFailed;
- }
- byte[] pucChMsg = new byte[4096];
- byte[] pucPhMsg = new byte[4096];
- returnCode = SdtApi.SDT_ReadBaseMsg(_portID, pucChMsg, ref puiChMsgLen, pucPhMsg, ref puiPhMsgLen, IfOpen);
- if (returnCode != 0x90)
- {
- return ReadCardReturnCodeEnum.ReadCardFailed;
- }
- try
- {
- _idCardInfo = new IDCardInfo();
- _idCardInfo.Name = GetString(pucChMsg, 0, 30);
- _idCardInfo.SexCode = GetString(pucChMsg, 30, 2);
- _idCardInfo.PeopleCode = GetString(pucChMsg, 32, 4);
- var strBirthday = GetString(pucChMsg, 36, 16);
- try
- {
- if (string.IsNullOrEmpty(strBirthday) == false)
- {
- var birthday = string.Format("{0}-{1}-{2}", strBirthday.Substring(0, 4),
- strBirthday.Substring(4, 2),
- strBirthday.Substring(6, 2));
- Logger.Info("Birthday: " + birthday);
- _idCardInfo.Birthday = Convert.ToDateTime(birthday, DateTimeFormatInfo.InvariantInfo);
- }
- }
- catch (Exception ex)
- {
- Logger.Error("Birthday", ex);
- }
- _idCardInfo.Address = GetString(pucChMsg, 52, 70);
- _idCardInfo.Idc = GetString(pucChMsg, 122, 36);
- _idCardInfo.SignAddress = GetString(pucChMsg, 158, 30);
- var strTem = GetString(pucChMsg, 188, pucChMsg.GetLength(0) - 188);
- try
- {
- if (string.IsNullOrEmpty(strTem) == false)
- {
- var strStartDate = string.Format("{0}-{1}-{2}", strTem.Substring(0, 4), strTem.Substring(4, 2),
- strTem.Substring(6, 2));
- Logger.Info("StartDate: " + strStartDate);
- _idCardInfo.StartDate = Convert.ToDateTime(strStartDate, DateTimeFormatInfo.InvariantInfo);
- }
- }
- catch (Exception ex)
- {
- Logger.Error("StartDate", ex);
- }
- try
- {
- if (string.IsNullOrEmpty(strTem) == false)
- {
- strTem = strTem.Substring(8);
- Logger.Info("EndDate: " + strTem.Trim());
- if (strTem.Trim() != "长期")
- {
- var strEndDate = string.Format("{0}-{1}-{2}", strTem.Substring(0, 4), strTem.Substring(4, 2),
- strTem.Substring(6, 2));
- Logger.Info("EndDate: " + strEndDate);
- _idCardInfo.EndDate = Convert.ToDateTime(strEndDate, DateTimeFormatInfo.InvariantInfo);
- }
- else
- {
- _idCardInfo.EndDate = DateTime.MaxValue;
- }
- }
- }
- catch (Exception ex)
- {
- Logger.Error("EndDate", ex);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- catch (Exception ex)
- {
- Logger.Error("ReadIcCard", ex);
- return ReadCardReturnCodeEnum.ParseCardInfoFailed;
- }
- return readCardCode;
- }
- private string GetString(byte[] bytes, int index, int count)
- {
- try
- {
- var str= Encoding.Unicode.GetString(bytes, index, count).Trim();
- return str;
- }
- catch (Exception ex)
- {
- Logger.Error(string.Format("GetString index:{0}, count:{1}", index, count));
- Logger.Error("GetString", ex);
- }
- return string.Empty;
- }
- }
- }
|