/// <summary> /// 关闭读取器连接 /// </summary> /// <returns></returns> public ReturnMessage CloseReaderConnection() { ReturnMessage returnMsg = new ReturnMessage(); if (!m_ReaderInitiatedDisconnectionReceived) { try { //读取相关回调删除 m_RFIDReader.Events.ReadNotify -= Events_ReadNotify; //m_RFIDReader.Events.StatusNotify -= Events_StatusNotify; m_RFIDReader.Disconnect(); returnMsg.CallStatus = true; returnMsg.CallMessage = "Close Connection Success."; //日志记录 DeclaimerReaderLog.Info("[" + returnMsg.CallMessage + "] ==== [" + DateTime.Now.ToString() + "]"); } catch (OperationFailureException ofe) { returnMsg.CallStatus = false; returnMsg.CallMessage = "Close Connection Error : " + ofe.VendorMessage; //日志记录 DeclaimerReaderLog.Error("[" + returnMsg.CallMessage + "] ==== [" + DateTime.Now.ToString() + "]"); } } return(returnMsg); }
/// <summary> /// 初始化读取器 /// </summary> /// <param name="strIP"></param> /// <param name="strPort"></param> /// <param name="connTimeOut"></param> /// <param name="readerPowerList"></param> /// <param name="readerAntennasList"></param> /// <returns></returns> public ReturnMessage InitReader(string strIP, string strPort, string connTimeOut, List <string> readerPowerList, List <string> readerAntennasList) { ReturnMessage returnMsg = new ReturnMessage(); //初始化 Fixed Reader日志记录对象 DeclaimerReaderLog.InstanceLogger(LoggerClassType.DeclaimerWaitingGodotRollingFileAppender); //默认值设置 if (string.IsNullOrEmpty(strPort)) { strPort = "5084"; } if (string.IsNullOrEmpty(connTimeOut)) { connTimeOut = "0"; } if (readerPowerList == null) { readerPowerList = new List <string>() { "3000", "3000", "3000", "3000" }; } if (readerAntennasList == null) { readerAntennasList = new List <string>() { "1", "2", "3", "4" }; } this.ReaderIP = strIP; this.ReaderPort = strPort; this.ConnectTimeOut = connTimeOut; this.ReaderPowerList = readerPowerList; this.ReaderAntennasList = readerAntennasList; m_TagTable = new Hashtable(1023); //默认全开 AvailableAntennas ushort[] antList = new ushort[] { 1, 2, 3, 4 }; m_AntennaList = new Symbol.RFID3.AntennaInfo(antList); //创建读取器对象 m_RFIDReader = new RFIDReader(this.ReaderIP, uint.Parse(this.ReaderPort), uint.Parse(this.ConnectTimeOut)); //设置允许连接 IsAllowConnect = true; returnMsg.CallMessage = "Reader Init Success."; returnMsg.CallStatus = true; //日志记录 DeclaimerReaderLog.Info("[Reader Init Success.] ==== [" + DateTime.Now.ToString() + "]"); return(returnMsg); }
/// <summary> /// 设置功率 /// </summary> /// <param name="powerList"></param> /// <returns></returns> private ReturnMessage SetReaderPower(List <string> powerList) { ReturnMessage resultMsg = new ReturnMessage(); for (int i = 0; i < powerList.Count; i++) { try { if (m_RFIDReader.IsConnected) { //antID[0]天线号码 0对应1天线, 1对应2天线 ushort[] antID = m_RFIDReader.Config.Antennas.AvailableAntennas; Antennas.Config antConfig = m_RFIDReader.Config.Antennas[antID[i]].GetConfig(); //默认为0 antConfig.ReceiveSensitivityIndex = (ushort)0; int index = Convert.ToInt32((Convert.ToInt32(powerList[i]) - 1000) / 10); //功率选项 【索引最小值是0对应1000,索引最大值是200对应3000】 换算功率=(功率-2000)/10 antConfig.TransmitPowerIndex = (ushort)index; if (!m_RFIDReader.ReaderCapabilities.IsHoppingEnabled) { antConfig.TransmitFrequencyIndex = (ushort)1;//默认1 } else { antConfig.TransmitFrequencyIndex = (ushort)1;//默认1 } //antID[0]天线号码 0对应1天线, 1对应2天线 m_RFIDReader.Config.Antennas[antID[i]].SetConfig(antConfig); resultMsg.CallStatus = true; resultMsg.CallMessage = "设置天线功率成功!"; //"Set Antenna Configuration Successfully"; } else { resultMsg.CallStatus = false; resultMsg.CallMessage = "设置功率失败!请连接读取器后再设置功率。"; //日志记录 DeclaimerReaderLog.Info("[" + resultMsg.CallMessage + "] ==== [" + DateTime.Now.ToString() + "]"); //"Please connect to a reader"; } } catch (InvalidUsageException iue) { resultMsg.CallStatus = false; resultMsg.CallMessage = "InvalidUsageException" + iue.VendorMessage; //日志记录 DeclaimerReaderLog.Error("[" + resultMsg.CallMessage + "] ==== [" + DateTime.Now.ToString() + "]"); } catch (OperationFailureException ofe) { resultMsg.CallStatus = false; resultMsg.CallMessage = "OperationFailureException :" + ofe.StatusDescription; //日志记录 DeclaimerReaderLog.Error("[" + resultMsg.CallMessage + "] ==== [" + DateTime.Now.ToString() + "]"); } catch (Exception ex) { resultMsg.CallStatus = false; resultMsg.CallMessage = "OperationFailureException :" + ex.Message; //日志记录 DeclaimerReaderLog.Error("[" + resultMsg.CallMessage + "] ==== [" + DateTime.Now.ToString() + "]"); } } return(resultMsg); }
/// <summary> /// 打开读取器连接 /// </summary> /// <returns></returns> public ReturnMessage OpenReaderConnection() { ReturnMessage returnMsg = new ReturnMessage(); if (m_RFIDReader == null) { returnMsg.CallStatus = false; returnMsg.CallMessage = "读取器开启失败 : 读取器对象为空."; //日志记录 DeclaimerReaderLog.Error("[读取器开启失败 : 读取器对象为空.] ==== [" + DateTime.Now.ToString() + "]"); } try { if (!m_RFIDReader.IsConnected) { //连接操作 m_RFIDReader.Connect(); //允许开始读取 IsAllowStartRead = true; //设置功率 returnMsg = SetReaderPower(this.ReaderPowerList); //日志记录 DeclaimerReaderLog.Info("[" + returnMsg.CallMessage + "] ==== [" + DateTime.Now.ToString() + "]"); SetReaderAntennas(this.ReaderAntennasList); } m_IsReading = false; if (m_RFIDReader != null) { m_RFIDReader.Actions.TagAccess.OperationSequence.DeleteAll(); TagAccess.Sequence.Operation op = new TagAccess.Sequence.Operation(); op.AccessOperationCode = ACCESS_OPERATION_CODE.ACCESS_OPERATION_READ; op.ReadAccessParams.MemoryBank = (MEMORY_BANK)3; // 3 user区 op.ReadAccessParams.ByteCount = 0; op.ReadAccessParams.ByteOffset = 0; op.ReadAccessParams.AccessPassword = 0; m_RFIDReader.Actions.TagAccess.OperationSequence.Add(op); } try { TagStorageSettings tagStorageSettings = m_RFIDReader.Config.GetTagStorageSettings(); tagStorageSettings.DiscardTagsOnInventoryStop = true; m_RFIDReader.Config.SetTagStorageSettings(tagStorageSettings); /* * Events Registration */ m_RFIDReader.Events.AttachTagDataWithReadEvent = false; //读取回调 m_RFIDReader.Events.ReadNotify += new Events.ReadNotifyHandler(Events_ReadNotify); //状态回调 //m_RFIDReader.Events.StatusNotify += new Events.StatusNotifyHandler(Events_StatusNotify); m_RFIDReader.Events.NotifyBufferFullWarningEvent = true; m_RFIDReader.Events.NotifyBufferFullEvent = true; m_RFIDReader.Events.NotifyReaderDisconnectEvent = true; m_RFIDReader.Events.NotifyAccessStartEvent = true; m_RFIDReader.Events.NotifyAccessStopEvent = true; m_RFIDReader.Events.NotifyInventoryStartEvent = true; m_RFIDReader.Events.NotifyInventoryStopEvent = true; m_RFIDReader.Events.NotifyReaderExceptionEvent = true; m_RFIDReader.Events.NotifyHandheldTriggerEvent = false; } catch (OperationFailureException ofe) { returnMsg.CallStatus = false; returnMsg.CallMessage = "Connect Configuration : " + ofe.VendorMessage; //日志记录 DeclaimerReaderLog.Error("[" + returnMsg.CallMessage + "] ==== [" + DateTime.Now.ToString() + "]"); } } catch (OperationFailureException ofe) { returnMsg.CallStatus = false; returnMsg.CallMessage = "Connect Error : " + ofe.StatusDescription; //日志记录 DeclaimerReaderLog.Error("[" + returnMsg.CallMessage + "] ==== [" + DateTime.Now.ToString() + "]"); } return(returnMsg); }