using GRreader; using HighWayIot.Common; using HighWayIot.Log4net; using HighWayIot.Rfid; using HighWayIot.Rfid.Dto; using HighWayIot.Rfid.Impl; using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Threading; namespace MaterialTraceability.Rfid.Impl { [ClassInterface(ClassInterfaceType.None)] public class RFly_I160Adapter : IDeviceAdapter { private LogHelper log = LogHelper.Instance; private StringChange stringChange = StringChange.Instance; private JsonChange jsonChange = JsonChange.Instance; #region 全局变量声明 List m_TagInfoList = new List(); CommType gConnetType = new CommType(); private DeviceType m_iDeviceType = DeviceType.RFly_I160; private DeviceType DeviceType; ICommunicateService m_ICommunicateService = null; public UHFreader MyReader = new UHFreader(); public event RecvIdentifyData RecvIdentifyDataEvent = null; public UInt16 m_iDeviceId = 0; private string m_strIp; //读写器IP或串口号 private int m_iPort; //读写器端口号或波特率 private readonly string m_ReadDbm = "10"; private readonly string m_WriteDbm = "10"; private readonly string m_AnalysisFlag = "1"; //1:按照次数最多返回条码;2:按照平均功率最大返回条码 private int m_ConnectFlag = 0; //private Mutex mut = new Mutex(); private ManualResetEvent BeginEvent = new ManualResetEvent(true); public bool TagInventory_Lable = true;//连续盘点标签标志 private Semaphore m_GlobalSem = new Semaphore(1, 1); private bool m_GetHeartSuccessful = false; private Semaphore m_GetHeartSem = new Semaphore(0, 100000); private Semaphore m_MulEpcSem = new Semaphore(0, 100000); private Semaphore m_StopSem = new Semaphore(0, 100000); private bool m_OneEpcSemSuccessful = false; private Semaphore m_OneEpcSem = new Semaphore(0, 100000); private bool m_ReadDataSuccessful = false; private bool m_Device_ReadSuccessful = false; private byte m_ReadDataLen = 0; private byte[] m_ReadData = null; private Semaphore m_ReadSem = new Semaphore(0, 100000); private Semaphore m_WriteSem = new Semaphore(0, 100000); private int m_BarcodeGroupCount = 0; private byte[] m_MulAllData = null; private byte[] m_AutoReadEPC = null; private int m_readEPCDataLen = 0; private byte m_OneEpcDataLen = 0; private byte m_OneEpcAntenna = 254; private byte[] m_OneEpcData = null; private bool m_GetReadNoDataSuccessful; private bool writeResult = false; #endregion public int AutoReport { get { return AutoReport; } set { AutoReport = value; } } public int Filter { get { return Filter; } set { Filter = value; } } #region 设备连接部分 public bool Device_Init(CommType iCommType, string pUrl, DeviceType iDeviceType) { try { m_iDeviceType = iDeviceType; //LogService.Instance.Debug("函数调用:Device_Init Start: "); //if (iDeviceType == DeviceType.Mesnac_GRUV100) { if (iCommType == CommType.RJ45) //网口 { if (m_ICommunicateService == null) { m_ICommunicateService = new BgTcpClient(); } string[] split = pUrl.Split(new Char[] { ':' }); m_strIp = split[0]; string strTemp = split[1]; m_iPort = Convert.ToInt32(strTemp); m_ICommunicateService.Init(m_strIp, m_iPort, this); log.RfidLog("设备初始化成功,IP:" + m_strIp + "端口号:" + m_iPort); } else //串口,代用串口号和波特率 { if (m_ICommunicateService == null) { return false; } string[] split = pUrl.Split(new Char[] { ':' }); m_strIp = split[0]; string strTemp = split[1]; m_iPort = Convert.ToInt32(strTemp); m_ICommunicateService.Init(m_strIp, m_iPort, this); log.RfidLog("设备初始化成功,串口号:" + m_strIp + "波特率:" + m_iPort); } } } catch (Exception ex) { log.Error("连接读写器异常:",ex); return false; } return true; } public bool Device_Init_Id(CommType iCommType, string pUrl, ushort iDeviceId) { m_iDeviceId = iDeviceId; Device_Init(iCommType, pUrl, (DeviceType)1); return true; } public bool Device_Connect() { try { if (m_ICommunicateService != null) { if (m_ICommunicateService.Connect()) { log.RfidLog("Device_Connect:连接成功"); return true; } else { log.RfidLog("Device_Connect:连接失败"); return false; } } else { return false; } } catch (Exception ex) { log.Error("Device_Connect异常:",ex); return false; } } public void Device_Destroy() { m_ICommunicateService.DisConnect(); } public bool Device_GetState() { return m_ICommunicateService.GetState(); } public bool Device_ReConnect() { log.RfidLog("Device_Connect 调用重连函数!"); return Device_Connect(); } #endregion public bool Device_DealValidPack(byte[] ValidData) { byte bAntana = 0; //读写器天线号 默认0x04 UInt16 iReadCount = 0; //标签读取次数 UInt16 iRSSI = 0; //标签信号强度 byte[] bResultEPC_Data = new byte[14]; byte[] bNoData = new byte[12]; try { byte info = ValidData[3]; switch (info) //心跳 { case 0X90: m_GetHeartSuccessful = true; Console.WriteLine("Device_DealValidPack处理函数:" + stringChange.bytesToHexStr(ValidData, ValidData.Length)); try { log.SemaphoreLog("信号量m_GetHeartSem释放"); m_GetHeartSem.Release(); } catch (Exception ex) { log.Error("心跳信号量处理异常", ex); log.SemaphoreLog("信号量m_GetHeartSem释放异常:" + ex.Message); } break; case 0x02: if (ValidData[2] == 0x00 || ValidData[2] == 0x01) { log.RfidLog("----函数调用:Device_DealValidPack:NoRead!"); m_BarcodeGroupCount = 0; m_GetReadNoDataSuccessful = true; m_OneEpcDataLen = 0; } else { log.RfidLog("----函数调用:Device_DealValidPack 有数据!"); m_BarcodeGroupCount = Convert.ToInt32(ValidData[5].ToString()); //标签组数TagCount m_MulAllData = new byte[ValidData.Length]; Array.Clear(m_MulAllData, 0, ValidData.Length); Array.Copy(ValidData, 0, m_MulAllData, 0, ValidData.Length); Array.Copy(m_MulAllData, 11, bResultEPC_Data, 0, 14); m_ReadDataLen = 14; m_OneEpcDataLen = 14; m_OneEpcData = new byte[m_OneEpcDataLen]; bAntana = ValidData[8]; m_OneEpcAntenna = ValidData[8]; iReadCount = ValidData[6]; iRSSI = ValidData[7]; //DeviceType 改为 m_iDeviceType,原代码为DeviceType,因测试一体机没有赋值改为m_iDeviceType //四通道读写器业务逻辑 if (DeviceType == DeviceType.Mesnac_GRUR445) { m_TagInfoList = Device_DealTagInfoList2(m_MulAllData); } else { m_TagInfoList = Device_AutoDealContent(m_MulAllData); } //m_OneEpcSem.Release(); //m_ReadSem.Release(); //log.RfidLog("----有数据,释放信号量!"); } try { log.SemaphoreLog("信号量m_OneEpcSem,释放"); m_OneEpcSem.Release(); //log.SemaphoreLog("信号量m_GlobalSem,释放"); //m_GlobalSem.Release(); } catch(Exception e) { log.Error("释放信号量错误!"+e.ToString()); log.SemaphoreLog("释放信号量错误:"+e.Message); } break; default: log.RfidLog("Device_DealValidPack处理函数:" + stringChange.bytesToHexStr(ValidData, ValidData.Length)); Console.WriteLine("Device_DealValidPack处理函数:" + stringChange.bytesToHexStr(ValidData, ValidData.Length)); break; } } catch (Exception ex) { log.Error("空间名:" + ex.Source + ";" + '\n' + "方法名:" + ex.TargetSite + '\n' + "故障点:" + ex.StackTrace.Substring(ex.StackTrace.LastIndexOf("\\") + 1, ex.StackTrace.Length - ex.StackTrace.LastIndexOf("\\") - 1) + '\n' + "错误提示:",ex); return false; } return true; } public byte Device_SendHeartPack() { byte iResult = 0; try { log.RfidLog("函数调用:Device_SendHeartPack"); //m_GlobalSem.WaitOne(-1, false); MessagePack pMessagePack = new MessagePack(); pMessagePack.m_pData = new byte[9]; Array.Clear(pMessagePack.m_pData, 0, 1);//清空为0 //获取温度 pMessagePack.m_pData[0] = 0xAA; pMessagePack.m_pData[1] = 0x55; pMessagePack.m_pData[2] = 0x00; pMessagePack.m_pData[3] = 0x90; pMessagePack.m_pData[4] = 0x90; pMessagePack.m_pData[5] = 0x0D; m_GetHeartSem.WaitOne(1, false); log.SemaphoreLog("信号量m_GetHeartSem,WaitOne(1, false)"); if (m_ICommunicateService != null) { if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 { log.SemaphoreLog("信号量m_GetHeartSem,WaitOne(9000, false)"); if (m_GetHeartSem.WaitOne(5000, false)) //等待结果,并取结果返回。 { if (m_GetHeartSuccessful) { //log.RfidLog("发送心跳正常。"); iResult = 1; //通讯连接和读写器都正常 } } else //超时 { log.SemaphoreLog("信号量m_GetHeartSem,发送心跳超时"); log.RfidLog("发送心跳超时。"); iResult = 2; //通讯连接器正常,读写器故障 }; } else { //通讯连接器失败或网络故障 log.RfidLog("发送心跳报文失败,通讯故障。"); iResult = 3; } } else { //m_GetHeartSem.Release(); return 3; } } catch (Exception ex) { log.RfidLog("Device_SendHeartPack:"+ex.Message); iResult = 3; } finally { //m_GlobalSem.Release(); //m_GetHeartSem.Release(); } return iResult; } public ushort Device_GetReportData(ref byte[] pReadData, Byte Antenna, UInt32 Timedout) { byte[] pTemp = null; byte iReadLen = 0; if ((iReadLen = Device_GetOneIdentifyData(ref pTemp, Antenna, (UInt16)Timedout)) > 0) { // log.RfidLog("Device_GetReportData获取自报数据" + "数据长度" + iReadLen); pReadData = new byte[iReadLen + 1]; pReadData[0] = iReadLen; Array.Copy(pTemp, 0, pReadData, 1, iReadLen); return (ushort)(iReadLen + 1); } else { return 0; } } /// /// 根据天线号识别单个标签EPC数据,只返回读到的第一条数据 /// /// 识别的标签EPC长度,0为识别失败 /// 识别到的数据缓存区 /// 天线号,0为本机,255为所有天线 /// 超时时间,单位毫秒,识别到立即返回,未识别到等待超时返回 public Byte Device_GetOneIdentifyData(ref Byte[] pReadData, Byte Antenna, UInt16 Timedout) { byte[] u16byte = new byte[2]; byte iResult = 0; byte[] bCRC = new byte[4]; try { m_GlobalSem.WaitOne(1, false); //log.RfidLog("函数调用:Device_GetOneIdentifyData"); if (Antenna == 0) //此版本4为主机 { Antenna = (byte)(Antenna + 1); } MessagePack pMessagePack = new MessagePack(); //A5 5A 00 0A 80 00 64 EE 0D 0A //100毫秒的示例 pMessagePack.m_pData = new byte[8]; pMessagePack.m_pData[0] = 0xAA; pMessagePack.m_pData[1] = 0x55; pMessagePack.m_pData[2] = 0x02; pMessagePack.m_pData[3] = 0x02; //pMessagePack.m_pData[4] = 0x03; //pMessagePack.m_pData[5] = 0xE8; u16byte = BitConverter.GetBytes(Timedout); //超时时间 u16byte = stringChange.Swap16Bytes(u16byte); //协议里为大端在前 //log.RfidLog("u16byte:" + u16byte); Array.Copy(u16byte, 0, pMessagePack.m_pData, 4, 2); Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); pMessagePack.m_pData[6] = stringChange.CalculateVerify(bCRC, bCRC.Length); pMessagePack.m_pData[7] = 0x0D; //log.RfidLog("pMessagePack.m_pData:" + pMessagePack.m_pData); //int i = m_OneEpcSem.Release(1); m_OneEpcSem.WaitOne(1, false); m_OneEpcDataLen = 0; if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 { m_ReadDataSuccessful = true; if (m_OneEpcSem.WaitOne(Timedout + 1000, false)) //等待结果,并取结果返回。 { if ((m_OneEpcDataLen > 0 && m_OneEpcAntenna == Antenna)) //有数据,正常 { pReadData = new byte[14]; Array.Copy(m_AutoReadEPC, 0, pReadData, 0, 14); log.RfidLog("Device_GetOneIdentifyData:" + stringChange.bytesToHexStr(pReadData, pReadData.Length)); iResult = m_OneEpcDataLen; m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; m_ReadDataSuccessful = false; } else { log.RfidLog("Device_GetOneIdentifyData长度或者天线号不正确"); m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; iResult = 0; m_ReadDataSuccessful = false; } } else //超时 { iResult = 0; m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; m_ReadDataSuccessful = false; log.RfidLog("Device_GetOneIdentifyData超时"); } } else { m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; log.RfidLog("Device_GetOneIdentifyData发送识别单条EPC命令失败:"); iResult = 0; m_ReadDataSuccessful = false; } } catch (Exception ex) { log.Error("Device_GetOneIdentifyData识别单条EPC数据异常:",ex); iResult = 0; m_ReadDataSuccessful = false; } finally { //m_GlobalSem.Release(); } return iResult; } public UInt16 Device_Read(G2MemBank filterMembank, UInt16 filterWordPtr, UInt16 filterWordCnt, Byte[] filterData, G2MemBank Membank, UInt16 WordPtr, UInt16 WordCnt, ref Byte[] pReadData, byte Antenna) { byte[] u16byte = new byte[2]; byte iResult = 0; try { m_GlobalSem.WaitOne(1, false); log.RfidLog("函数调用:Device_Read"); MessagePack pMessagePack = new MessagePack(); pMessagePack.m_pData = new byte[8]; Array.Clear(pMessagePack.m_pData, 0, pMessagePack.m_pData.Length);//清空为0 pMessagePack.m_pData[0] = 0xAA; pMessagePack.m_pData[1] = 0x55; pMessagePack.m_pData[2] = 0x02; pMessagePack.m_pData[3] = 0x02; pMessagePack.m_pData[4] = 0x03; pMessagePack.m_pData[5] = 0xE8; pMessagePack.m_pData[6] = 0xEB; pMessagePack.m_pData[7] = 0x0D; //m_ReadSem.Release(1); m_ReadSem.WaitOne(1, false); m_ReadDataLen = 0; if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 { m_ReadDataSuccessful = true; if (m_ReadSem.WaitOne(2000, false)) //等待结果,并取结果返回。 { if (m_ReadDataLen > 0) //有数据,正常 { pReadData = new byte[m_ReadDataLen]; Array.Copy(m_OneEpcData, 0, pReadData, 0, m_ReadDataLen); iResult = m_ReadDataLen; log.RfidLog("Device_Read:" + stringChange.bytesToHexStr(pReadData, pReadData.Length)); m_ReadDataLen = 0; } else { m_ReadDataLen = 0; log.RfidLog("Device_Read失败,返回长度为0"); iResult = 0; } } else //超时 { log.RfidLog("Device_Read失败,超时未返回"); iResult = 0; } } else { log.RfidLog("发送读取命令失败"); iResult = 0; } } catch (Exception ex) { log.Error("读取数据异常:",ex); iResult = 0; } finally { //m_GlobalSem.Release(); } return iResult; } public UInt16 Device_Write(G2MemBank filterMembank, UInt16 filterWordPtr, UInt16 filterWordCnt, Byte[] filterData, G2MemBank Membank, UInt16 WordPtr, UInt16 WordCnt, Byte[] pWriteData, byte Antenna) { byte[] u16byte = new byte[2]; byte iResult = 0; try { m_GlobalSem.WaitOne(1, false); log.RfidLog("函数调用:Device_Write"); //int datalen = 36+ pWriteData.Length; int datalen = 48; MessagePack pMessagePack = new MessagePack(); pMessagePack.m_pData = new byte[datalen]; Array.Clear(pMessagePack.m_pData, 0, pMessagePack.m_pData.Length);//清空为0 pMessagePack.m_pData[0] = 0xAA; pMessagePack.m_pData[1] = 0x55; pMessagePack.m_pData[2] = (byte)(datalen - 6);//先占位 pMessagePack.m_pData[3] = 0x03; pMessagePack.m_pData[4] = 0x00; pMessagePack.m_pData[5] = 0x64; pMessagePack.m_pData[6] = 0x00; pMessagePack.m_pData[7] = 0x00; pMessagePack.m_pData[8] = 0x00; pMessagePack.m_pData[9] = 0x00; pMessagePack.m_pData[10] = 0x01; pMessagePack.m_pData[11] = 0x00; pMessagePack.m_pData[12] = 0x00; pMessagePack.m_pData[13] = 0x00; pMessagePack.m_pData[14] = 0x20; pMessagePack.m_pData[15] = 0x60; Array.Copy(filterData, 0, pMessagePack.m_pData, 16, filterData.Length); pMessagePack.m_pData[28] = 0x03; pMessagePack.m_pData[29] = 0x00; pMessagePack.m_pData[30] = 0x00; pMessagePack.m_pData[31] = 0x00; pMessagePack.m_pData[32] = 0x00; pMessagePack.m_pData[33] = (byte)WordCnt; Array.Copy(pWriteData, 0, pMessagePack.m_pData, 34, pWriteData.Length); byte[] bCRC = new byte[datalen - 4]; //Array.Copy(u16byte, 0, pMessagePack.m_pData, 4, 2); Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, bCRC.Length); pMessagePack.m_pData[datalen - 2] = stringChange.CalculateVerify(bCRC, bCRC.Length); pMessagePack.m_pData[datalen - 1] = 0x0D; m_WriteSem.WaitOne(1, false); m_ReadDataLen = 0; if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 { m_ReadDataSuccessful = true; if (m_WriteSem.WaitOne(2000, false)) //等待结果,并取结果返回。 { //log.RfidLog("Device_Write成功"); if (writeResult) { iResult = 1; } } else //超时 { log.RfidLog("Device_Write失败,超时未返回"); iResult = 0; } } else { log.RfidLog("发送写入命令失败"); iResult = 0; } } catch (Exception ex) { log.Error("写入数据异常:",ex); iResult = 0; } finally { //m_GlobalSem.Release(); } return iResult; } /// /// 返回读写器获取的条码中最好的一条 1:按照读取次数最多返回条码;2:按照功率最大返回条码 /// /// 要处理的数据 /// 1:按照读取次数最多返回条码;2:按照平均功率最大返回条码 /// 1:打印log;0:不打印Log /// public byte[] CommandAnalysisBarcode(List cBarcodeObjList, int iFlag, int iPrintLogFlag) { byte[] tempBarcode = null; if (iFlag == 1) //按次数最多获取 { if (iPrintLogFlag == 1) { // log.RfidLog("----调用通用函数:CommandAnalysisBarcode Flag=1 按照最大次数选择条码,如果次数相同 ,按照平均功率最大的选择条码:"); } #region 按照最大次数选择条码,如果次数相同 ,按照平均功率最大的选择条码 #region 求出最大次数 int iOrderMaxCount = cBarcodeObjList[0].Count; for (int i = 0; i < cBarcodeObjList.Count; i++) { if (iPrintLogFlag == 1) { #region 打印所有条码 次数和平均功率 log.RfidLog("条码:[ " + stringChange.bytesToHexStr(cBarcodeObjList[i].EPC, cBarcodeObjList[i].EPC.Length) + " ] 次数:[ " + cBarcodeObjList[i].Count.ToString() + " ] 平均最大功率: [" + cBarcodeObjList[i].RSSI.ToString() + " ]"); #endregion } if (cBarcodeObjList[i].Count > iOrderMaxCount) { iOrderMaxCount = cBarcodeObjList[i].Count; } } if (iPrintLogFlag == 1) { //LogService.Instance.Debug("按照次数优先,最大次数为:" + iOrderMaxCount.ToString()); } #endregion #region 求出等于最大次数的所有条码对象 List cBarcodeObjListCount = new List(); foreach (TagInfo itemMax in cBarcodeObjList) { if (itemMax.Count == iOrderMaxCount) { cBarcodeObjListCount.Add(itemMax); //LogService.Instance.Debug("按照次数优先,等于最大次数条码为:" + stringChange.bytesToHexStr(itemMax.EPC, itemMax.EPC.Length)); } } if (iPrintLogFlag == 1) { //LogService.Instance.Debug("按照次数优先,等于最大次数条码数量为:" + cBarcodeObjListCount.Count.ToString()); } #endregion #region 求出最优条码 if (cBarcodeObjListCount.Count == 1) //如果只有一条 { foreach (TagInfo itemCount in cBarcodeObjListCount) { if (itemCount.Count == iOrderMaxCount) { if (iPrintLogFlag == 1) { //log.RfidLog("---- 取出的最优条码是: " + stringChange.bytesToHexStr(itemCount.EPC, itemCount.EPC.Length)); } tempBarcode = new byte[itemCount.EPC.Length]; Array.Copy(itemCount.EPC, 0, tempBarcode, 0, itemCount.EPC.Length); } } //Add By baogq 2019年5月24日 15:29:20 //tempBarcode = new byte[cBarcodeObjListCount[0].EPC.Length]; //tempBarcode = cBarcodeObjListCount[0].EPC; //Array.Copy(cBarcodeObjListCount[0].EPC, 0, tempBarcode, 0, cBarcodeObjListCount[0].EPC.Length); cBarcodeObjListCount.Clear(); return tempBarcode; } else //如果有多条 { #region 求出最大次数相同下条码的强度最强的条码 #region 求出最大次数相同的条码中,强度的最大值 float iOrderAvgMaxPower = cBarcodeObjListCount[0].RSSI; for (int i = 0; i < cBarcodeObjListCount.Count; i++) { if (cBarcodeObjListCount[i].RSSI > iOrderAvgMaxPower) { iOrderAvgMaxPower = cBarcodeObjListCount[i].RSSI; } } if (iPrintLogFlag == 1) { //LogService.Instance.Debug("按照次数优先,等于最大次数条码中平均功率最大值为:" + iOrderAvgMaxPower.ToString()); } #endregion foreach (TagInfo itemAvg in cBarcodeObjListCount) { if (itemAvg.RSSI == iOrderAvgMaxPower) { if (iPrintLogFlag == 1) { //log.RfidLog("---- 取出的最优条码是: " + stringChange.bytesToHexStr(itemAvg.EPC, itemAvg.EPC.Length)); } tempBarcode = new byte[itemAvg.EPC.Length]; Array.Copy(itemAvg.EPC, 0, tempBarcode, 0, itemAvg.EPC.Length); } } #endregion } #endregion #endregion return tempBarcode; } else if (iFlag == 2)//按照功率最大获取 { if (iPrintLogFlag == 1) { //log.RfidLog("----调用通用函数:CommandAnalysisBarcode Flag=2 按照最大平均功率取出条码,如果最大平均功率相同,则按照次数最大取值最优条码:"); } #region 按照最大平均功率取出条码,如果最大功率相同,则按照次数最大取值最优条码 #region 求出最大功率 int iMaxAvgPow = cBarcodeObjList[0].RSSI; for (int i = 0; i < cBarcodeObjList.Count; i++) { if (iPrintLogFlag == 1) { #region 打印所有条码 次数和平均功率 log.RfidLog("条码:[ " + stringChange.bytesToHexStr(cBarcodeObjList[i].EPC, cBarcodeObjList[i].EPC.Length) + " ] 平均最大功率: [" + cBarcodeObjList[i].RSSI.ToString() + " ] 次数:[ " + cBarcodeObjList[i].Count.ToString() + " ] "); #endregion } if (cBarcodeObjList[i].RSSI > iMaxAvgPow) { iMaxAvgPow = cBarcodeObjList[i].RSSI; } } if (iPrintLogFlag == 1) { //LogService.Instance.Debug("按照平均功率最大,最大平均功率为:" + iMaxAvgPow.ToString()); } #endregion #region 求出等于最大功率的所有条码对象 List cBarcodeObjListCount = new List(); foreach (TagInfo itemMax in cBarcodeObjList) { if (itemMax.RSSI == iMaxAvgPow) { cBarcodeObjListCount.Add(itemMax); } } if (iPrintLogFlag == 1) { //LogService.Instance.Debug("按照平均功率最大,最大平均功率相同的条码数量为:" + cBarcodeObjListCount.Count.ToString()); } #endregion #region 求出最优条码 //如果只有一条 if (cBarcodeObjListCount.Count == 1) { if (iPrintLogFlag == 1) { // log.RfidLog("---- 取出的最优条码是: " + stringChange.bytesToHexStr(cBarcodeObjList[0].EPC, cBarcodeObjList[0].EPC.Length)); } tempBarcode = new byte[cBarcodeObjListCount[0].EPC.Length]; Array.Copy(cBarcodeObjListCount[0].EPC, 0, tempBarcode, 0, cBarcodeObjListCount[0].EPC.Length); } else //如果有多条 { #region 求出平均功率最大相同时,条码读到次数最多的一个条码 #region 求出平均功率最大相同条码中,最大的次数 int iReadMaxCount = cBarcodeObjListCount[0].Count; for (int i = 0; i < cBarcodeObjListCount.Count; i++) { if (cBarcodeObjListCount[i].Count > iReadMaxCount) { iReadMaxCount = cBarcodeObjListCount[i].Count; } } if (iPrintLogFlag == 1) { //LogService.Instance.Debug("在最大平均功率相同的条码中,读取次数最多为:" + iReadMaxCount.ToString()); } #endregion foreach (TagInfo itemReadCount in cBarcodeObjListCount) { if (itemReadCount.Count == iReadMaxCount) { if (iPrintLogFlag == 1) { // log.RfidLog("---- 取出的最优条码是: " + stringChange.bytesToHexStr(itemReadCount.EPC, itemReadCount.EPC.Length)); } tempBarcode = new byte[itemReadCount.EPC.Length]; Array.Copy(itemReadCount.EPC, 0, tempBarcode, 0, itemReadCount.EPC.Length); } } #endregion } #endregion #endregion return tempBarcode; } else { return tempBarcode; } } #region 处理自动上传的函数 private Mutex mutauto = new Mutex(); /// /// 四通道读写器标签逻辑处理 /// /// /// public List Device_DealTagInfoList(byte[] AutoDealReportData) { List tagInfoList = new List(); byte[] bResultEPC_Data = new byte[14]; m_AutoReadEPC = null; m_readEPCDataLen = 0; //LogService.Instance.Debug("----函数调用:Device_AutoDealContent 开始!"); try { mutauto.WaitOne(); int iFirstCountPos = 6; //第一次读取标签次数位置 int iFirstRSSIPos = 7; //第一次读取标签强度位置 int iFirstAnt = 8; int iFirstPC = 9; //第一次读取标签天线位置 int iFirstLeftBarcketPos = 11;//EPC数据起始位置 UInt16 tempDataCount = 0; int tempDataRSSI = 0; UInt16 tempDataANT = 0; int iBarcodeGroupCount = Convert.ToInt32(AutoDealReportData[5].ToString()); //标签组数 int iBarcodeLength = 16; //标签长度 int iCommonSecondFlag = 0; for (int j = 0; j < iBarcodeGroupCount; j++) { TagInfo tag = new TagInfo(); byte[] tempDataByte = new byte[iBarcodeLength]; Array.Clear(tempDataByte, 0, iBarcodeLength); Array.Copy(AutoDealReportData, iFirstLeftBarcketPos, tempDataByte, 0, iBarcodeLength); byte[] tempCountByte = new byte[1]; //取出标签次数 Array.Clear(tempCountByte, 0, 1); Array.Copy(AutoDealReportData, iFirstCountPos, tempCountByte, 0, 1); tempDataCount = tempCountByte[0]; byte[] tempRSSIByte = new byte[1]; //取出标签强度 Array.Clear(tempRSSIByte, 0, 1); Array.Copy(AutoDealReportData, iFirstRSSIPos, tempRSSIByte, 0, 1); //tempDataRSSI = tempRSSIByte[0]; tempDataRSSI = stringChange.HexStringToNegative(stringChange.bytesToHexStr(tempRSSIByte, 1)); byte[] tempPCByte = new byte[2]; //取出PC Array.Clear(tempPCByte, 0, 2); Array.Copy(AutoDealReportData, iFirstPC, tempPCByte, 0, 2); //tempPCByte = tempPCByte[0]; #region add by wenjy 20220829 取出天线号 byte[] tempAntByte = new byte[1]; //取出天线号 Array.Clear(tempAntByte, 0, 1); Array.Copy(AutoDealReportData, iFirstAnt, tempAntByte, 0, 1); tempDataANT = tempAntByte[0]; #endregion tag.Count = tempDataCount; tag.RSSI = tempDataRSSI; tag.EPC = tempDataByte; tag.EPCstring = System.Text.Encoding.ASCII.GetString(tempDataByte); //tag.EPCstring = stringChange.bytesToHexStr(tempDataByte, tempDataByte.Length); tag.PC = tempPCByte; tag.Antana = tempDataANT; tagInfoList.Add(tag); int iBarcodeListLen = tagInfoList.Count; //特别注意,必须这样,要不然会多一条数据 //int iFirstCountPos = 6; //第一次读取标签次数位置 //int iFirstRSSIPos = 7; //第一次读取标签强度位置 //int iFirstAnt = 8; //第一次读取标签天线位置 //int iFirstLeftBarcketPos = 11;//EPC数据起始位置 iFirstCountPos = iFirstCountPos + 21; //次数 iFirstRSSIPos = iFirstCountPos + 1; //强度 iFirstAnt = iFirstRSSIPos + 1; //天线 iFirstPC = iFirstAnt + 1; iFirstLeftBarcketPos = iFirstLeftBarcketPos + 21; log.RfidLog("----函数调用:Device_AutoDealContent 第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); iCommonSecondFlag++; if (iCommonSecondFlag == iBarcodeGroupCount) { mutauto.ReleaseMutex(); log.RfidLog("《《《返回标签数据!"); return tagInfoList; } } } catch (Exception ex) { log.RfidLog("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString()); mutauto.ReleaseMutex(); } return tagInfoList; } public List Device_DealTagInfoList2(byte[] AutoDealReportData) { List tagInfoList = new List(); byte[] bResultEPC_Data = new byte[14]; m_AutoReadEPC = null; m_readEPCDataLen = 0; try { mutauto.WaitOne(); int iFirstCountPos = 6; //第一次读取标签次数位置 int iFirstRSSIPos = 7; //第一次读取标签强度位置 int iFirstAnt = 8; int iFirstPC = 9; //第一次读取标签天线位置 int iFirstLeftBarcketPos = 11;//EPC数据起始位置 UInt16 tempDataCount = 0; int tempDataRSSI = 0; UInt16 tempDataANT = 0; int iBarcodeGroupCount = Convert.ToInt32(AutoDealReportData[5].ToString()); //标签组数 int iBarcodeLength = 16; //标签长度 int iCommonSecondFlag = 0; for (int j = 0; j < iBarcodeGroupCount; j++) { TagInfo tag = new TagInfo(); byte[] tempPCByte = new byte[2]; //取出PC Array.Clear(tempPCByte, 0, 2); Array.Copy(AutoDealReportData, iFirstPC, tempPCByte, 0, 2); int pc = Convert.ToInt32(tempPCByte[0].ToString("X")); int epcLength = EPCLengthByPC(pc); iBarcodeLength = epcLength; byte[] tempDataByte = new byte[epcLength]; Array.Clear(tempDataByte, 0, iBarcodeLength); Array.Copy(AutoDealReportData, iFirstLeftBarcketPos, tempDataByte, 0, iBarcodeLength); byte[] tempCountByte = new byte[1]; //取出标签次数 Array.Clear(tempCountByte, 0, 1); Array.Copy(AutoDealReportData, iFirstCountPos, tempCountByte, 0, 1); tempDataCount = tempCountByte[0]; byte[] tempRSSIByte = new byte[1]; //取出标签强度 Array.Clear(tempRSSIByte, 0, 1); Array.Copy(AutoDealReportData, iFirstRSSIPos, tempRSSIByte, 0, 1); tempDataRSSI = stringChange.HexStringToNegative(stringChange.bytesToHexStr(tempRSSIByte, 1)); #region add by wenjy 20220829 取出天线号 byte[] tempAntByte = new byte[1]; //取出天线号 Array.Clear(tempAntByte, 0, 1); Array.Copy(AutoDealReportData, iFirstAnt, tempAntByte, 0, 1); tempDataANT = tempAntByte[0]; #endregion tag.Count = tempDataCount; tag.RSSI = tempDataRSSI; tag.EPC = tempDataByte; if (pc == 24) { tag.EPCstring = stringChange.bytesToHexStr(tempDataByte, tempDataByte.Length).Substring(0, 7); } else { tag.EPCstring = System.Text.Encoding.ASCII.GetString(tempDataByte); } tag.PC = tempPCByte; tag.Antana = tempDataANT; tagInfoList.Add(tag); int iBarcodeListLen = tagInfoList.Count; //特别注意,必须这样,要不然会多一条数据 iFirstCountPos = iFirstCountPos + iBarcodeLength + 5; //次数 iFirstRSSIPos = iFirstCountPos + 1; //强度 iFirstAnt = iFirstRSSIPos + 1; //天线 iFirstPC = iFirstAnt + 1; iFirstLeftBarcketPos = iFirstLeftBarcketPos + iBarcodeLength + 5; log.RfidLog("----函数调用:Device_DealTagInfoList2 第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); iCommonSecondFlag++; if (iCommonSecondFlag == iBarcodeGroupCount) { mutauto.ReleaseMutex(); log.RfidLog("《《《返回标签数据!"); return tagInfoList; } } } catch (Exception ex) { log.RfidLog("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString()); mutauto.ReleaseMutex(); } return tagInfoList; } private int EPCLengthByPC(int pcValue) { int epcLength = 0; if (pcValue >= 10 && pcValue < 20) { epcLength = 4; } else if (pcValue >= 20 && pcValue < 30) { epcLength = 8; } else if (pcValue >= 30 && pcValue < 40) { epcLength = 12; } else if (pcValue >= 40 && pcValue < 50) { epcLength = 16; } else if (pcValue >= 50 && pcValue < 60) { epcLength = 20; } else if (pcValue >= 60 && pcValue < 70) { epcLength = 24; } else if (pcValue >= 70 && pcValue < 80) { epcLength = 28; } else if (pcValue >= 80 && pcValue < 90) { epcLength = 30; } return epcLength; } /// /// 一体机读写器标签逻辑处理 /// /// /// public List Device_AutoDealContent(byte[] AutoDealReportData) { List tagInfoList = new List(); byte[] bResultEPC_Data = new byte[12]; m_AutoReadEPC = null; m_readEPCDataLen = 0; try { mutauto.WaitOne(); int iFirstCountPos = 6; //第一次读取标签次数位置 int iFirstRSSIPos = 7; //第一次读取标签强度位置 int iFirstAnt = 8; //第一次读取标签天线位置 int iFirstLeftBarcketPos = 11;//EPC数据起始位置 UInt16 tempDataCount = 0; UInt16 tempDataRSSI = 0; UInt16 tempDataANT = 0; int iBarcodeGroupCount = Convert.ToInt32(AutoDealReportData[5].ToString()); //标签组数 int iBarcodeLength = 16; //标签长度 for (int j = 0; j < iBarcodeGroupCount; j++) { TagInfo tag = new TagInfo(); byte[] tempDataByte = new byte[iBarcodeLength]; Array.Clear(tempDataByte, 0, iBarcodeLength); Array.Copy(AutoDealReportData, iFirstLeftBarcketPos, tempDataByte, 0, iBarcodeLength); byte[] tempCountByte = new byte[1]; //取出标签次数 Array.Clear(tempCountByte, 0, 1); Array.Copy(AutoDealReportData, iFirstCountPos, tempCountByte, 0, 1); tempDataCount = tempCountByte[0]; byte[] tempRSSIByte = new byte[1]; //取出标签强度 Array.Clear(tempRSSIByte, 0, 1); Array.Copy(AutoDealReportData, iFirstRSSIPos, tempRSSIByte, 0, 1); tempDataRSSI = tempRSSIByte[0]; byte[] tempAntByte = new byte[1]; //取出天线号 Array.Clear(tempAntByte, 0, 1); Array.Copy(AutoDealReportData, iFirstAnt, tempAntByte, 0, 1); tempDataANT = tempAntByte[0]; tag.Count = tempDataCount; tag.RSSI = tempDataRSSI; tag.EPC = tempDataByte; tag.EPCstring = System.Text.Encoding.ASCII.GetString(tempDataByte);; tag.Antana = tempDataANT; tagInfoList.Add(tag); int iBarcodeListLen = tagInfoList.Count; //特别注意,必须这样,要不然会多一条数据 iFirstCountPos = iFirstCountPos + 21; iFirstRSSIPos = iFirstCountPos + 1; iFirstAnt = iFirstRSSIPos + 1; iFirstLeftBarcketPos = iFirstLeftBarcketPos + 21; } } catch (Exception ex) { log.RfidLog("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString()); mutauto.ReleaseMutex(); } return tagInfoList; } #endregion #region 该函数不实现了 public bool Device_SetRf(int iDbi, byte Antenna, WriteOrRead RorW) { bool bResult = false; try { bool Set_OK; if (RorW == WriteOrRead.Read) { Set_OK = MyReader.SetPower(iDbi, Convert.ToSingle(m_WriteDbm)); if (Set_OK) //设置功率工程 { log.RfidLog("设置天线读功率成功!"); bResult = true; } else { log.Error("设置天线读功率失败;"); bResult = false; } } else { Set_OK = MyReader.SetPower(Convert.ToSingle(m_ReadDbm), iDbi); if (Set_OK) //设置功率工程 { log.RfidLog("设置天线写功率成功!"); bResult = true; } else { log.Error("设置天线写功率失败;"); bResult = false; } } } catch (Exception ex) { log.Error("Device_SetRf异常:",ex); bResult = false; } return bResult; } public string Device_GetOneIdentifyData(Byte Antenna, UInt16 Timedout) { byte[] u16byte = new byte[2]; byte[] bCRC = new byte[4]; string strResult = ""; try { m_GlobalSem.WaitOne(1, false); //log.RfidLog("函数调用:Device_GetOneIdentifyData"); if (Antenna == 0) //此版本1为主机 { Antenna = (byte)(Antenna + 1); } MessagePack pMessagePack = new MessagePack(); pMessagePack.m_pData = new byte[8]; pMessagePack.m_pData[0] = 0xAA; pMessagePack.m_pData[1] = 0x55; pMessagePack.m_pData[2] = 0x02;//change by yinzf pMessagePack.m_pData[3] = 0x02; //pMessagePack.m_pData[4] = 0x00; //pMessagePack.m_pData[5] = 0x64; //pMessagePack.m_pData[6] = 0x67; u16byte = BitConverter.GetBytes(Timedout); //超时时间 u16byte = stringChange.Swap16Bytes(u16byte); //协议里为大端在前 //log.RfidLog("u16byte:" + u16byte); Array.Copy(u16byte, 0, pMessagePack.m_pData, 4, 2); Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); pMessagePack.m_pData[6] = stringChange.CalculateVerify(bCRC, bCRC.Length); pMessagePack.m_pData[7] = 0x0D; //m_OneEpcSem.Release(1); m_OneEpcSem.WaitOne(1, false); m_OneEpcDataLen = 0; if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 { if (m_OneEpcSem.WaitOne(Timedout + 500, false)) //等待结果,并取结果返回。 { if (m_OneEpcDataLen >= 1) //有数据,正常 { strResult = Encoding.ASCII.GetString(m_OneEpcData); //Encoding.ASCII.GetBytes(strResult); //pReadData = new byte[m_OneEpcDataLen]; //Array.Copy(m_OneEpcData, 0, pReadData, 0, m_OneEpcDataLen); log.RfidLog("Device_GetOneIdentifyData:" + stringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen)); m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; } else { strResult = stringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen); log.RfidLog("Device_GetOneIdentifyData:" + stringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen)); m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; strResult = ""; } } else //超时 { m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; log.RfidLog("Device_GetOneIdentifyData超时未返回"); strResult = ""; } } else { m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; log.RfidLog("Device_GetOneIdentifyData发送识别单条EPC命令超时"); strResult = ""; } } catch (Exception ex) { log.Error("Device_GetOneIdentifyData识别单条EPC数据异常:",ex); strResult = ""; } finally { //m_GlobalSem.Release(); } return strResult; } public string Device_GetOneIdentifyData_Finish(byte Antenna, ushort Timedout) { return ""; } public Byte Device_GetOneIdentifyData_Finish(ref byte[] pReadData, byte Antenna, ushort Timedout) { return 0; } public bool Device_BeginIdentify() { byte[] u16byte = new byte[2]; bool bResult = false; try { log.RfidLog("函数调用:Device_BeginIdentify"); MessagePack pMessagePack = new MessagePack(); pMessagePack.m_pData = new byte[1]; if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 { if (BeginEvent.WaitOne(200, false)) //等待结果,并取结果返回。 { bResult = true; } else //超时 { bResult = false; } } else { log.RfidLog("发送开始连续识别命令失败:"); bResult = false; } } catch (Exception ex) { log.Error("发送开始连续识别命令异常", ex); bResult = false; } return bResult; } public bool Device_StopIdentify() { bool bResult = false; try { log.RfidLog("函数调用:Device_StopIdentify"); MessagePack pMessagePack = new MessagePack(); pMessagePack.m_pData = new byte[1]; if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 { if (m_StopSem.WaitOne(200, false)) //等待结果,并取结果返回。 { bResult = true; } else //超时 { bResult = false; } } else { log.RfidLog("发送停止连续识别命令失败:"); bResult = false; } } catch (Exception ex) { log.Error("发送停止连续识别命令异常:",ex); bResult = false; } return bResult; } public ushort Device_GetIdentifyData(ref byte[] pReadData, byte Antenna) { return 0; } public List Device_GetTagInfoList(DeviceType devicetype,int waitTime) { log.Info("进入Device_GetTagInfoList函数,参数deviceType:" + jsonChange.ModeToJson(devicetype) + ";waitTime:" + waitTime); byte[] u16byte = new byte[2]; byte iResult = 0; byte[] bCRC = new byte[4]; try { //log.SemaphoreLog("信号量m_GlobalSem,WaitOne(1, false)"); //m_GlobalSem.WaitOne(1, false); MessagePack pMessagePack = new MessagePack(); //A5 5A 00 0A 80 00 64 EE 0D 0A //100毫秒的示例 pMessagePack.m_pData = new byte[8]; pMessagePack.m_pData[0] = 0xAA; pMessagePack.m_pData[1] = 0x55; pMessagePack.m_pData[2] = 0x02; pMessagePack.m_pData[3] = 0x02; //pMessagePack.m_pData[4] = 0x03; //pMessagePack.m_pData[5] = 0xE8; if (waitTime == 0) { waitTime = 1000; } u16byte = BitConverter.GetBytes(waitTime); //超时时间 u16byte = stringChange.Swap16Bytes(u16byte); //协议里为大端在前 //log.RfidLog("u16byte:" + u16byte); Array.Copy(u16byte, 0, pMessagePack.m_pData, 4, 2); Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); pMessagePack.m_pData[6] = stringChange.CalculateVerify(bCRC, bCRC.Length); pMessagePack.m_pData[7] = 0x0D; //m_OneEpcSem.WaitOne(1, false); GetAllRelese(m_OneEpcSem); log.SemaphoreLog("信号量m_OneEpcSem,WaitOne(1, false)"); m_OneEpcDataLen = 0; DeviceType = devicetype; if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 { m_ReadDataSuccessful = true; log.SemaphoreLog("信号量m_OneEpcSem,WaitOne("+ waitTime + ", false)"); if (m_OneEpcSem.WaitOne(waitTime+2000, false)) //等待结果,并取结果返回。 { if (m_OneEpcDataLen > 0) //有数据,正常 { m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; m_ReadDataSuccessful = false; log.Info("成功返回数据!"); return m_TagInfoList; } else { log.Info("Device_GetTagInfoLista长度或者天线号不正确"); m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; iResult = 0; m_TagInfoList.Clear(); m_ReadDataSuccessful = false; } } else //超时 { iResult = 0; m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; m_ReadDataSuccessful = false; m_TagInfoList.Clear(); log.Info("Device_GetTagInfoList超时"); log.SemaphoreLog("信号量m_OneEpcSem返回,Device_GetTagInfoList超时"); } } else { m_OneEpcDataLen = 0; m_OneEpcAntenna = 254; log.Info("Device_GetTagInfoList发送识别单条EPC命令失败:"); iResult = 0; m_TagInfoList.Clear(); m_ReadDataSuccessful = false; } } catch (Exception ex) { log.Info("Device_GetTagInfoList识别单条EPC数据异常:" + ex.Message); log.Error("Device_GetTagInfoList识别单条EPC数据异常:", ex); iResult = 0; m_TagInfoList.Clear(); m_ReadDataSuccessful = false; } finally { // m_GlobalSem.Release(); } return m_TagInfoList; } #endregion private void GetAllRelese(Semaphore sph) { bool res = sph.WaitOne(1, false); if (res) { log.RfidLog("信号量手动释放"); GetAllRelese(sph); } } } }