/// <summary> /// 关闭注销设备 /// </summary> /// <returns></returns> public bool Close() { bool rtn = false; if (_isDeviceOpen) { Stop(); try { if (_curRfidCfg.ConType == DeviceConType.Net) { ReaderB.StaticClassReaderB.CloseNetPort(_readerHandel); } else//串口 { ReaderB.StaticClassReaderB.CloseSpecComPort(comPort); ReaderB.StaticClassReaderB.CloseComPort(); } rtn = true; RFIDReaderLogger.Debug("关闭读卡器。", _curRfidCfg.Ip); } catch (Exception ex) { RFIDReaderLogger.Error("关闭设备异常。", _curRfidCfg.Ip, ex); } } else { RFIDReaderLogger.Error("关闭设备时,设备未打开。", _curRfidCfg.Ip); } return(rtn); }
/// <summary> /// 连接初始化设备 /// </summary> /// <returns></returns> public bool Open() { bool rtn = false; //FileHelpClass.WriteLog("Ljyzn102准备打开RFID设备:" + _curRfidCfg.IsUse, "RFID"); if (_curRfidCfg != null && _curRfidCfg.IsUse) { //FileHelpClass.WriteLog(string.Format("Ljyzn102读写器连接方式:{0}", _curRfidCfg.ConType), "RFID"); if (!OpenReader(_curRfidCfg)) { //FileHelpClass.WriteLog("Ljyzn102启动RFID设备失败。", "RFID"); ShowErrorMsg("打开RFID设备失败。"); } else { _isDeviceOpen = true; rtn = true; RFIDReaderLogger.Debug("打开RFID设备成功。", _curRfidCfg.Ip); } } else { RFIDReaderLogger.Error("打开RFID设备失败,配置信息不存在或读写器未启用。", _curRfidCfg.Ip); rtn = false; } return(rtn); }
/// <summary> /// 读取标签数据 /// </summary> private void ReadData() { while (_searchCard) { List <string> cardIds = ReadCardId(); if (cardIds.Count > 0 && onReceivedData != null) { RFIDReaderLogger.Debug("读取到卡片数据,并触发onReceivedData事件。cardIds=" + string.Join(",", cardIds.ToArray()), _curRfidCfg.Ip); onReceivedData(cardIds); } Thread.Sleep(_curRfidCfg.Interval); } }
/// <summary> /// 启动定时读取功能 /// </summary> /// <param name="tIp"></param> /// <param name="tPort"></param> /// <returns></returns> public bool Start() { bool rtn = false; if (!_isDeviceOpen) { Open(); } if (_isDeviceOpen) { try { if (_isDeviceOpen) { if (tsRfidReader != null) { while (tsRfidReader.IsAlive) { tsRfidReader.Abort(); RFIDReaderLogger.Debug("启动寻卡时先强制结束寻卡线程。", _curRfidCfg.Ip); } } // tsRfidReader = new Thread(new ThreadStart(ReadData)); tsRfidReader.IsBackground = true; _searchCard = true; tsRfidReader.Start(); RFIDReaderLogger.Debug("启动寻卡线程。", _curRfidCfg.Ip); rtn = true; } else { RFIDReaderLogger.Debug("启动寻卡时设备未打开。", _curRfidCfg.Ip); } } catch (Exception ex) { RFIDReaderLogger.Error("启动寻卡时异常。", _curRfidCfg.Ip, ex); } } return(rtn); }
/// <summary> /// 配置文件名 /// </summary> /// <param name="pConfigFile"></param> /// <param name="isMulti"></param> public RfidController(string pConfigFile) { RFIDReaderLogger.Debug("-----------------------------开始-------------------------------", ""); //读取配置 ConfigReader cfgReader = new ConfigReader(pConfigFile); if (curCfgList == null) { curCfgList = ConfigReader.ReadListRfidConfig(); RFIDReaderLogger.Debug("读取配置文件。pConfigFile=" + pConfigFile, ""); } rfidList = new List <IRfidController>(); for (int i = 0; i < curCfgList.Count; i++) { IRfidController rfid = CreateInstance(curCfgList[i]); rfid.OnShowErrorMsg += rfid_OnShowErrorMsg; rfid.onReceivedData += ReceivedDataMethod; rfidList.Add(rfid); } }
/// <summary> /// 创建RFID实例 /// </summary> /// <returns></returns> private IRfidController CreateInstance(RfidCfg rf) { try { string driverPath = System.AppDomain.CurrentDomain.BaseDirectory; driverPath = Path.Combine(driverPath, rf.EquDll); Assembly assembly = Assembly.LoadFile(driverPath); RFIDReaderLogger.Debug("载入硬件封装模块。driverPath=" + driverPath, ""); string name = assembly.FullName.Split(',')[0] + ".RfidController"; Type type = assembly.GetType(name); IRfidController irfid = Activator.CreateInstance(type, rf) as IRfidController; return(irfid); } catch (Exception ex) { RFIDReaderLogger.Error("CreateInstance异常。", "", ex); return(null); } }
public RfidController(RfidCfg pRfidCfg) { _curRfidCfg = pRfidCfg; RFIDReaderLogger.Debug(string.Format("实例化RFID读写器。IP={0},端口={1}", pRfidCfg.Ip, pRfidCfg.Port), pRfidCfg.Ip); }
/// <summary> /// 关闭设备 /// </summary> /// <returns></returns> public bool Stop() { RFIDReaderLogger.Debug("停止寻卡。_searchCard=" + _searchCard.ToString(), _curRfidCfg.Ip); _searchCard = false; return(true); }