Exemplo n.º 1
0
        /// <summary>
        /// 解析数据,收到一包有效数据后,添加到消息队列
        /// </summary>
        /// <param name="recvBytes">接收到的数据</param>
        /// <param name="recvCount">接收到的数据长度</param>
        /// <param name="client">客户端</param>
        /// <param name="parseCount">解析计数器</param>
        /// <param name="arrayParse">解析缓冲区</param>
        public void ParseAndAddMessageToQueue(byte[] RecvBytes, int RecvCount, TcpClient Client, ref int parseCount, byte[] arrayParse)
        {
            //匹配比较数组, -1表示不需要比较,忽略
            int[] arrayCompare = new int[Message.MessageLength]
            {
                Message.MessStartCode, Message.MessVID1, Message.MessVID2, -1, Message.MessRightState, -1, Message.MessStationCode, -1,
                -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, Message.MessEndCode,
            };

            for (int i = 0; i < RecvCount; i++)
            {
                if (arrayCompare[parseCount] != -1)               //需要匹配
                {
                    if (RecvBytes[i] == arrayCompare[parseCount]) //相等
                    {
                        arrayParse[parseCount++] = RecvBytes[i];
                    }
                    else  //不相等,复位计数器
                    {
                        parseCount = 0;
                    }
                }
                else  //不需要比较,直接赋值,并更新计数器
                {
                    arrayParse[parseCount++] = RecvBytes[i];
                }

                if (parseCount >= Message.MessageLength)
                {
                    parseCount = 0;                                         //和校验

                    if (MyMath.CheckSum(arrayParse, Message.MessageLength)) //分析数据,把数据添加到队列m_TcpMeas
                    {
                        TcpMeasType MeasType = TcpMeasType.MEAS_TYPE_NONE;
                        byte        MeasCode = 0;

                        if (arrayParse[Message.MessageCommandIndex] == (byte)Message.MessageCodePLC.GetCurStationState)  //根据命令码区分消息类型
                        {
                            MeasType = TcpMeasType.MEAS_TYPE_PLC;
                            MeasCode = arrayParse[Message.MessageCommandIndex];
                        }

                        TcpMeas TempMeas = new TcpMeas();
                        if (TempMeas != null)
                        {
                            TempMeas.Client   = Client;
                            TempMeas.MeasType = MeasType;
                            TempMeas.MeasCode = MeasCode;
                            Array.Copy(RecvBytes, TempMeas.Param, TempMeas.Param.Length);
                            m_RecvMeasQueue.Enqueue(TempMeas);
                        }
                    }
                    else  //校验和错误,则更新错误码后发回
                    {
                        arrayParse[Message.MessageStateIndex] = Message.MessErrorState;
                        using (NetworkStream stream = Client.GetStream())
                        {
                            stream.Write(arrayParse, 0, arrayParse.Length);
                        }
                    }
                }
            }
        }