/// <summary> /// 读取卡号 /// </summary> /// <returns></returns> public string ReadCardOrgNo() { string result = string.Empty; try { if (this._icDev <= 0) { this.Open(); } if (this._icDev > 0) { short num = Rf32Controller.rf_card(this._icDev, 0, out this.cardNo); if (num == 0) { result = this.cardNo.ToString(); Rf32Controller.rf_beep(_icDev, 20); } else { IcCardReaderLogger.Error(string.Format("ReadCardOrgNo读卡失败,原因:{0}", num)); } } else { IcCardReaderLogger.Error("ReadCardOrgNo打开IC读卡器失败!_icDev=" + _icDev.ToString()); } } catch (Exception ex) { IcCardReaderLogger.Error("ReadCardOrgNo执行异常!_icDev=" + _icDev.ToString(), ex); } return(result); }
/// <summary> /// 初始化设备 /// </summary> /// <returns></returns> public bool Open() { bool rtn = false; try { if (_icDev > 0) { this.Stop(); this.Close(); Thread.Sleep(1000); } _icDev = Rf32Controller.rf_init(short.Parse(_icCardCfg.ComPort), int.Parse(_icCardCfg.Baudrate.Substring(0, _icCardCfg.Baudrate.Length - 3))); if (_icDev > 0) { IcCardReaderLogger.Debug("打开IC读卡器成功!"); Rf32Controller.rf_beep(_icDev, 20); IcCardReaderLogger.Debug("IC读卡器播放声音!"); rtn = true; } else { IcCardReaderLogger.Error("打开IC读卡器失败!_icDev=" + _icDev.ToString()); string msg = GetErrMsgByErrCode(_icDev); ShowMsg(msg); } } catch (Exception ex) { IcCardReaderLogger.Error("打开IC读卡器异常!", ex); ShowMsg("IC读卡器初始化异常."); } return(rtn); }
/// <summary> /// 读卡回调函数 /// </summary> /// <param name="paramObject"></param> private void Alarm() { int temp = 1000;//默认寻卡时间 int.TryParse(_icCardCfg.Interval, out temp); IcCardReaderLogger.Debug("读取IC读卡器寻卡时间!Interval=" + temp); while (_searchCard) { short result = Rf32Controller.rf_card(_icDev, 0, out cardNo); IcCardReaderLogger.Debug("IC读卡器寻卡!result=" + result.ToString()); if (result == 0) { Rf32Controller.rf_beep(_icDev, 20); IcCardReaderLogger.Debug("读卡成功"); if (OnReadCardNo != null && _canReadCard) { if (_isDebug) { OnReadCardNo(_icCardCfg.ComPort, "B35CADE2"); } else { IcCardReaderLogger.Debug(string.Format("触发读卡事件。ComPort={0},cardNo={1}", _icCardCfg.ComPort, cardNo.ToString())); OnReadCardNo(_icCardCfg.ComPort, cardNo.ToString()); } } ////启动监控定时器 //TimerCallback callBack = new TimerCallback(MonitorCard); //_monitorCardNoTimer = new Timer(MonitorCard, null, Timeout.Infinite, 500); //_monitorCardNoTimer.Change(100, 500); } Thread.Sleep(temp); } }
public IcCardsController(string pConfigFile, bool pCanReadData) { //读取配置 IcCardReaderLogger.Debug(string.Format("---------------------开始------------------------")); ConfigReader cfgReader = new ConfigReader(pConfigFile); IcCardReaderLogger.Debug(string.Format("加载配置文件成功!file={0}", pConfigFile)); List <ICCard> icCardList = ConfigReader.ReadIcCard(); IcCardReaderLogger.Debug(string.Format("读取配置文件成功!")); InnerInit(icCardList, pCanReadData); }
/// <summary> /// 开始 /// </summary> public bool Start() { bool rtn = true; try { if (_icDev <= 0) { IcCardReaderLogger.Debug("在启动时打开IC读卡器!"); rtn = Open(); } if (_icDev > 0) { if (threadICCardReader != null) { while (threadICCardReader.IsAlive) { threadICCardReader.Abort(); IcCardReaderLogger.Debug("强制结束IC读卡器寻卡线程。"); } } threadICCardReader = new Thread(new ThreadStart(Alarm)); threadICCardReader.IsBackground = true; _searchCard = true; threadICCardReader.Start(); IcCardReaderLogger.Debug("启动IC读卡器寻卡线程。"); } else { rtn = false; IcCardReaderLogger.Debug("设备故障,无法启动寻卡线程。_icDev=" + _icDev.ToString()); ShowMsg(string.Format("IC卡打开失败,返回值={0}", _icDev.ToString())); } } catch (Exception ex) { rtn = false; IcCardReaderLogger.Error("启动IC读卡器寻卡异常!", ex); throw ex; } return(rtn); }
/// <summary> /// 注销设备 /// </summary> /// <returns></returns> public bool Close() { bool rtn = false; IcCardReaderLogger.Debug("关闭IC卡读卡器!_icDev=" + _icDev.ToString()); if (_icDev > 0) { if (Rf32Controller.rf_exit(_icDev) == 0) { ; } { IcCardReaderLogger.Debug("关闭IC卡读卡器成功!"); _icDev = -1; } rtn = true; //_log.Info(string.Format("关闭退出IC卡定时器,退出成功。", _icDev)); } return(rtn); }
/// <summary> /// 创建实例 /// 如果遇到多个卡,其中有的是相同的品牌,一个程序集不能加载两次 /// </summary> /// <returns></returns> private IIcController CreateInstance(ICCard pICCardConfig) { IIcController io = null; try { string driverPath = System.AppDomain.CurrentDomain.BaseDirectory; driverPath = Path.Combine(driverPath, pICCardConfig.Driver); //修改开始 20181120 Assembly assembly = null; if (loadedAssmbly.ContainsKey(driverPath)) { assembly = loadedAssmbly[driverPath]; } else { assembly = Assembly.LoadFile(driverPath); } //修改介绍 下面一句被注释掉 // Assembly assembly = Assembly.LoadFile(driverPath); string name = assembly.FullName.Split(',')[0] + ".IcController"; Type type = assembly.GetType(name); IcCardReaderLogger.Debug(string.Format("载入硬件封装模块!DLL={0}", driverPath)); io = Activator.CreateInstance(type, pICCardConfig, _canReadData) as IIcController; IcCardReaderLogger.Debug("创建IC读卡器实例成功!"); //添加到数据字典 loadedAssmbly.Add(driverPath, assembly); } catch (Exception ex) { IcCardReaderLogger.Error("创建IC读卡器实例失败!原因:" + ex.Message); throw ex; } return(io);// }
/// <summary> /// 停止读取IC卡 /// </summary> public bool Stop() { IcCardReaderLogger.Debug("停止IC读卡器寻卡!_searchCard=" + _searchCard.ToString()); _searchCard = false; return(true); }