/// <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[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_NONE; byte MeasCode = 0; if (arrayParse[CommunicationProtocol.MessageCommandIndex] == (byte)CommunicationProtocol.CommandCode_PLC.GetCurStationState) //根据命令码区分消息类型 { MeasType = TcpMeasType.MEAS_TYPE_PLC; 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); } } 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); } } } }
public void AddCommandMessageToQueue(Board boardIndex, ControlerCommandCode Code, ref byte[] Command) { switch (Code) { //设置相关的指令 case ControlerCommandCode.SetOutput: case ControlerCommandCode.SetOutputByInput: case ControlerCommandCode.SetOutputDefault: case ControlerCommandCode.SetAxisParametersDefault: case ControlerCommandCode.SetAxisParameters: case ControlerCommandCode.SetAxisStepsAbs: case ControlerCommandCode.SetAxisStepsRef: case ControlerCommandCode.SetAxisMoveContinuous: case ControlerCommandCode.SetAxisStepsMax: case ControlerCommandCode.StopAxis: case ControlerCommandCode.ResetAxisError: case ControlerCommandCode.AxisGoHome: case ControlerCommandCode.SetIp: case ControlerCommandCode.SetVersionHardware: case ControlerCommandCode.ResetFactory: { if (m_MyTcpClient[(int)boardIndex].IsConnected) { m_MyTcpClient[(int)boardIndex].m_SetCommandQueue.Enqueue(Command); } } break; //“获取”相关的指令 case ControlerCommandCode.GetOutput: case ControlerCommandCode.GetInput: case ControlerCommandCode.GetAxisParameters: case ControlerCommandCode.GetAxisStepsAbs: case ControlerCommandCode.GetAxisState: case ControlerCommandCode.GetBoardInformation: { if (m_MyTcpClient[(int)boardIndex].IsConnected) { if (m_MyTcpClient[(int)boardIndex].m_GetCommandQueue.Count == 0) { m_MyTcpClient[(int)boardIndex].m_GetCommandQueue.Enqueue(Command); } else { bool AddToDequeue = true; foreach (var temp in m_MyTcpClient[(int)boardIndex].m_GetCommandQueue) { if (MyMath.IsEquals(temp, Command)) //避免加入的相同的指令过多 { AddToDequeue = false; break; } } if (AddToDequeue) { m_MyTcpClient[(int)boardIndex].m_GetCommandQueue.Enqueue(Command); } } } } break; default: break; } }