private string readIccard() { CICCardRW iccdObj = new CIcCardRWOne(iccdCom, 9600, 0, 0); bool isConn = false; string iccode = ""; try { isConn = iccdObj.ConnectCOM(); } catch (Exception ex) { MessageBox.Show("刷卡器建立异常:" + ex.ToString()); } try { if (isConn) { int nback = 1; Int16 nICType = 0; uint ICNum = 0; byte[] IcData = new byte[16]; nback = iccdObj.RequestICCard(ref nICType); //寻卡 if (nback == 0) { nback = iccdObj.SelectCard(ref ICNum); //读取物理卡号 if (nback == 0) { nback = iccdObj.ReadCard(1, 0, ref IcData); //读取指定扇区:1,指定DB块:0 的数据 if (nback == 0) { string data = ""; for (int i = 0; i < IcData.Length; i++) { string a = Convert.ToString(IcData[i], 16); if (a.Length < 2) { a = "0" + a; } data += a; } iccode = data.Substring(0, 4); //4位数卡号 } } } } iccdObj.disConnectCOM(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return(iccode); }
//读卡 private void btnRead_Click(object sender, EventArgs e) { txtPhysicCode.Text = ""; txtUseCode.Text = ""; try { CICCardRW mcIccObj = new CIcCardRWOne(iccdCom, 9600, 0, 0); bool isConn = false; try { isConn = mcIccObj.ConnectCOM(); } catch (Exception ex) { MessageBox.Show("操作台刷卡器建立连接异常:" + ex.ToString()); } if (isConn) { int nback = 1; Int16 nICType = 0; uint ICNum = 0; byte[] IcData = new byte[16]; nback = mcIccObj.RequestICCard(ref nICType); //寻卡 if (nback == 0) { nback = mcIccObj.SelectCard(ref ICNum); //读取物理卡号 if (nback == 0) { txtPhysicCode.Text = ICNum.ToString(); try { nback = mcIccObj.ReadCard(1, 0, ref IcData); //读取指定扇区:1,指定DB块:0 的数据 if (nback == 0) { string data = ""; for (int i = 0; i < IcData.Length; i++) { string a = Convert.ToString(IcData[i], 16); if (a.Length < 2) { a = "0" + a; } data += a; } txtUseCode.Text = data.Substring(0, 4); //4位数卡号 } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } mcIccObj.disConnectCOM(); //关闭刷卡器 } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }