Exemplo n.º 1
0
        private int SendMessage(byte btReadId, byte btCmd, byte[] btAryData)
        {
            MessageTran msgTran = new MessageTran(btReadId, btCmd, btAryData);

            return(SendMessage(msgTran.AryTranData));
        }
Exemplo n.º 2
0
        public byte CheckValue(byte[] btAryData)
        {
            MessageTran msgTran = new MessageTran();

            return(msgTran.CheckSum(btAryData, 0, btAryData.Length));
        }
Exemplo n.º 3
0
        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 (AnalyCallback != null)
                                {
                                    AnalyCallback(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)
            {
            }
        }