/// <summary> /// 写卡 /// </summary> private bool WriteCard(decimal money, int i, out string msg) { if (i >= 3) { msg = "3次写卡失败。请联系管理员。"; return(false); } try { #region 校验卡号 if (PubGlobal.BussinessVar.card.InCardno == CardStrFormater.GetCardNo(icCard.ReadIC("0", "1", PubGlobal.SysConfig.CzkPassword), PubGlobal.SysConfig.CzkLen)) { #region 写金额 msg = "写卡成功"; bool ok = icCard.WriteIC("1", "0", PubGlobal.SysConfig.CzkPassword, CardStrFormater.FormateMoney(money)); if (ok) { WintecIDT700.Beep(100); return(true); } else { return(false); } #endregion } else { throw new Exception("写卡失败"); } #endregion } catch { return(WriteCard(money, i++, out msg)); } }
static Mutex readCardStatus = new Mutex(true);//读卡状态标志 /// <summary> /// 读卡 /// </summary> private void ReadCard() { icCard = new ICCard("com2");//初始化 string inCardNo, msg; while (true) // { readCardStatus.WaitOne(); //等待窗口开启 #region 寻卡 if (!readSuccess && icCard.SearchIC()) { Thread.Sleep(100); #region 读卡号 try { inCardNo = CardStrFormater.GetCardNo(icCard.ReadIC("0", "1", PubGlobal.SysConfig.CzkPassword), PubGlobal.SysConfig.CzkLen); if (!string.IsNullOrEmpty(inCardNo)) { readSuccess = true; WintecIDT700.Beep(100); } } catch { //读卡失败,释放资源,重新读卡 //this.Invoke(dlgShowErrorMsgBox, "读卡错误"); readCardStatus.ReleaseMutex(); readSuccess = false; continue; } #endregion Thread.Sleep(100); this.Invoke(dlgShowWaitMsg, "正在读取金额"); #region 读金额 if (!TransModule.GetCzCard(inCardNo, out msg)) { //读取金额失败 this.Invoke(dlgShowErrorMsgBox, "读取金额失败\r\n错误原因:" + msg); this.Invoke(dlgHideWaitMsg); readSuccess = false; readCardStatus.ReleaseMutex(); continue; } #endregion if (!WriteCard(PubGlobal.BussinessVar.card.Total - PubGlobal.BussinessVar.Total, 1, out msg)) { MessageBox.Show("写金额失败"); } Thread.Sleep(100); this.Invoke(dlgShowWaitMsg, "正在交易"); #region 交易 //PubGlobal.BussinessVar.card.Total = CardStrFormater.GetMoney(icCard.ReadIC("1", "0", PubGlobal.SysConfig.CzkPassword)); //WintecIDT700.Beep(100); if (Trade(out msg)) { this.Invoke(dlgShowInfoMsgBox, "交易成功\r\n卡余额:" + PubGlobal.BussinessVar.card.Total); tradeSuccess = true; } else { this.Invoke(dlgShowErrorMsgBox, "交易失败\r\n失败原因:" + msg); //交易失败 if (!WriteCard(PubGlobal.BussinessVar.card.Total, 1, out msg)) { MessageBox.Show("写卡失败"); } PubGlobal.BussinessVar.card = null; } #endregion this.Invoke(dlgHideWaitMsg); } #endregion readCardStatus.ReleaseMutex(); //释放阻塞 this.Invoke(dlgCloseForm); //关闭窗口 Thread.Sleep(500); //线程休眠500毫秒 } }