远瑞通读卡器c#对接说明

远瑞通读卡器c#对接说明

目录

ip修改

工具安装

在这里插入图片描述在这里插入图片描述

读卡器调试

工具
在这里插入图片描述
设置界面:
在这里插入图片描述在这里插入图片描述功率大小会影响识别距离。(大->长)
在这里插入图片描述

代码实现

 /// <summary>
    /// 远睿通读卡器
    /// 卡号规则
    /// 
    /// </summary>
    public class YRTReaderBLL
    {
        private HardWareSDK.YRTReaderSDK reader;
        private InventoryBuffer m_curInventoryBuffer = new InventoryBuffer();
        System.Timers.Timer timerInventory = new System.Timers.Timer(Convert.ToInt32(AppConfigHelper.GetAppSettings("YRTCardReaderTimer")));
        private ReaderSetting m_curSetting = new ReaderSetting();
        private bool IsOpen = false;
        static readonly string HeaderBZ = "091202";

        /// <summary>
        /// 数据回传委托
        /// </summary>
        /// <param name="epc"></param>
        public delegate void GetDataCallbackDelegate(string epc);
        /// <summary>
        /// 读卡器数据回传函数
        /// </summary>
        public GetDataCallbackDelegate getDataCallbackEvent;
        Models.TerminalM terminal;
       
        public YRTReaderBLL(Models.TerminalM t)
        {
            this.terminal = t;
            DelegateHelper.YRTResetEvent += YRTResetEvent1;
            //初始化访问读写器实例
            reader = new HardWareSDK.YRTReaderSDK();
            //回调函数
            reader.AnalyCallback = AnalyData;

            ConnectServer();
            Open();

            timerInventory.Elapsed += timerInventoryEvent;
            timerInventory.Enabled = true;
            timerInventory.AutoReset = false;
        }
        /// <summary>
        /// 重启读卡器
        /// </summary>
        private void YRTResetEvent1()
        {
            timerInventory.Elapsed -= timerInventoryEvent;
            reader.SignOut();
            Thread.Sleep(500);
            ConnectServer();
            Open();
            timerInventory.Elapsed += timerInventoryEvent;
            timerInventory.Enabled = true;
            timerInventory.AutoReset = false;
        }

        private void timerInventoryEvent(object sender, ElapsedEventArgs e)
        {
            try
            {
                reader.InventoryReal(m_curSetting.btReadId, m_curInventoryBuffer.btRepeat);
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteWarn(ex.ToString());
            }
            timerInventory.Start();
        }

        public void Open()
        {
            try
            {
                if (!IsOpen)
                {
                    IsOpen = true;
                    m_curInventoryBuffer.ClearInventoryPar();
                    m_curInventoryBuffer.btRepeat = 1;
                    m_curInventoryBuffer.bLoopCustomizedSession = false;
                    m_curInventoryBuffer.lAntenna.Add(0x00);
                    m_curInventoryBuffer.bLoopInventory = true;
                    m_curInventoryBuffer.bLoopInventoryReal = true;
                    m_curInventoryBuffer.ClearInventoryRealResult();
                    byte btWorkAntenna = m_curInventoryBuffer.lAntenna[m_curInventoryBuffer.nIndexAntenna];
                    reader.SetWorkAntenna(m_curSetting.btReadId, btWorkAntenna);
                    m_curSetting.btWorkAntenna = btWorkAntenna;

                    timerInventory.Start();
                }

            }
            catch (System.Exception ex)
            {

                Common.Log4.LogManage.WriteWarn(ex.ToString());
                Common.DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);
            }
        }

        public void Close()
        {
            IsOpen = false;
        }

        private void ConnectServer()
        {
            try
            {
                Models.TerminalM hardWareM = terminal; //IConfig.GetSetCardReader;
                TestConnect.CheckDeviceStatus(hardWareM);
                //处理Tcp连接读写器
                string strException = string.Empty;
                IPAddress ipAddress = IPAddress.Parse(hardWareM.ip);
                int nPort = Convert.ToInt32(hardWareM.port);
                int nRet = reader.ConnectServer(ipAddress, nPort, out strException);
                // int nRet = 0;
                if (nRet != 0)
                {
                    string strLog = "连接读写器失败,失败原因: " + strException;
                    // SetMess(strLog);
                    Common.Log4.LogManage.WriteWarn(strLog);
                    Common.DelegateHelper.InfoMessageEvent?.Invoke(strLog);
                    // AutoControlTransportApiServer.AddHardWareLog(hardWareM.Name, 1, strLog);
                    return;
                }
                else
                {
                    //SetMess("连接成功");
                }
            }
            catch (System.Exception ex)
            {
                // SetMess(ex.Message);
                Common.Log4.LogManage.WriteWarn(ex.ToString());
                Common.DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);
            }
        }
        private static readonly object obj = new object();
       // public string cardNum = string.Empty;
        private void AnalyData(MessageTran msgTran)
        {
            // SetMess("AnalyData");

            lock (obj)
            {
                if (msgTran.AryData.Length != 1)
                {
                    int nLength = msgTran.AryData.Length;
                    int nEpcLength = nLength - 4;
                    string strEPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 3, nEpcLength);
                    if (strEPC.Length >= 12)
                    {
                        strEPC = strEPC.Substring(0, 12);
                        string header = strEPC.Substring(0, 6);
                        if (header == HeaderBZ)
                        {
                            //cardNum = strEPC;
                            Common.Log4.LogManage.WriteWarn("YRTReaderBLL get cardNum " + strEPC);
                            getDataCallbackEvent?.Invoke(strEPC);
                        }
                        else
                        {
                            Common.Log4.LogManage.WriteWarn(strEPC + "卡号有误,请重新刷卡");
                            Common.DelegateHelper.InfoMessageEvent?.Invoke(strEPC + "卡号有误,请重新刷卡");
                        }
                    }
                }
                else
                {
                    string strErrorCode = CCommondMethod.FormatErrorCode(msgTran.AryData[0]);
                    string strLog = "读卡器回调失败,失败原因: " + strErrorCode;
                    Common.Log4.LogManage.WriteWarn(strLog);
                }
            }
        }
    }

    class InventoryBuffer
    {
        public byte btRepeat;
        public byte btSession;
        public byte btTarget;
        public List<byte> lAntenna;
        public bool bLoopInventory;
        public int nIndexAntenna;
        public int nCommond;
        public bool bLoopInventoryReal;
        public bool bLoopCustomizedSession;

        public int nTagCount;
        public int nDataCount; //执行一次命令所返回的标签记录条数
        public int nCommandDuration;
        public int nReadRate;
        public int nCurrentAnt;
        public List<int> lTotalRead;
        public DateTime dtStartInventory;
        public DateTime dtEndInventory;
        public int nMaxRSSI;
        public int nMinRSSI;
        public DataTable dtTagTable;
        public DataTable dtTagDetailTable;

        public InventoryBuffer()
        {
            btRepeat = 0x00;
            lAntenna = new List<byte>();
            bLoopInventory = false;
            nIndexAntenna = 0;
            nCommond = 0;
            bLoopInventoryReal = false;

            nTagCount = 0;
            nReadRate = 0;
            lTotalRead = new List<int>();
            dtStartInventory = DateTime.Now;
            dtEndInventory = DateTime.Now;
            nMaxRSSI = 0;
            nMinRSSI = 0;

            dtTagTable = new DataTable();
            dtTagTable.Columns.Add("COLPC", typeof(string));
            dtTagTable.Columns.Add("COLCRC", typeof(string));
            dtTagTable.Columns.Add("COLEPC", typeof(string));
            dtTagTable.Columns.Add("COLANT", typeof(string));
            dtTagTable.Columns.Add("COLRSSI", typeof(string));
            dtTagTable.Columns.Add("COLINVCNT", typeof(string));
            dtTagTable.Columns.Add("COLFREQ", typeof(string));
            dtTagTable.Columns.Add("COLANT1", typeof(string));
            dtTagTable.Columns.Add("COLANT2", typeof(string));
            dtTagTable.Columns.Add("COLANT3", typeof(string));
            dtTagTable.Columns.Add("COLANT4", typeof(string));

            dtTagDetailTable = new DataTable();
            dtTagDetailTable.Columns.Add("COLEPC", typeof(string));
            dtTagDetailTable.Columns.Add("COLRSSI", typeof(string));
            dtTagDetailTable.Columns.Add("COLANT", typeof(string));
            dtTagDetailTable.Columns.Add("COLFRE", typeof(string));
        }

        public void ClearInventoryPar()
        {
            btRepeat = 0x00;
            lAntenna.Clear();
            //bLoopInventory = false;
            nIndexAntenna = 0;
            nCommond = 0;
            bLoopInventoryReal = false;
        }

        public void ClearInventoryResult()
        {
            nTagCount = 0;
            nReadRate = 0;
            lTotalRead.Clear();
            dtStartInventory = DateTime.Now;
            dtEndInventory = DateTime.Now;
            nMaxRSSI = 0;
            nMinRSSI = 0;
            dtTagTable.Rows.Clear();
        }

        public void ClearInventoryRealResult()
        {
            nTagCount = 0;
            nReadRate = 0;
            lTotalRead.Clear();
            dtStartInventory = DateTime.Now;
            dtEndInventory = DateTime.Now;
            nMaxRSSI = 0;
            nMinRSSI = 0;
            dtTagTable.Rows.Clear();
            dtTagDetailTable.Clear();
        }
    }

    class ReaderSetting
    {
        public byte btReadId;
        public byte btMajor;
        public byte btMinor;
        public byte btIndexBaudrate;
        public byte btPlusMinus;
        public byte btTemperature;
        public byte btOutputPower;
        public byte btWorkAntenna;
        public byte btDrmMode;
        public byte btRegion;
        public byte btFrequencyStart;
        public byte btFrequencyEnd;
        public byte btBeeperMode;
        public byte btGpio1Value;
        public byte btGpio2Value;
        public byte btGpio3Value;
        public byte btGpio4Value;
        public byte btAntDetector;
        public byte btMonzaStatus;
        public string btReaderIdentifier;
        public byte btAntImpedance;
        public byte btImpedanceFrequency;

        public int nUserDefineStartFrequency;
        public byte btUserDefineFrequencyInterval;
        public byte btUserDefineChannelQuantity;
        public byte btLinkProfile;

        public ReaderSetting()
        {
            btReadId = 0xFF;
            btMajor = 0x00;
            btMinor = 0x00;
            btIndexBaudrate = 0x00;
            btPlusMinus = 0x00;
            btTemperature = 0x00;
            btOutputPower = 0x00;
            btWorkAntenna = 0x00;
            btDrmMode = 0x00;
            btRegion = 0x00;
            btFrequencyStart = 0x00;
            btFrequencyEnd = 0x00;
            btBeeperMode = 0x00;
            btGpio1Value = 0x00;
            btGpio2Value = 0x00;
            btGpio3Value = 0x00;
            btGpio4Value = 0x00;
            btAntDetector = 0x00;
        }
    }


 public class CCommondMethod
    {
        /// <summary>
        /// 字符串转16进制数组,字符串以空格分割
        /// </summary>
        /// <param name="strHexValue"></param>
        /// <returns></returns>
        public static byte[] StringToByteArray(string strHexValue)
        {
            string[] strAryHex = strHexValue.Split(' ');
            byte[] btAryHex = new byte[strAryHex.Length];

            try
            {
                int nIndex = 0;
                foreach (string strTemp in strAryHex)
                {
                    btAryHex[nIndex] = Convert.ToByte(strTemp, 16);
                    nIndex++;
                }
            }
            catch (System.Exception ex)
            {
                Log4.LogManage.WriteErr("StringToByteArray " + ex.Message);
            }

            return btAryHex;
        }

        /// <summary>
        /// 字符数组转为16进制数组
        /// </summary>
        /// <param name="strAryHex"></param>
        /// <param name="nLen"></param>
        /// <returns></returns>
        public static byte[] StringArrayToByteArray(string[] strAryHex, int nLen)
        {
            if (strAryHex.Length < nLen)
            {
                nLen = strAryHex.Length;
            }

            byte[] btAryHex = new byte[nLen];

            try
            {
                int nIndex = 0;
                foreach (string strTemp in strAryHex)
                {
                    btAryHex[nIndex] = Convert.ToByte(strTemp, 16);
                    nIndex++;
                }
            }
            catch (System.Exception ex)
            {
                Common.Log4.LogManage.WriteErr("StringArrayToByteArray " + ex.Message);
            }

            return btAryHex;
        }

        /// <summary>
        /// 16进制字符数组转成字符串
        /// </summary>
        /// <param name="btAryHex"></param>
        /// <param name="nIndex"></param>
        /// <param name="nLen"></param>
        /// <returns></returns>
        public static string ByteArrayToString(byte[] btAryHex, int nIndex, int nLen)
        {
            if (nIndex + nLen > btAryHex.Length)
            {
                nLen = btAryHex.Length - nIndex;
            }

            string strResult = string.Empty;

            for (int nloop = nIndex; nloop < nIndex + nLen; nloop++)
            {
                string strTemp = string.Format("{0:X2}", btAryHex[nloop]);

                strResult += strTemp;
            }

            return strResult;
        }

        public static string FormatErrorCode(byte btErrorCode)
        {
            string strErrorCode = "";
            switch (btErrorCode)
            {
                case 0x10:
                    strErrorCode = "命令已执行";
                    break;
                case 0x11:
                    strErrorCode = "命令执行失败";
                    break;
                case 0x20:
                    strErrorCode = "CPU 复位错误";
                    break;
                case 0x21:
                    strErrorCode = "打开CW 错误";
                    break;
                case 0x22:
                    strErrorCode = "天线未连接";
                    break;
                case 0x23:
                    strErrorCode = "写Flash 错误";
                    break;
                case 0x24:
                    strErrorCode = "读Flash 错误";
                    break;
                case 0x25:
                    strErrorCode = "设置发射功率错误";
                    break;
                case 0x31:
                    strErrorCode = "盘存标签错误";
                    break;
                case 0x32:
                    strErrorCode = "读标签错误";
                    break;
                case 0x33:
                    strErrorCode = "写标签错误";
                    break;
                case 0x34:
                    strErrorCode = "锁定标签错误";
                    break;
                case 0x35:
                    strErrorCode = "灭活标签错误";
                    break;
                case 0x36:
                    strErrorCode = "无可操作标签错误";
                    break;
                case 0x37:
                    strErrorCode = "成功盘存但访问失败";
                    break;
                case 0x38:
                    strErrorCode = "缓存为空";
                    break;
                case 0x40:
                    strErrorCode = "访问标签错误或访问密码错误";
                    break;
                case 0x41:
                    strErrorCode = "无效的参数";
                    break;
                case 0x42:
                    strErrorCode = "wordCnt 参数超过规定长度";
                    break;
                case 0x43:
                    strErrorCode = "MemBank 参数超出范围";
                    break;
                case 0x44:
                    strErrorCode = "Lock 数据区参数超出范围";
                    break;
                case 0x45:
                    strErrorCode = "LockType 参数超出范围";
                    break;
                case 0x46:
                    strErrorCode = "读卡器地址无效";
                    break;
                case 0x47:
                    strErrorCode = "Antenna_id 超出范围";
                    break;
                case 0x48:
                    strErrorCode = "输出功率参数超出范围";
                    break;
                case 0x49:
                    strErrorCode = "射频规范区域参数超出范围";
                    break;
                case 0x4A:
                    strErrorCode = "波特率参数超过范围";
                    break;
                case 0x4B:
                    strErrorCode = "蜂鸣器设置参数超出范围";
                    break;
                case 0x4C:
                    strErrorCode = "EPC 匹配长度越界";
                    break;
                case 0x4D:
                    strErrorCode = "EPC 匹配长度错误";
                    break;
                case 0x4E:
                    strErrorCode = "EPC 匹配参数超出范围";
                    break;
                case 0x4F:
                    strErrorCode = "频率范围设置参数错误";
                    break;
                case 0x50:
                    strErrorCode = "无法接收标签的RN16";
                    break;
                case 0x51:
                    strErrorCode = "DRM 设置参数错误";
                    break;
                case 0x52:
                    strErrorCode = "PLL 不能锁定";
                    break;
                case 0x53:
                    strErrorCode = "射频芯片无响应";
                    break;
                case 0x54:
                    strErrorCode = "输出达不到指定的输出功率";
                    break;
                case 0x55:
                    strErrorCode = "版权认证未通过";
                    break;
                case 0x56:
                    strErrorCode = "频谱规范设置错误";
                    break;
                case 0x57:
                    strErrorCode = "输出功率过低";
                    break;
                case 0xFF:
                    strErrorCode = "未知错误";
                    break;

                default:
                    break;
            }

            return strErrorCode;
        }



    }


 /// <summary>
    /// 远睿通读卡器
    /// </summary>
    public class YRTReaderSDK
    {
        #region 属性
        public static IPAddress myIp;
        public static  int myPort;
        #endregion
        private ITalker italker;
        private SerialPort iSerialPort;
        private int m_nType = -1;

        public ReciveDataCallback ReceiveCallback;
        public SendDataCallback SendCallback;
        public AnalyDataCallback AnalyCallback;

       

        //记录未处理的接收数据,主要考虑接收数据分段
        byte[] m_btAryBuffer = new byte[4096];
        //记录未处理数据的有效长度
        int m_nLenth = 0;
      

        public YRTReaderSDK()
        {
            this.italker = new Talker();

            italker.MessageReceived += new MessageReceivedEventHandler(ReceivedTcpData);

            iSerialPort = new SerialPort();

            iSerialPort.DataReceived += new SerialDataReceivedEventHandler(ReceivedComData);

          
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        public int OpenCom(string strPort, int nBaudrate, out string strException)
        {
            strException = string.Empty;

            if (iSerialPort.IsOpen)
            {
                iSerialPort.Close();
            }

            try
            {
                iSerialPort.PortName = strPort;
                iSerialPort.BaudRate = nBaudrate;
                iSerialPort.ReadTimeout = 200;
                iSerialPort.Open();
            }
            catch (System.Exception ex)
            {
                strException = ex.Message;
                Common.Log4.LogManage.WriteErr("error "+ ex.ToString());
                return -1;
            }

            m_nType = 0;
            return 0;
        }

        public void CloseCom()
        {
            if (iSerialPort.IsOpen)
            {
                iSerialPort.Close();
            }

            m_nType = -1;
        }

        public int ConnectServer(IPAddress ipAddress, int nPort, out string strException)
        {
            myIp = ipAddress;
            myPort = nPort;
            strException = string.Empty;

            if (!italker.Connect(ipAddress, nPort, out strException))
            {
                return -1;
            }

            m_nType = 1;
            return 0;
        }
      
        public void SignOut()
        {
            italker.SignOut();
            m_nType = -1;
        }

        private void ReceivedTcpData(byte[] btAryBuffer)
        {
            RunReceiveDataCallback(btAryBuffer);
        }

        private void ReceivedComData(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                int nCount = iSerialPort.BytesToRead;
                if (nCount == 0)
                {
                    return;
                }

                byte[] btAryBuffer = new byte[nCount];
                iSerialPort.Read(btAryBuffer, 0, nCount);

                RunReceiveDataCallback(btAryBuffer);
            }
            catch (System.Exception ex)
            {
                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
        }

        private void RunReceiveDataCallback(byte[] btAryReceiveData)
        {
            try
            {
                if (ReceiveCallback != null)
                {
                    ReceiveCallback(btAryReceiveData);
                }

                int nCount = btAryReceiveData.Length;
                byte[] btAryBuffer = new byte[nCount + m_nLenth];
                Array.Copy(m_btAryBuffer, btAryBuffer, m_nLenth);
                Array.Copy(btAryReceiveData, 0, btAryBuffer, m_nLenth, btAryReceiveData.Length);

                //分析接收数据,以0xA0为数据起点,以协议中数据长度为数据终止点
                int nIndex = 0;//当数据中存在A0时,记录数据的终止点
                int nMarkIndex = 0;//当数据中不存在A0时,nMarkIndex等于数据组最大索引
                for (int nLoop = 0; nLoop < btAryBuffer.Length; nLoop++)
                {
                    if (btAryBuffer.Length > nLoop + 1)
                    {
                        if (btAryBuffer[nLoop] == 0xA0)
                        {
                            int nLen = Convert.ToInt32(btAryBuffer[nLoop + 1]);
                            if (nLoop + 1 + nLen < btAryBuffer.Length)
                            {
                                byte[] btAryAnaly = new byte[nLen + 2];
                                Array.Copy(btAryBuffer, nLoop, btAryAnaly, 0, nLen + 2);

                                MessageTran msgTran = new MessageTran(btAryAnaly);
                                if (msgTran!= null && msgTran.AryData != null)
                                {
                                    AnalyCallback?.Invoke(msgTran);
                                }
                               
                               
                                nLoop += 1 + nLen;
                                nIndex = nLoop + 1;
                            }
                            else
                            {
                                nLoop += 1 + nLen;
                            }
                        }
                        else
                        {
                            nMarkIndex = nLoop;
                        }
                    }
                }

                if (nIndex < nMarkIndex)
                {
                    nIndex = nMarkIndex + 1;
                }

                if (nIndex < btAryBuffer.Length)
                {
                    m_nLenth = btAryBuffer.Length - nIndex;
                    Array.Clear(m_btAryBuffer, 0, 4096);
                    Array.Copy(btAryBuffer, nIndex, m_btAryBuffer, 0, btAryBuffer.Length - nIndex);
                }
                else
                {
                    m_nLenth = 0;
                }
            }
            catch (System.Exception ex)
            {
                Common.Log4.LogManage.WriteErr("error RunReceiveDataCallback" + ex.ToString());
            }
        }

        public int SendMessage(byte[] btArySenderData)
        {
           
            //串口连接方式
            if (m_nType == 0)
            {
                if (!iSerialPort.IsOpen)
                {
                    return -1;
                }

                iSerialPort.Write(btArySenderData, 0, btArySenderData.Length);

                if (SendCallback != null)
                {
                    SendCallback(btArySenderData);
                }

                return 0;
            }
            //Tcp连接方式
            else if (m_nType == 1)
            {
                if (!italker.IsConnect())
                {
                    return -1; 
                } 

                if (italker.SendMessage(btArySenderData))
                {
                    SendCallback?.Invoke(btArySenderData);

                    return 0;
                }
            }

            return -1;
        }

        private int SendMessage(byte btReadId, byte btCmd)
        {
            MessageTran msgTran = new MessageTran(btReadId, btCmd);
            return SendMessage(msgTran.AryTranData);
        }

        private int SendMessage(byte btReadId, byte btCmd, byte[] btAryData)
        {
            MessageTran msgTran = new MessageTran(btReadId, btCmd, btAryData);

            return SendMessage(msgTran.AryTranData);
        }

        public byte CheckValue(byte[] btAryData)
        {
            MessageTran msgTran = new MessageTran();

            return msgTran.CheckSum(btAryData, 0, btAryData.Length);
        }

        public int Reset(byte btReadId)
        {
            byte btCmd = 0x70;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int SetUartBaudrate(byte btReadId, int nIndexBaudrate)
        {
            byte btCmd = 0x71;
            byte[] btAryData = new byte[1];
            btAryData[0] = Convert.ToByte(nIndexBaudrate);

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int GetFirmwareVersion(byte btReadId)
        {
            byte btCmd = 0x72;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int SetReaderAddress(byte btReadId, byte btNewReadId)
        {
            byte btCmd = 0x73;
            byte[] btAryData = new byte[1];
            btAryData[0] = btNewReadId;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int SetWorkAntenna(byte btReadId, byte btWorkAntenna)
        {
            byte btCmd = 0x74;
            byte[] btAryData = new byte[1];
            btAryData[0] = btWorkAntenna;
            int nResult = SendMessage(btReadId, btCmd, btAryData);
            return nResult;
        }

        public int GetWorkAntenna(byte btReadId)
        {
            byte btCmd = 0x75;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int SetOutputPower(byte btReadId, byte btOutputPower)
        {
            byte btCmd = 0x76;
            byte[] btAryData = new byte[1];
            btAryData[0] = btOutputPower;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int GetOutputPower(byte btReadId)
        {
            byte btCmd = 0x77;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int MeasureReturnLoss(byte btReadId, byte btFrequency)
        {
            byte btCmd = 0x7E;
            byte[] btAryData = new byte[1];
            btAryData[0] = btFrequency;
            int nResult = SendMessage(btReadId, btCmd, btAryData);
            return nResult;
        }

        public int SetFrequencyRegion(byte btReadId, byte btRegion, byte btStartRegion, byte btEndRegion)
        {
            byte btCmd = 0x78;
            byte[] btAryData = new byte[3];
            btAryData[0] = btRegion;
            btAryData[1] = btStartRegion;
            btAryData[2] = btEndRegion;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }
        public int SetUserDefineFrequency(byte btReadId, int nStartFreq, byte btFreqInterval, byte btChannelQuantity)
        {
            byte btCmd = 0x78;
            byte[] btAryFreq = new byte[3];
            byte[] btAryData = new byte[6];
            btAryFreq = BitConverter.GetBytes(nStartFreq);

            btAryData[0] = 4;
            btAryData[1] = btFreqInterval;
            btAryData[2] = btChannelQuantity;
            btAryData[3] = btAryFreq[2];
            btAryData[4] = btAryFreq[1];
            btAryData[5] = btAryFreq[0];

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int GetFrequencyRegion(byte btReadId)
        {
            byte btCmd = 0x79;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int SetBeeperMode(byte btReadId, byte btMode)
        {
            byte btCmd = 0x7A;
            byte[] btAryData = new byte[1];
            btAryData[0] = btMode;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int GetReaderTemperature(byte btReadId)
        {
            byte btCmd = 0x7B;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int GetAntImpedanceMatch(byte btReadId, byte btFrequency)
        {
            byte btCmd = 0x7E;
            byte[] btAryData = new byte[1];
            btAryData[0] = btFrequency;
            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int SetDrmMode(byte btReadId, byte btDrmMode)
        {
            byte btCmd = 0x7C;
            byte[] btAryData = new byte[1];
            btAryData[0] = btDrmMode;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int GetDrmMode(byte btReadId)
        {
            byte btCmd = 0x7D;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int ReadGpioValue(byte btReadId)
        {
            byte btCmd = 0x60;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int WriteGpioValue(byte btReadId, byte btChooseGpio, byte btGpioValue)
        {
            byte btCmd = 0x61;
            byte[] btAryData = new byte[2];
            btAryData[0] = btChooseGpio;
            btAryData[1] = btGpioValue;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int SetAntDetector(byte btReadId, byte btDetectorStatus)
        {
            byte btCmd = 0x62;
            byte[] btAryData = new byte[1];
            btAryData[0] = btDetectorStatus;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int GetAntDetector(byte btReadId)
        {
            byte btCmd = 0x63;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int GetMonzaStatus(byte btReadId)
        {
            byte btCmd = 0x8e;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int SetMonzaStatus(byte btReadId, byte btMonzaStatus)
        {
            byte btCmd = 0x8D;
            byte[] btAryData = new byte[1];
            btAryData[0] = btMonzaStatus;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int SetRadioProfile(byte btReadId, byte btProfile)
        {
            byte btCmd = 0x69;
            byte[] btAryData = new byte[1];
            btAryData[0] = btProfile;
            int nResult = SendMessage(btReadId, btCmd, btAryData);
            return nResult;
        }
        public int GetRadioProfile(byte btReadId)
        {
            byte btCmd = 0x6A;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int GetReaderIdentifier(byte btReadId)
        {
            byte btCmd = 0x68;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int SetReaderIdentifier(byte btReadId, byte[] identifier)
        {
            byte btCmd = 0x67;

            int nResult = SendMessage(btReadId, btCmd, identifier);

            return nResult;
        }


        public int Inventory(byte btReadId, byte byRound)
        {
            byte btCmd = 0x80;
            byte[] btAryData = new byte[1];
            btAryData[0] = byRound;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int CustomizedInventory(byte btReadId, byte session, byte target, byte byRound)
        {
            byte btCmd = 0x8B;
            byte[] btAryData = new byte[3];
            btAryData[0] = session;
            btAryData[1] = target;
            btAryData[2] = byRound;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int ReadTag(byte btReadId, byte btMemBank, byte btWordAdd, byte btWordCnt)
        {
            byte btCmd = 0x81;
            byte[] btAryData = new byte[3];
            btAryData[0] = btMemBank;
            btAryData[1] = btWordAdd;
            btAryData[2] = btWordCnt;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int WriteTag(byte btReadId, byte[] btAryPassWord, byte btMemBank, byte btWordAdd, byte btWordCnt, byte[] btAryData)
        {
            byte btCmd = 0x82;
            byte[] btAryBuffer = new byte[btAryData.Length + 7];
            btAryPassWord.CopyTo(btAryBuffer, 0);
            btAryBuffer[4] = btMemBank;
            btAryBuffer[5] = btWordAdd;
            btAryBuffer[6] = btWordCnt;
            btAryData.CopyTo(btAryBuffer, 7);

            int nResult = SendMessage(btReadId, btCmd, btAryBuffer);

            return nResult;
        }

        public int LockTag(byte btReadId, byte[] btAryPassWord, byte btMembank, byte btLockType)
        {
            byte btCmd = 0x83;
            byte[] btAryData = new byte[6];
            btAryPassWord.CopyTo(btAryData, 0);
            btAryData[4] = btMembank;
            btAryData[5] = btLockType;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int KillTag(byte btReadId, byte[] btAryPassWord)
        {
            byte btCmd = 0x84;
            byte[] btAryData = new byte[4];
            btAryPassWord.CopyTo(btAryData, 0);

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int SetAccessEpcMatch(byte btReadId, byte btMode, byte btEpcLen, byte[] btAryEpc)
        {
            byte btCmd = 0x85;
            int nLen = Convert.ToInt32(btEpcLen) + 2;
            byte[] btAryData = new byte[nLen];
            btAryData[0] = btMode;
            btAryData[1] = btEpcLen;
            btAryEpc.CopyTo(btAryData, 2);

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int CancelAccessEpcMatch(byte btReadId, byte btMode)
        {
            byte btCmd = 0x85;
            byte[] btAryData = new byte[1];
            btAryData[0] = btMode;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int GetAccessEpcMatch(byte btReadId)
        {
            byte btCmd = 0x86;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int InventoryReal(byte btReadId, byte byRound)
        {
            byte btCmd = 0x89;
            byte[] btAryData = new byte[1];
            btAryData[0] = byRound;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int FastSwitchInventory(byte btReadId, byte[] btAryData)
        {
            byte btCmd = 0x8A;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int GetInventoryBuffer(byte btReadId)
        {
            byte btCmd = 0x90;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int GetAndResetInventoryBuffer(byte btReadId)
        {
            byte btCmd = 0x91;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int GetInventoryBufferTagCount(byte btReadId)
        {
            byte btCmd = 0x92;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int ResetInventoryBuffer(byte btReadId)
        {
            byte btCmd = 0x93;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int SetBufferDataFrameInterval(byte btReadId, byte btInterval)
        {
            byte btCmd = 0x94;
            byte[] btAryData = new byte[1];
            btAryData[0] = btInterval;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int GetBufferDataFrameInterval(byte btReadId)
        {
            byte btCmd = 0x95;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int InventoryISO18000(byte btReadId)
        {
            byte btCmd = 0xb0;

            int nResult = SendMessage(btReadId, btCmd);

            return nResult;
        }

        public int ReadTagISO18000(byte btReadId, byte[] btAryUID, byte btWordAdd, byte btWordCnt)
        {
            byte btCmd = 0xb1;
            int nLen = btAryUID.Length + 2;
            byte[] btAryData = new byte[nLen];
            btAryUID.CopyTo(btAryData, 0);
            btAryData[nLen - 2] = btWordAdd;
            btAryData[nLen - 1] = btWordCnt;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int WriteTagISO18000(byte btReadId, byte[] btAryUID, byte btWordAdd, byte btWordCnt, byte[] btAryBuffer)
        {
            byte btCmd = 0xb2;
            int nLen = btAryUID.Length + 2 + btAryBuffer.Length;
            byte[] btAryData = new byte[nLen];
            btAryUID.CopyTo(btAryData, 0);
            btAryData[btAryUID.Length] = btWordAdd;
            btAryData[btAryUID.Length + 1] = btWordCnt;
            btAryBuffer.CopyTo(btAryData, btAryUID.Length + 2);

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int LockTagISO18000(byte btReadId, byte[] btAryUID, byte btWordAdd)
        {
            byte btCmd = 0xb3;
            int nLen = btAryUID.Length + 1;
            byte[] btAryData = new byte[nLen];
            btAryUID.CopyTo(btAryData, 0);
            btAryData[nLen - 1] = btWordAdd;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }

        public int QueryTagISO18000(byte btReadId, byte[] btAryUID, byte btWordAdd)
        {
            byte btCmd = 0xb4;
            int nLen = btAryUID.Length + 1;
            byte[] btAryData = new byte[nLen];
            btAryUID.CopyTo(btAryData, 0);
            btAryData[nLen - 1] = btWordAdd;

            int nResult = SendMessage(btReadId, btCmd, btAryData);

            return nResult;
        }
    }

    public delegate void ReciveDataCallback(byte[] btAryReceiveData);
    public delegate void SendDataCallback(byte[] btArySendData);
    public delegate void AnalyDataCallback(MessageTran msgTran);

    public class MessageTran
    {
        private byte btPacketType;     //数据包头,默认为0xA0
        private byte btDataLen;        //数据包长度,数据包从‘长度’字节后面开始的字节数,不包含‘长度’字节本身
        private byte btReadId;         //读写器地址
        private byte btCmd;            //数据包命令代码
        private byte[] btAryData;      //数据包命令参数,部分命令无参数
        private byte btCheck;          //校验和,除校验和本身外所有字节的校验和
        private byte[] btAryTranData;  //完整数据包

        //设置属性
        public byte[] AryTranData
        {
            get
            {
                return btAryTranData;
            }
        }

        public byte[] AryData
        {
            get
            {
                return btAryData;
            }
        }

        public byte ReadId
        {
            get
            {
                return btReadId;
            }
        }

        public byte Cmd
        {
            get
            {
                return btCmd;
            }
        }

        public byte PacketType
        {
            get
            {
                return btPacketType;
            }
        }

        public MessageTran()
        {

        }

        public MessageTran(byte btReadId, byte btCmd, byte[] btAryData)
        {
            int nLen = btAryData.Length;

            this.btPacketType = 0xA0;
            this.btDataLen = Convert.ToByte(nLen + 3);
            this.btReadId = btReadId;
            this.btCmd = btCmd;

            this.btAryData = new byte[nLen];
            btAryData.CopyTo(this.btAryData, 0);

            this.btAryTranData = new byte[nLen + 5];
            this.btAryTranData[0] = this.btPacketType;
            this.btAryTranData[1] = this.btDataLen;
            this.btAryTranData[2] = this.btReadId;
            this.btAryTranData[3] = this.btCmd;
            this.btAryData.CopyTo(this.btAryTranData, 4);

            this.btCheck = CheckSum(this.btAryTranData, 0, nLen + 4);
            this.btAryTranData[nLen + 4] = this.btCheck;
        }

        public MessageTran(byte btReadId, byte btCmd)
        {
            this.btPacketType = 0xA0;
            this.btDataLen = 0x03;
            this.btReadId = btReadId;
            this.btCmd = btCmd;

            this.btAryTranData = new byte[5];
            this.btAryTranData[0] = this.btPacketType;
            this.btAryTranData[1] = this.btDataLen;
            this.btAryTranData[2] = this.btReadId;
            this.btAryTranData[3] = this.btCmd;

            this.btCheck = CheckSum(this.btAryTranData, 0, 4);
            this.btAryTranData[4] = this.btCheck;
        }

        public MessageTran(byte[] btAryTranData)
        {
            int nLen = btAryTranData.Length;

            this.btAryTranData = new byte[nLen];
            btAryTranData.CopyTo(this.btAryTranData, 0);


            byte btCK = CheckSum(this.btAryTranData, 0, this.btAryTranData.Length - 1);
            if (btCK != btAryTranData[nLen - 1])
            {
                return;
            }

            this.btPacketType = btAryTranData[0];
            this.btDataLen = btAryTranData[1];
            this.btReadId = btAryTranData[2];
            this.btCmd = btAryTranData[3];
            this.btCheck = btAryTranData[nLen - 1];

            if (nLen > 5)
            {
                this.btAryData = new byte[nLen - 5];
                for (int nloop = 0; nloop < nLen - 5; nloop++)
                {
                    this.btAryData[nloop] = btAryTranData[4 + nloop];
                }
            }
        }

        public byte CheckSum(byte[] btAryBuffer, int nStartPos, int nLen)
        {
            byte btSum = 0x00;

            for (int nloop = nStartPos; nloop < nStartPos + nLen; nloop++)
            {
                btSum += btAryBuffer[nloop];
            }

            return Convert.ToByte(((~btSum) + 1) & 0xFF);
        }
    }

    public delegate void MessageReceivedEventHandler(byte[] btAryBuffer);

    interface ITalker
    {
        event MessageReceivedEventHandler MessageReceived;    // 接收到发来的消息
        bool Connect(IPAddress ip, int port, out string strException);                 // 连接到服务端
        bool Connect(IPAddress ip, int port);
        bool SendMessage(byte[] btAryBuffer);                 // 发送数据包
        void SignOut();                                       // 注销连接

        bool IsConnect();                                    //校验是否连接服务器
    }

    public class Talker : ITalker
    {
      static  int DKQCLJG = Convert.ToInt32(AppConfigHelper.GetAppSettings("DKQCLJG"));
        static int DKQCLJGSM = Convert.ToInt32(AppConfigHelper.GetAppSettings("DKQCLJGSM"));
        System.Timers.Timer timer = new System.Timers.Timer(DKQCLJG);
        bool connnn = true;
        public Talker()
        {
            timer.Elapsed += Timer_Elapsed;
            timer.Enabled = true;
            timer.AutoReset = false;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //Connect(YRTReaderSDK.myIp, YRTReaderSDK.myPort);
        }

        public event MessageReceivedEventHandler MessageReceived;

        TcpClient client;
        Stream streamToTran;
        private Thread waitThread;

        private bool bIsConnect = false;
       

        public bool Connect(IPAddress ipAddress, int nPort, out string strException)
        {
            strException = string.Empty;
            try
            {
                client = new TcpClient();
                client.Connect(ipAddress, nPort);
                streamToTran = client.GetStream();    // 获取连接至远程的流
                //建立线程收取服务器发送数据
                ThreadStart stThead = new ThreadStart(ReceivedData);
                waitThread = new Thread(stThead);
                waitThread.IsBackground = true;
                waitThread.Start();
                bIsConnect = true;
                return true;
            }
            catch (System.Exception ex)
            {
                strException = ex.Message;
                Common.Log4.LogManage.WriteErr(ex.ToString());
                return false;
            }
        }
        public bool Connect(IPAddress ipAddress, int nPort)
        {
            try
            {
                Common.Log4.LogManage.WriteDebug("读卡器断开并重新开始连接。。。");
                this.SignOut();
                Thread.Sleep(DKQCLJGSM);
                client = new TcpClient();
                client.Connect(ipAddress, nPort);
                streamToTran = client.GetStream();
                return true;
                //if (!client.Connected)
                //{
                //    client.Connect(ipAddress, nPort);
                //    streamToTran = client.GetStream();    // 获取连接至远程的流
                //    建立线程收取服务器发送数据
                //    //ThreadStart stThead = new ThreadStart(ReceivedData);
                //    //waitThread = new Thread(stThead);
                //    //waitThread.IsBackground = true;
                //    //waitThread.Start();
                //    bIsConnect = true;
                //    return true;
                //}
                //else
                //{
                //    Common.Log4.LogManage.WriteErr("在发送失败的情况下,关闭连接重新连接,关闭失败!");
                //    return false;
                //}
            }
            catch (System.Exception ex)
            {
                Common.Log4.LogManage.WriteErr("error 读卡器二次连接失败 "+ ex.ToString());
                return false;
            }
        }

        private static readonly object obj = new object();
        private void ReceivedData()
        {
            while (true)
            {
                lock (obj)
                {
                    try
                    {
                        byte[] btAryBuffer = new byte[4096];
                        int nLenRead = streamToTran.Read(btAryBuffer, 0, btAryBuffer.Length);
                      //  timer.Stop();
                        if (nLenRead == 0)
                        {
                            continue;
                        }

                        if (MessageReceived != null)
                        {
                            byte[] btAryReceiveData = new byte[nLenRead];

                            Array.Copy(btAryBuffer, btAryReceiveData, nLenRead);

                            MessageReceived(btAryReceiveData);
                        }
                        else
                        {
                        }
                        
                    }
                    catch (Exception ex)
                    {
                        //Common.Log4.LogManage.WriteErr("error 循环读取读卡器的信息 "+ ex.ToString());
                       // Connect(YRTReaderSDK.myIp, YRTReaderSDK.myPort);
                    } 
                }
            }
        }

        public bool SendMessage(byte[] btAryBuffer)
        {
            //if (!timer.Enabled)
            //{
            //    timer.Start();
            //}
            try
            {
                lock (streamToTran)
                {
                    streamToTran.Write(btAryBuffer, 0, btAryBuffer.Length);
                    return true;
                }
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("error talker SendMessage " + ex.ToString());
                // Connect(YRTReaderSDK.myIp, YRTReaderSDK.myPort);
                //if (ex.Message.Contains("远程主机强迫关闭了一个现有的连接"))
                //{
                //    Connect(YRTReaderSDK.myIp, YRTReaderSDK.myPort);
                //}
                DelegateHelper.YRTResetEvent?.Invoke();
                return false;
            }
        }

        public void SignOut()
        {
            if (streamToTran != null)
                streamToTran.Dispose();
            if (client != null)
                client.Close();

            waitThread.Abort();
            bIsConnect = false;
        }

        public bool IsConnect()
        {
            return bIsConnect;
        }

       
    }


//调用
  private HardWareBLL.YRTReaderBLL yRTReaderBLL;
  //软睿通读卡器
                {
                    PageShow("软睿通读卡器初始化");
                    yRTReaderBLL = new YRTReaderBLL(terminalMs.Where(p => p.code == Convert.ToInt32(myconfig.DKQ)).FirstOrDefault());
                    yRTReaderBLL.getDataCallbackEvent = GetReadDataCallback;
                }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值