using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading;
using System.Configuration;
using System.Collections;

namespace Mesnac.DeviceAdapter.RFly_I160
{
    [ClassInterface(ClassInterfaceType.None)]
    public class RFly_I160Adapter : IDeviceAdapter
    {
        public log4net.ILog LogInfo = log4net.LogManager.GetLogger("RollingLogFileAppender"); //Logging 名字要在 App.config 中能找到

        #region 全局变量声明
        private DeviceType m_iDeviceType = DeviceType.RFly_I160;
        ICommunicateService m_ICommunicateService = null;
        public event RecvIdentifyData RecvIdentifyDataEvent = null;
        public event RecvIdentifyData RecvIdentifyData_ToMES_Event;

        public UInt16 m_iDeviceId = 0;
        private string m_strIp;   //读写器IP或串口号
        private int m_iPort;      //读写器端口号或波特率
        private readonly string m_ReadDbm = ConfigurationManager.AppSettings["ReadDbm"];
        private readonly string m_WriteDbm = ConfigurationManager.AppSettings["WriteDbm"];
        private readonly string m_AnalysisFlag = ConfigurationManager.AppSettings["AnalysisFlag"]; //1：按照次数最多返回条码；2：按照平均功率最大返回条码
        //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_WriteAlarmSemSuccessful = false;
        private Semaphore m_WriteAlarmSem = 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 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;

        List<TagInfo> tagInfos = null;
        #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;
                // LogInfo.Info("函数调用: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, m_iDeviceId);
                        // LogInfo.Info("设备初始化成功，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, m_iDeviceId);
                         LogInfo.Info("设备初始化成功，串口号：" + m_strIp + "波特率:" + m_iPort);
                    }
                }
            }
            catch (Exception ex)
            {
                LogInfo.Fatal("IP：" + m_strIp + "端口号:" + m_iPort + "连接读写器异常：" + ex.Message);
                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())
                    {
                        // LogInfo.Info("Device_Connect:连接成功");
                        return true;
                    }
                    else
                    {
                        //LogInfo.Info("设备初始化成功，IP：" + m_strIp + "端口号:" + m_iPort);
                        LogInfo.Fatal("IP：" + m_strIp + "端口号: " + m_iPort + ",Device_Connect:连接失败");
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("Device_Connect异常：" + ex.Message);
                return false;
            }
        }

        public void Device_Destroy()
        {
            m_ICommunicateService.DisConnect();
        }

        public bool Device_GetState()
        {
            return m_ICommunicateService.GetState();
        }

        public bool Device_ReConnect()
        {
            LogInfo.Fatal("IP：" + m_strIp + "端口号: " + m_iPort + ",Device_ReConnect:调用重连函数");
            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[12];
            byte[] bNoData = new byte[12];
            // LogInfo.Info("----函数调用:Device_DealValidPack 开始!");
            try
            {
                switch (ValidData[3])       //心跳
                {
                    case 0X00:
                        if (ValidData[2] == 0X01 && ValidData[3] == 0X00)  //成功
                        {
                            m_GetHeartSuccessful = true;
                        }
                        else
                        {
                            m_GetHeartSuccessful = false;
                        }
                        m_GetHeartSem.Release();
                        break;
                    //GPIO返回
                    case 0X51:
                        m_WriteAlarmSemSuccessful = true;
                        m_WriteAlarmSem.Release();
                        //this.RecvIdentifyDataEvent(1, null, bAntana, m_iDeviceId, m_strIp, "1");
                        break;
                }
                if (ValidData[0] == 0XBB && ValidData[1] == 0XDD && ValidData[2] == 0X00 && ValidData[3] == 0x02) //超时 02 4E 6F 52 65 61 64 03
                {
                    // LogInfo.Info("----函数调用:Device_DealValidPack：NoRead!");
                    m_BarcodeGroupCount = 0;
                    m_GetReadNoDataSuccessful = true;

                    //m_MulEpcSem.Release();
                    if (m_GetReadNoDataSuccessful && m_ReadDataSuccessful == false)
                    {
                        bAntana = 4;
                        m_AutoReadEPC = null;
                        m_GetReadNoDataSuccessful = false;
                        this.RecvIdentifyDataEvent(0, null, bAntana, m_iDeviceId, m_strIp,"1");
                    }
                    else
                    {
                        m_OneEpcDataLen = 0;
                        m_OneEpcSem.Release();
                    }
                }
                if (ValidData[0] == 0XBB && ValidData[1] == 0XDD && ValidData[2] != 0X00 && ValidData[3] != 0XBF) //正常有数据
                {

                    // LogInfo.Info("----函数调用:Device_DealValidPack 有数据!");

                    m_BarcodeGroupCount = Convert.ToInt32(ValidData[5].ToString("X2"));         //标签组数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, 12);



                    bAntana = ValidData[8];
                    m_OneEpcAntenna = ValidData[8];
                    iReadCount = ValidData[6];
                    iRSSI = ValidData[7];

                    //Device_AutoDealContent(m_MulAllData);
                    tagInfos = GetTagInfos(m_MulAllData);
                    m_ReadDataLen = (byte)tagInfos.Count;
                    m_OneEpcDataLen = (byte)tagInfos.Count;
                    //m_OneEpcData = new byte[m_OneEpcDataLen];
                    if (RecvIdentifyDataEvent != null)
                    {
                        if (m_ReadDataSuccessful == false)
                        {
                  
                            //this.RecvIdentifyDataEvent.Invoke(Convert.ToUInt16(Convert.ToUInt16(m_readEPCDataLen)), tagInfos, bAntana, m_iDeviceId, m_strIp,"1");
                            this.RecvIdentifyDataEvent.Invoke(Convert.ToUInt16(Convert.ToUInt16(tagInfos.Count)), tagInfos, bAntana, m_iDeviceId, m_strIp,"1");
                        }
                        else
                        {
                            m_OneEpcSem.Release();
                            m_ReadSem.Release();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("Device_DealValidPack异常：" + ex.Message);
                return false;
            }
            return true;
        }

        public byte Device_SendHeartPack()
        {
            byte iResult = 0;
            try
            {
                // LogInfo.Info("函数调用: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;
                //pMessagePack.m_pData[0] = 0xAA;
                //pMessagePack.m_pData[1] = 0x55;
                //pMessagePack.m_pData[2] = 0x03;
                //pMessagePack.m_pData[3] = 0xBF;
                //pMessagePack.m_pData[4] = 0x01;
                //pMessagePack.m_pData[5] = 0x0A;
                //pMessagePack.m_pData[6] = 0x01;
                //pMessagePack.m_pData[7] = 0xB6;
                //pMessagePack.m_pData[8] = 0x0D;
                m_GetHeartSem.WaitOne(1, false);
                if (m_ICommunicateService != null)
                {
                    if (m_ICommunicateService.SendMessage(pMessagePack))   //发送报文成功
                    {
                        if (m_GetHeartSem.WaitOne(2000, false))   //等待结果,并取结果返回。
                        {
                            // LogInfo.Info("发送心跳报文正常。");
                            iResult = 1;        //通讯连接和读写器都正常
                        }
                        else                    //超时
                        {
                            //LogInfo.Info("发送心跳报文超时。");
                            iResult = 2;       //通讯连接器正常，读写器故障
                        };
                    }
                    else
                    {
                        //通讯连接器失败或网络故障
                         //LogInfo.Info("发送心跳报文失败，通讯故障。");
                        iResult = 3;
                    }
                }
                else
                {
                    return 3;
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("Device_SendHeartPack：" + ex.Message);
                iResult = 3;
            }
            finally
            {
                m_GlobalSem.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)) > 0)
            {
                 LogInfo.Info("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;
            }
        }

        /// <summary>
        /// 根据天线号识别单个标签EPC数据，只返回读到的第一条数据
        /// </summary>
        /// <returns>识别的标签EPC长度，0为识别失败</returns>
        /// <param name="pReadData">识别到的数据缓存区</param>
        /// <param name="Antenna">天线号，0为本机，255为所有天线</param>
        /// <param name="Timedout">超时时间，单位毫秒，识别到立即返回，未识别到等待超时返回</param>
        public Byte Device_GetOneIdentifyData(ref Byte[] pReadData, Byte Antenna, UInt16 Timedout, int ReadCounts)
        {
            byte[] u16byte = new byte[2];
            byte iResult = 0;
            byte[] bCRC = new byte[4];
            string s_CompareData1 = "";
            try
            {
                m_GlobalSem.WaitOne(-1, false);
                // LogInfo.Info("函数调用:Device_GetOneIdentifyData");
                if (Antenna == 0)  //此版本4为主机
                {
                    Antenna = (byte)(Antenna + 4);
                }

                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);  //协议里为大端在前
                // LogInfo.Info("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;
                // LogInfo.Info("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 + 500, false))   //等待结果,并取结果返回。
                    {
                        if (m_OneEpcDataLen > 0) //有数据，正常
                        {
                            pReadData = new byte[m_OneEpcDataLen];
                            Array.Copy(m_AutoReadEPC, 0, pReadData, 0, m_OneEpcDataLen);
                            // LogInfo.Info("Device_GetOneIdentifyData:" + StringChange.bytesToHexStr(pReadData, pReadData.Length));
                            iResult = m_OneEpcDataLen;
                            m_OneEpcDataLen = 0;
                            m_OneEpcAntenna = 254;
                            m_ReadDataSuccessful = false;
                            //开始对比过滤数据
                            //s_CompareData1 = Encoding.ASCII.GetString(pReadData, 0, FilterData.Length);

                            //if (s_CompareData1 == FilterData)
                            //{
                            //    iResult = m_OneEpcDataLen;
                            //    m_OneEpcDataLen = 0;
                            //    m_OneEpcAntenna = 254;
                            //    m_ReadDataSuccessful = false;
                            //}
                            //else
                            //{
                            //     LogInfo.Info("Device_GetOneIdentifyData对比数据不匹配");
                            //    iResult = 0;
                            //    m_OneEpcDataLen = 0;
                            //    m_OneEpcAntenna = 254;
                            //    m_ReadDataSuccessful = false;
                            //}
                        }
                        else
                        {
                            //LogInfo.Info("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;
                        //LogInfo.Info("Device_GetOneIdentifyData超时");
                    }
                }
                else
                {
                    m_OneEpcDataLen = 0;
                    m_OneEpcAntenna = 254;
                     LogInfo.Info("Device_GetOneIdentifyData发送识别单条EPC命令失败：");
                    iResult = 0;
                    m_ReadDataSuccessful = false;
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("Device_GetOneIdentifyData识别单条EPC数据异常：" + ex.Message);
                iResult = 0;
                m_ReadDataSuccessful = false;
            }
            finally
            {
                m_GlobalSem.Release();
            }
            return iResult;
        }

        public List<TagInfo> Device_GetAllIdentifyData(Byte Antenna, UInt16 Timedout, int ReadCounts)
        {
            List<TagInfo> tagInfo = new List<TagInfo>();
            byte[] u16byte = new byte[2];
            byte iResult = 0;
            byte[] bCRC = new byte[4];
            string s_CompareData1 = "";
            try
            {
                m_GlobalSem.WaitOne(-1, false);
                // LogInfo.Info("函数调用:Device_GetOneIdentifyData");
                if (Antenna == 0)  //此版本4为主机
                {
                    Antenna = (byte)(Antenna + 4);
                }

                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);  //协议里为大端在前
                // LogInfo.Info("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;
                // LogInfo.Info("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 + 500, false))   //等待结果,并取结果返回。
                    {
                        if (m_OneEpcDataLen > 0) //有数据，正常
                        {
                            tagInfo = tagInfos;
                            //Array.Copy(m_AutoReadEPC, 0, pReadData, 0, m_OneEpcDataLen);
                            // LogInfo.Info("Device_GetOneIdentifyData:" + StringChange.bytesToHexStr(pReadData, pReadData.Length));
                            iResult = m_OneEpcDataLen;
                            m_OneEpcDataLen = 0;
                            m_OneEpcAntenna = 254;
                            m_ReadDataSuccessful = false;
                            return tagInfo;
                        }
                        else
                        {
                            //LogInfo.Info("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;
                        LogInfo.Info("Device_GetOneIdentifyData超时");
                    }
                }
                else
                {
                    m_OneEpcDataLen = 0;
                    m_OneEpcAntenna = 254;
                    LogInfo.Info("Device_GetOneIdentifyData发送识别单条EPC命令失败：");
                    iResult = 0;
                    m_ReadDataSuccessful = false;
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("Device_GetOneIdentifyData识别单条EPC数据异常：" + ex.Message);
                iResult = 0;
                m_ReadDataSuccessful = false;
            }
            finally
            {
                m_GlobalSem.Release();
            }
            return tagInfo;
        }



        public UInt16 Device_Read(G2MemBank filterMembank, UInt16 filterWordPtr, UInt16 filterWordCnt, Byte[] filterData,
                                G2MemBank Membank, UInt16 WordPtr, UInt16 WordCnt, ref Byte[] pReadData, byte Antenna, int ReadCounts)
        {
            byte[] u16byte = new byte[2];
            byte iResult = 0;
            try
            {
                m_GlobalSem.WaitOne(-1, false);
                 LogInfo.Info("函数调用: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;
                             LogInfo.Info("Device_Read:" + StringChange.bytesToHexStr(pReadData, pReadData.Length));

                            m_ReadDataLen = 0;
                        }
                        else
                        {
                            m_ReadDataLen = 0;
                             LogInfo.Info("Device_Read失败,返回长度为0");
                            iResult = 0;
                        }
                    }
                    else   //超时
                    {
                         LogInfo.Info("Device_Read失败,超时未返回");
                        iResult = 0;
                    }
                }
                else
                {
                     LogInfo.Info("发送读取命令失败");
                    iResult = 0;
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("读取数据异常：" + ex.Message);
                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)
        {
            return 1;
        }
        public bool Device_WriteAlarmLight(int port, UInt16 Timedout)
        {
            bool iflag = false; 
            byte[] u16byte = new byte[2];
            byte iResult = 0;
            byte[] bCRC = new byte[6];
            string s_CompareData1 = "";
            try
            {
                m_GlobalSem.WaitOne(-1, false);
                // LogInfo.Info("函数调用:Device_GetOneIdentifyData");

                MessagePack pMessagePack = new MessagePack();

                //AA 55 04 51 02 00 00 00 57 0D   //100毫秒的示例
                pMessagePack.m_pData = new byte[10];
                pMessagePack.m_pData[0] = 0xAA;
                pMessagePack.m_pData[1] = 0x55;
                pMessagePack.m_pData[2] = 0x04;
                pMessagePack.m_pData[3] = 0x51;
                pMessagePack.m_pData[4] = (byte)port;
                pMessagePack.m_pData[5] = 0x00;
                //pMessagePack.m_pData[6] = 0x00;
                //pMessagePack.m_pData[7] = 0x00;
                u16byte = BitConverter.GetBytes(Timedout);  //超时时间
                u16byte = StringChange.Swap16Bytes(u16byte);  //协议里为大端在前
                // LogInfo.Info("u16byte：" + u16byte);
                Array.Copy(u16byte, 0, pMessagePack.m_pData, 6, 2);
                Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 6);
                pMessagePack.m_pData[8] = StringChange.CalculateVerify(bCRC, bCRC.Length);
                pMessagePack.m_pData[9] = 0x0D;
                m_WriteAlarmSem.WaitOne(1, false);
                if (m_ICommunicateService.SendMessage(pMessagePack))   //发送报文成功
                {
                    if (m_WriteAlarmSem.WaitOne(500, false))   //等待结果,并取结果返回。
                    {
                        if (m_WriteAlarmSemSuccessful)
                        {
                            iflag = true;
                        }
                    }
                    else   //超时
                    {
                         LogInfo.Info("超时");
                    }
                }
                else
                {
                     LogInfo.Info("发送识别命令失败：");
                    m_WriteAlarmSemSuccessful = false;
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("识别数据异常：" + ex.Message);
                m_WriteAlarmSemSuccessful = false;
            }
            finally
            {
                m_GlobalSem.Release();
            }
            return iflag;
        }
        /// <summary>
        /// 返回读写器获取的条码中最好的一条 1:按照读取次数最多返回条码；2：按照功率最大返回条码
        /// </summary>
        /// <param name="cBarcodeObjList">要处理的数据</param>
        /// <param name="iFlag">1:按照读取次数最多返回条码；2：按照平均功率最大返回条码</param>
        ///  <param name="iPrintLogFlag">1:打印log；0：不打印Log</param>
        /// <returns></returns>
        public byte[] CommandAnalysisBarcode(List<TagInfo> cBarcodeObjList, int iFlag, int iPrintLogFlag)
        {
            byte[] tempBarcode = null;
            if (iFlag == 1)         //按次数最多获取
            {
                if (iPrintLogFlag == 1)
                {
                     LogInfo.Info("----调用通用函数:CommandAnalysisBarcode Flag=1 按照最大次数选择条码，如果次数相同 ，按照平均功率最大的选择条码:");
                }
                #region 按照最大次数选择条码，如果次数相同 ，按照平均功率最大的选择条码
                #region 求出最大次数
                int iOrderMaxCount = cBarcodeObjList[0].Count;
                for (int i = 0; i < cBarcodeObjList.Count; i++)
                {
                    if (iPrintLogFlag == 1)
                    {
                        #region 打印所有条码 次数和平均功率
                         LogInfo.Info("条码:[ " + 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)
                {
                     LogInfo.Info("按照次数优先，最大次数为：" + iOrderMaxCount.ToString());
                }
                #endregion
                #region 求出等于最大次数的所有条码对象
                List<TagInfo> cBarcodeObjListCount = new List<TagInfo>();
                foreach (TagInfo itemMax in cBarcodeObjList)
                {
                    if (itemMax.Count == iOrderMaxCount)
                    {
                        cBarcodeObjListCount.Add(itemMax);
                         LogInfo.Info("按照次数优先，等于最大次数条码为：" + StringChange.bytesToHexStr(itemMax.EPC, itemMax.EPC.Length));
                    }
                }
                if (iPrintLogFlag == 1)
                {
                     LogInfo.Info("按照次数优先，等于最大次数条码数量为：" + cBarcodeObjListCount.Count.ToString());
                }
                #endregion
                #region 求出最优条码

                if (cBarcodeObjListCount.Count == 1)        //如果只有一条
                {

                    foreach (TagInfo itemCount in cBarcodeObjListCount)
                    {
                        if (itemCount.Count == iOrderMaxCount)
                        {
                            if (iPrintLogFlag == 1)
                            {
                                 LogInfo.Info("---- 取出的最优条码是: " + 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)
                    {
                         LogInfo.Info("按照次数优先，等于最大次数条码中平均功率最大值为：" + iOrderAvgMaxPower.ToString());
                    }

                    #endregion 

                    foreach (TagInfo itemAvg in cBarcodeObjListCount)
                    {
                        if (itemAvg.RSSI == iOrderAvgMaxPower)
                        {
                            if (iPrintLogFlag == 1)
                            {
                                 LogInfo.Info("---- 取出的最优条码是: " + 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)
                {
                     LogInfo.Info("----调用通用函数:CommandAnalysisBarcode Flag=2  按照最大平均功率取出条码，如果最大平均功率相同，则按照次数最大取值最优条码:");
                }
                #region 按照最大平均功率取出条码，如果最大功率相同，则按照次数最大取值最优条码
                #region 求出最大功率
                int iMaxAvgPow = cBarcodeObjList[0].RSSI;
                for (int i = 0; i < cBarcodeObjList.Count; i++)
                {
                    if (iPrintLogFlag == 1)
                    {
                        #region 打印所有条码 次数和平均功率
                         LogInfo.Info("条码:[ " + 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)
                {
                     LogInfo.Info("按照平均功率最大，最大平均功率为：" + iMaxAvgPow.ToString());
                }
                #endregion
                #region 求出等于最大功率的所有条码对象
                List<TagInfo> cBarcodeObjListCount = new List<TagInfo>();
                foreach (TagInfo itemMax in cBarcodeObjList)
                {
                    if (itemMax.RSSI == iMaxAvgPow)
                    {
                        cBarcodeObjListCount.Add(itemMax);
                    }
                }
                if (iPrintLogFlag == 1)
                {
                     LogInfo.Info("按照平均功率最大，最大平均功率相同的条码数量为：" + cBarcodeObjListCount.Count.ToString());
                }
                #endregion
                #region 求出最优条码
                //如果只有一条
                if (cBarcodeObjListCount.Count == 1)
                {
                    if (iPrintLogFlag == 1)
                    {
                         LogInfo.Info("---- 取出的最优条码是: " + 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)
                    {
                         LogInfo.Info("在最大平均功率相同的条码中，读取次数最多为：" + iReadMaxCount.ToString());
                    }
                    #endregion
                    foreach (TagInfo itemReadCount in cBarcodeObjListCount)
                    {
                        if (itemReadCount.Count == iReadMaxCount)
                        {
                            if (iPrintLogFlag == 1)
                            {
                                 LogInfo.Info("---- 取出的最优条码是: " + 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();
        private List<TagInfo> GetTagInfos(byte[] AutoDealReportData)
        {
            List<TagInfo> tagInfoList = new List<TagInfo>();
            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;
                int dataLength = Convert.ToInt32(AutoDealReportData[2].ToString());
                for (int j = 0; j < iBarcodeGroupCount; j++)
                {
                    //int EPCLength = dataLength - (6 * iBarcodeGroupCount);

                    TagInfo tag = new TagInfo();
                    byte[] tempPCByte = new byte[2];     //取出PC
                    Array.Clear(tempPCByte, 0, 2);
                    Array.Copy(AutoDealReportData, iFirstPC, tempPCByte, 0, 2);

                    //PC转二进制取前五位转十进制
                    int epcLength = Convert.ToInt32(Convert.ToString(Convert.ToInt64(tempPCByte[0].ToString("X"), 16), 2).PadLeft(8, '0').Substring(0, 5), 2) * 2;

                    //int pc = Convert.ToInt32(tempPCByte[0].ToString());
                    //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.EPCstring = StringChange.bytesToHexStr(tempDataByte, tempDataByte.Length).Substring(0, 8);

                    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;

                    LogInfo.Info("----函数调用:Device_AutoDealContent 第[" + (iCommonSecondFlag + 1) + "]次数据解析为：" + StringChange.bytesToHexStr(tempDataByte, tempDataByte.Length) + ",读取标签次数：[" + tempDataCount + "],标签信号强度：[" + tempDataRSSI + "],天线号：[" + tempDataANT + "]");
                    iCommonSecondFlag++;
                    if (iCommonSecondFlag == iBarcodeGroupCount)
                    {
                        mutauto.ReleaseMutex();
                        return tagInfoList;
                    }
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString());
                mutauto.ReleaseMutex();
            }
            return tagInfoList;
            

        }

        public byte[] Device_AutoDealContent(byte[] AutoDealReportData)
        {
            List<TagInfo> tagInfoList = new List<TagInfo>();

            byte[] bResultEPC_Data = new byte[12];
            m_AutoReadEPC = null;
            m_readEPCDataLen = 0;
            // LogInfo.Info("----函数调用:Device_AutoDealContent 开始!");
            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("X2")); //标签组数
                int iBarcodeLength = 12;        //标签长度
                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];

                    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 = StringChange.bytesToHexStr(tempDataByte, tempDataByte.Length);
                    tag.Antana = tempDataANT;
                    tagInfoList.Add(tag);
                    int iBarcodeListLen = tagInfoList.Count; //特别注意，必须这样，要不然会多一条数据

                    iFirstCountPos = iFirstCountPos + 17;
                    iFirstRSSIPos = iFirstCountPos + 1;
                    iFirstAnt = iFirstRSSIPos + 1;
                    iFirstLeftBarcketPos = iFirstLeftBarcketPos + 17;

                     LogInfo.Info("----函数调用:Device_AutoDealContent 第[" + (iCommonSecondFlag + 1) + "]次数据解析为：" + StringChange.bytesToHexStr(tempDataByte, tempDataByte.Length) + ",读取标签次数：[" + tempDataCount + "],标签信号强度：[" + tempDataRSSI + "],天线号：[" + tempDataANT + "]");
                    iCommonSecondFlag++;
                    if (iCommonSecondFlag == iBarcodeGroupCount)
                    {
                        byte[] bResult = new byte[12];
                        bResult = CommandAnalysisBarcode(tagInfoList, 1, 1);
                        m_readEPCDataLen = tempDataByte.Length;
                        m_AutoReadEPC = new byte[m_readEPCDataLen];
                        Array.Copy(bResult, 0, m_AutoReadEPC, 0, m_readEPCDataLen);
                         LogInfo.Info("----函数调用:Device_GetOneIdentifyData_Finish for End!");
                         LogInfo.Info("------------------------------------------------------------------------------------------------------");
                         LogInfo.Info("\r\n");
                        mutauto.ReleaseMutex();
                        return m_AutoReadEPC;
                    }
                }
            }
            catch (Exception ex)
            {
                 LogInfo.Info("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString());
                mutauto.ReleaseMutex();
            }
            return m_AutoReadEPC;
        }
        #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)   //设置功率工程
                //    {
                //         LogInfo.Info("设置天线读功率成功！");
                //        bResult = true;
                //    }
                //    else
                //    {
                //        LogInfo.Info("设置天线读功率失败;");
                //        bResult = false;
                //    }
                //}
                //else
                //{
                //    Set_OK = MyReader.SetPower(Convert.ToSingle(m_ReadDbm), iDbi);
                //    if (Set_OK)   //设置功率工程
                //    {
                //         LogInfo.Info("设置天线写功率成功！");
                //        bResult = true;
                //    }
                //    else
                //    {
                //        LogInfo.Info("设置天线写功率失败;");
                //        bResult = false;
                //    }
                //}
            }
            catch (Exception ex)
            {
                LogInfo.Info("Device_SetRf异常：" + ex.Message);
                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);
                 LogInfo.Info("函数调用: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;
                pMessagePack.m_pData[3] = 0x02;
                //pMessagePack.m_pData[4] = 0x03;
                //pMessagePack.m_pData[5] = 0xE8;
                u16byte = BitConverter.GetBytes(Timedout);  //超时时间
                u16byte = StringChange.Swap16Bytes(u16byte);  //协议里为大端在前
                 LogInfo.Info("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 && m_OneEpcAntenna == Antenna) || Antenna == 255) //有数据，正常
                        {
                            strResult = StringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen);
                            //pReadData = new byte[m_OneEpcDataLen];
                            //Array.Copy(m_OneEpcData, 0, pReadData, 0, m_OneEpcDataLen);
                             LogInfo.Info("Device_GetOneIdentifyData:" + StringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen));

                            m_OneEpcDataLen = 0;
                            m_OneEpcAntenna = 254;
                        }
                        else
                        {
                            strResult = StringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen);
                             LogInfo.Info("Device_GetOneIdentifyData:" + StringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen));

                            m_OneEpcDataLen = 0;
                            m_OneEpcAntenna = 254;
                            strResult = "";
                        }
                    }
                    else   //超时
                    {
                        m_OneEpcDataLen = 0;
                        m_OneEpcAntenna = 254;
                         LogInfo.Info("Device_GetOneIdentifyData超时未返回");
                        strResult = "";
                    }
                }
                else
                {
                    m_OneEpcDataLen = 0;
                    m_OneEpcAntenna = 254;
                     LogInfo.Info("Device_GetOneIdentifyData发送识别单条EPC命令超时");
                    strResult = "";
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("Device_GetOneIdentifyData识别单条EPC数据异常：" + ex.Message);
                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
            {
                 LogInfo.Info("函数调用: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
                {
                     LogInfo.Info("发送开始连续识别命令失败：");
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("发送开始连续识别命令异常：" + ex.Message);
                bResult = false;
            }
            return bResult;
        }

        public bool Device_StopIdentify()
        {
            bool bResult = false;
            try
            {
                 LogInfo.Info("函数调用: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
                {
                     LogInfo.Info("发送停止连续识别命令失败：");
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                LogInfo.Info("发送停止连续识别命令异常：" + ex.Message);
                bResult = false;
            }
            return bResult;
        }

        public ushort Device_GetIdentifyData(ref byte[] pReadData, byte Antenna)
        {
            return 0;
        }

        public bool Device_BeginIdentify(byte Antenna, int timeout, bool AutoRead, int num)
        {
            throw new NotImplementedException();
        }

        public bool Device_StopIdentify(byte Antenna)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
