public void SendCommandToRunAxisByTransportType(Axis axis, TransportType type, int steps) { int BoardIndex = 0; int AxisIndex = 0; GetMotorBoardAndAxisIndexByAxisType(axis, ref BoardIndex, ref AxisIndex); if (!IsControlerConnected((Board)BoardIndex)) { return; } byte TransportTypeCode = (byte)ControlerCommandCode.SetAxisStepsRef; if (type == TransportType.Absolute) { TransportTypeCode = (byte)ControlerCommandCode.SetAxisStepsAbs; } byte[] temp = new byte[CommunicationProtocol.MessageLength]; CommunicationProtocol.MakeSendArrayByCode(TransportTypeCode, ref temp); int DataIndex = CommunicationProtocol.MessageCommandIndex + 1; temp[DataIndex] = (byte)AxisIndex; temp[DataIndex + 1] = (byte)(steps & 0xffU); temp[DataIndex + 2] = (byte)((steps >> 8) & 0xffU); temp[DataIndex + 3] = (byte)((steps >> 16) & 0xffU); temp[DataIndex + 4] = (byte)((steps >> 24) & 0xffU); temp[CommunicationProtocol.MessageSumCheck] = 0x00; temp[CommunicationProtocol.MessageSumCheck] = MyMath.CalculateSum(temp, CommunicationProtocol.MessageLength); AddCommandMessageToQueue((Board)BoardIndex, (ControlerCommandCode)TransportTypeCode, ref temp); }
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); } } } }
public void SendCommandToRunAxis(Axis axis, ControlerCommandCode Code, AxisDir Dir) { int BoardIndex = 0; int AxisIndex = 0; GetMotorBoardAndAxisIndexByAxisType(axis, ref BoardIndex, ref AxisIndex); if (!IsControlerConnected((Board)BoardIndex)) { return; } byte[] temp = new byte[CommunicationProtocol.MessageLength]; CommunicationProtocol.MakeSendArrayByCode((byte)Code, ref temp); temp[CommunicationProtocol.MessageCommandIndex + 1] = (byte)AxisIndex; if (Dir == AxisDir.Forward) { temp[CommunicationProtocol.MessageCommandIndex + 2] = 0x00; } else { temp[CommunicationProtocol.MessageCommandIndex + 2] = 0x01; } temp[CommunicationProtocol.MessageSumCheck] = 0x00; temp[CommunicationProtocol.MessageSumCheck] = MyMath.CalculateSum(temp, CommunicationProtocol.MessageLength); AddCommandMessageToQueue((Board)BoardIndex, Code, ref temp); }
public bool SendCommandToSetSpeedParam(Axis axis, int velLow, int velHigh, int acc, int dec, bool Default) { int BoardIndex = 0; int AxisIndex = 0; GetMotorBoardAndAxisIndexByAxisType(axis, ref BoardIndex, ref AxisIndex); if (!IsControlerConnected((Board)BoardIndex)) { return(false); } ControlerCommandCode Code = Default ? ControlerCommandCode.SetAxisParametersDefault : ControlerCommandCode.SetAxisParameters; byte[] temp = new byte[CommunicationProtocol.MessageLength]; CommunicationProtocol.MakeSendArrayByCode((byte)Code, ref temp); int DataIndex = CommunicationProtocol.MessageCommandIndex + 1; temp[DataIndex] = (byte)AxisIndex; temp[DataIndex + 1] = (byte)(velLow & 0xffU); temp[DataIndex + 2] = (byte)((velLow >> 8) & 0xffU); temp[DataIndex + 3] = (byte)((velLow >> 16) & 0xffU); temp[DataIndex + 4] = (byte)((velLow >> 24) & 0xffU); temp[DataIndex + 5] = (byte)(velHigh & 0xffU); temp[DataIndex + 6] = (byte)((velHigh >> 8) & 0xffU); temp[DataIndex + 7] = (byte)((velHigh >> 16) & 0xffU); temp[DataIndex + 8] = (byte)((velHigh >> 24) & 0xffU); temp[DataIndex + 9] = (byte)(acc & 0xffU); temp[DataIndex + 10] = (byte)((acc >> 8) & 0xffU); temp[DataIndex + 11] = (byte)((acc >> 16) & 0xffU); temp[DataIndex + 12] = (byte)((acc >> 24) & 0xffU); temp[DataIndex + 13] = (byte)(dec & 0xffU); temp[DataIndex + 14] = (byte)((dec >> 8) & 0xffU); temp[DataIndex + 15] = (byte)((dec >> 16) & 0xffU); temp[DataIndex + 16] = (byte)((dec >> 24) & 0xffU); temp[CommunicationProtocol.MessageSumCheck] = 0x00; temp[CommunicationProtocol.MessageSumCheck] = MyMath.CalculateSum(temp, CommunicationProtocol.MessageLength); AddCommandMessageToQueue((Board)BoardIndex, ControlerCommandCode.SetAxisParametersDefault, ref temp); return(true); }
public void SendCommandToSetControlBoardOutput(IO_OUT_Type Io, IOValue Value) { int BoardIndex = 0; int IoOutIndex = 0; GetIoOutBoardAndAxisIndexByIO_OUT_Type(Io, ref BoardIndex, ref IoOutIndex); if (!IsControlerConnected((Board)BoardIndex)) { return; } uint outputBuffer = 0; //输出口数据 uint outputEnable = 0; //输出口使能 if (Value == IOValue.High) { outputBuffer |= ((uint)1 << IoOutIndex); } outputEnable |= ((uint)1 << IoOutIndex); byte[] temp = new byte[CommunicationProtocol.MessageLength]; CommunicationProtocol.MakeSendArrayByCode((byte)ControlerCommandCode.SetOutput, ref temp); const int DataIndex = CommunicationProtocol.MessageCommandIndex + 1; temp[DataIndex + 0] = (byte)(outputEnable & 0xffU); temp[DataIndex + 1] = (byte)((outputEnable >> 8) & 0xffU); temp[DataIndex + 2] = (byte)((outputEnable >> 16) & 0xffU); temp[DataIndex + 3] = (byte)((outputEnable >> 24) & 0xffU); temp[DataIndex + 4] = (byte)(outputBuffer & 0xffU); temp[DataIndex + 5] = (byte)((outputBuffer >> 8) & 0xffU); temp[DataIndex + 6] = (byte)((outputBuffer >> 16) & 0xffU); temp[DataIndex + 7] = (byte)((outputBuffer >> 24) & 0xffU); temp[CommunicationProtocol.MessageSumCheck] = 0x00; temp[CommunicationProtocol.MessageSumCheck] = MyMath.CalculateSum(temp, CommunicationProtocol.MessageLength); AddCommandMessageToQueue((Board)BoardIndex, ControlerCommandCode.SetOutput, ref temp); }
public void SendCommandToSetControlBoardOutputByInput(IO_IN_Type Io_In, int Value, IO_OUT_Type Io_Out1, int Out1_Value, IO_OUT_Type Io_Out2, int Out2_Value) { int BoardIndex = 0; int IoInIndex = 0; GetIoInBoardAndAxisIndexByIO_IN_Type(Io_In, ref BoardIndex, ref IoInIndex); int indexBoardOut1 = 0; int IoOut1Index = 0; GetIoOutBoardAndAxisIndexByIO_OUT_Type(Io_Out1, ref indexBoardOut1, ref IoOut1Index); int indexBoardOut2 = 0; int IoOut2Index = 0; GetIoOutBoardAndAxisIndexByIO_OUT_Type(Io_Out2, ref indexBoardOut2, ref IoOut2Index); if (!IsControlerConnected((Board)BoardIndex)) { return; } byte[] temp = new byte[CommunicationProtocol.MessageLength]; CommunicationProtocol.MakeSendArrayByCode((byte)ControlerCommandCode.SetOutputByInput, ref temp); const int DataIndex = CommunicationProtocol.MessageCommandIndex + 1; temp[DataIndex + 0] = (byte)IoInIndex; temp[DataIndex + 1] = (byte)Value; temp[DataIndex + 2] = (byte)IoOut1Index; temp[DataIndex + 3] = (byte)Out1_Value; temp[DataIndex + 4] = (byte)IoOut2Index; temp[DataIndex + 5] = (byte)Out2_Value; temp[CommunicationProtocol.MessageSumCheck] = 0x00; temp[CommunicationProtocol.MessageSumCheck] = MyMath.CalculateSum(temp, CommunicationProtocol.MessageLength); AddCommandMessageToQueue((Board)BoardIndex, ControlerCommandCode.SetOutputByInput, ref temp); }
public void SendCommandToControlerWithAxis(Axis axis, ControlerCommandCode Code) { if (axis >= Axis.Total) { return; } int BoardIndex = 0; int AxisIndex = 0; GetMotorBoardAndAxisIndexByAxisType(axis, ref BoardIndex, ref AxisIndex); if (IsControlerConnected((Board)BoardIndex)) { byte[] temp = new byte[CommunicationProtocol.MessageLength]; CommunicationProtocol.MakeSendArrayByCode((byte)Code, ref temp); temp[CommunicationProtocol.MessageCommandIndex + 1] = (byte)AxisIndex; temp[CommunicationProtocol.MessageSumCheck] = 0x00; temp[CommunicationProtocol.MessageSumCheck] = MyMath.CalculateSum(temp, CommunicationProtocol.MessageLength); AddCommandMessageToQueue((Board)BoardIndex, Code, ref temp); } }