public bool ReadCard(ref IDCardData _idcarddata, ref string msg) { try { Int32 result = OpenCardReader(port, 0x02); if (result != 0) { msg = "打卡设备端口失败"; return(false); } PERSONINFOW info = new PERSONINFOW(); result = GetPersonMsgW(ref info, ""); if (result != 0) { msg = "读身份证失败"; return(false); } _idcarddata = new IDCardData(); _idcarddata.Address = info.address; _idcarddata.Born = string.Format("{0}-{1}-{2}", info.birthday.Substring(0, 4), info.birthday.Substring(4, 2), info.birthday.Substring(6, 2)); _idcarddata.GrantDept = info.police; _idcarddata.IDCardNo = info.cardId; _idcarddata.Name = info.name; _idcarddata.Nation = info.nation + "族"; _idcarddata.Sex = info.sex; _idcarddata.UserLifeBegin = info.validStart; _idcarddata.UserLifeEnd = info.validEnd; msg = "读取成功"; return(true); } catch { msg = "未知的错误"; return(false); } finally { CloseCardReader(); } }
private static extern int IdcrReadAllW(IntPtr pRD, [In, Out] PERSONINFOW pInfo, string pszPhotoPath, [In, Out] FPINFOW pFPInfo, int bRepeatRead, int bReadAppend);
public static extern Int32 GetPersonMsgW(ref PERSONINFOW pInfo, string pszImageFile);
/// <summary> /// /// </summary> /// <param name="com">1表示串口1,2表示串口2,以此类推;1001表示USB;0表示自动选择</param> /// <returns></returns> public PersonInfo ReadPersonInfo() { var p = new PersonInfo(); try { person = new PERSONINFOW(); //p.Birthday = "1989-07-23"; //p.CardNO = "610424198907232671"; //p.Address = "显示莲湖区北院门178号"; //p.Name = "张三"; #region 打开设备 Int32 result; StringBuilder errorText = new StringBuilder(); /*参数1为端口号。1表示串口1,2表示串口2,依次类推。1001表示USB。0表示自动选择。 * 参数2为标志位。0x02表示启用重复读卡。0x04表示读卡后接着读取新地址。 * 各个数值可以用“按位或”运算符组合起来。 * 参数3为波特率。使用串口阅读器的程序应正确设置此参数。出厂机器的波特率一般为115200。 */ #region 检测 result = OpenCardReader(iPort, 2, 115200); var msg = Convert.ToString(result); GetErrorTextW(errorText, maxErrorTextLen); if (result > 0) { throw new Exception(errorText.ToString()); } #endregion #endregion //textDescription.Text = errorText.ToString(); #region 读取信息 String imagePath; imagePath = Path.GetTempPath() + "image.bmp"; result = GetPersonMsgW(ref person, imagePath); if (result > 0) { switch (result) { case 1: throw new Exception("端口打开失败!"); case 2: throw new Exception("数据传输超时!"); case 10: throw new Exception("没有找到卡!"); case 11: throw new Exception("读卡操作失败!"); case 20: throw new Exception("自检失败!"); case 30: throw new Exception("其他错误!"); } } else { p.Name = person.name; p.CardNO = person.cardId; p.Address = person.address; p.Sex = person.sex; p.Birthday = person.birthday; p.GrantDept = person.police; p.IDCardBeginDate = person.validStart; p.IDCardEndDate = person.validEnd; p.Nation = person.nation; p.Photo = Image.FromFile(imagePath); } #endregion } catch (Exception ex) { throw new Exception("中国普天读卡器异常:\r" + ex.Message, ex); } finally { #region 关闭设备 CloseCardReader(); #endregion } return(p); //throw new NotImplementedException(); }