コード例 #1
0
ファイル: TCP.cs プロジェクト: liushuiruobing/wpf_project_w
        public void ParseAndAddMessageToQueue(byte[] RecvBytes, int RecvCount, TcpClient Client, ref int parseCount, byte[] arrayParse)
        {
            //匹配比较数组, -1表示不需要比较,忽略
            int[] arrayCompare = new int[CommunicationProtocol.MessageLength]
            {
                CommunicationProtocol.MessStartCode, CommunicationProtocol.MessVID1, CommunicationProtocol.MessVID2, -1, CommunicationProtocol.MessRightState, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, CommunicationProtocol.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 >= CommunicationProtocol.MessageLength)
                {
                    parseCount = 0;                                                       //和校验

                    if (MyMath.CheckSum(arrayParse, CommunicationProtocol.MessageLength)) //分析数据,把数据添加到队列m_TcpMeas
                    {
                        TcpMeasType MeasType = TcpMeasType.MEAS_TYPE_CONTROLER_BOARD;
                        byte        MeasCode = arrayParse[CommunicationProtocol.MessageCommandIndex];
                        TcpMeas     TempMeas = new TcpMeas();
                        if (TempMeas != null)
                        {
                            TempMeas.Client   = Client;
                            TempMeas.MeasType = MeasType;
                            TempMeas.MeasCode = MeasCode;
                            Array.Copy(arrayParse, TempMeas.Param, TempMeas.Param.Length);
                            m_RecvMeasQueue.Enqueue(TempMeas);
                        }

                        Array.Clear(arrayParse, 0, arrayParse.Length);
                    }
                    else  //校验和错误,则更新错误码后发回
                    {
                        arrayParse[CommunicationProtocol.MessageStateIndex] = CommunicationProtocol.MessErrorState;
                        arrayParse[CommunicationProtocol.MessageSumCheck]   = 0x00;
                        arrayParse[CommunicationProtocol.MessageSumCheck]   = MyMath.CalculateSum(arrayParse, CommunicationProtocol.MessageLength);

                        Client.GetStream().Write(arrayParse, 0, arrayParse.Length);
                    }
                }
            }
        }
コード例 #2
0
        //发送指令

        //处理视觉消息
        public void ProcessVisualMessage(TcpMeas meassage)
        {
            if (meassage != null)
            {
                CommandCode Code = (CommandCode)meassage.MeasCode;
                switch (Code)
                {
                case CommandCode.GetVisualState:
                {
                }
                break;

                default:
                    break;
                }
            }
        }