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 btnMake_Click(object sender, EventArgs e) { if (txtUseCode.Text.Trim().Length != 4) { MessageBox.Show("请输入正确的4位数卡号!"); return; } txtPhysicCode.Text = ""; CICCardRW mcIccObj = new CIcCardRWOne(iccdCom, 38400, 0, 0); try { bool isConn = false; try { isConn = mcIccObj.ConnectCOM(); } catch (Exception ex) { MessageBox.Show("操作台刷卡器建立连接异常:" + ex.ToString()); } if (isConn) { int nback = 0; Int16 nICType = 0; uint ICNum = 0; byte[] Icdata = new byte[16]; CICCard obj = null; nback = mcIccObj.RequestICCard(ref nICType); //初始化寻卡功能 if (nback == 0) { nback = mcIccObj.SelectCard(ref ICNum);//读取卡号 if (nback == 0) { txtPhysicCode.Text = ICNum.ToString(); obj = new CICCard(); obj.Code = txtUseCode.Text.Trim(); obj.CreateDtime = DateTime.Now; obj.Type = EnmICCardType.Temp; obj.Status = EnmICCardStatus.Normal; obj.PhysicCode = ICNum.ToString(); int rit = Program.mng.InsertICCard(obj); //插入卡号 if (rit == 100) { //向IC卡芯片写入信息 string str = obj.Code + "1" + DateTime.Now.ToString("yyyyMMdd") + "01";//4位数卡号、类型、制卡时间、收费标准 str = str + "fffffffffffffffff"; byte[] b = new byte[16]; mcIccObj.TransStringTo16Char(str, ref b); mcIccObj.WriteCard(1, 0, b, ref nback); //1为扇区,0为扇区数据块 if (nback == 0) { MessageBox.Show("卡操作成功!"); } else { MessageBox.Show("录入卡成功,但写操作失败:" + nback.ToString()); } } else { switch (rit) { case 101: MessageBox.Show("存在相同的用户卡号!"); break; case 102: MessageBox.Show("出现重复的用户卡号!"); break; default: MessageBox.Show("制卡异常,无法完成操作!"); break; } txtUseCode.Text = ""; } } } if (nback != 0) { MessageBox.Show("写数据失败,代码:" + nback.ToString(), "写操作失败"); } } mcIccObj.disConnectCOM(); } catch (Exception e1) { MessageBox.Show(e1.ToString()); } }
//读卡 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()); } }