public static byte[] ConfirmConnectionInstructionOrFeedback(FunctionCodeEnum functionCodeEnum) { switch (functionCodeEnum) { case FunctionCodeEnum.ConfirmConnection_Instruction: functionCode = FunctionCodeHexString.ConfirmConnection_Instruction; break; case FunctionCodeEnum.ConfirmConnection_Feedback: functionCode = FunctionCodeHexString.ConfirmConnection_Feedback; break; default: break; } return((controllerId + functionCode).HexStringToByteArray()); }
protected AbstractModbusMessage(byte[] frameData, int functionCodeOffset) { this.functionCode = (FunctionCodeEnum)frameData[functionCodeOffset]; }
private void MSERIALPORT_TRIGGERMESSIGE(object sender, SERIALPORT.MESSIGEEventArgs e) { switch (e.MyType) { case SERIALPORT.MESSIGEEventArgs.MessageType.Warning: break; case SERIALPORT.MESSIGEEventArgs.MessageType.Error: break; case SERIALPORT.MESSIGEEventArgs.MessageType.Message: break; case SERIALPORT.MESSIGEEventArgs.MessageType.Data: if (e.MyData[0] == PRO) { //发出去的寄存器地址 string strTemp = ""; strTemp = e.SendData[2].ToString("X2") + e.SendData[3].ToString("X2"); int iRegister = Convert.ToInt32(strTemp, 16); RegisterEnum temp = (RegisterEnum)iRegister; //发出去的功能码 string strFunction = e.SendData[1].ToString("X2"); FunctionCodeEnum Function = (FunctionCodeEnum)Convert.ToInt32(strFunction); int inumber = 0; byte[] byteArray = null; if (Function == FunctionCodeEnum.MultipleReads) { try { //抓取数据回来 inumber = e.MyData[2]; byteArray = new byte[inumber]; for (int i = 0; i < inumber; i++) { byteArray[i] = e.MyData[i + 3]; } } catch (Exception ex) { MESSIGEEventArgs ee = new MESSIGEEventArgs(); ee.MyType = MESSIGEEventArgs.MessageType.Error; ee.MyMessage = ex.Message; OnTRIGGERMESSIGE(ee); } } else { return; } switch (temp) { case RegisterEnum.MotorMode: MotorModel = System.Text.Encoding.Default.GetString(byteArray).Replace("\0", ""); break; case RegisterEnum.MotorVersionHardware: MotorVersionHardware = System.Text.Encoding.Default.GetString(byteArray).Replace("\0", ""); break; case RegisterEnum.MotorVersionSoftware: MotorVersionSoftware = System.Text.Encoding.Default.GetString(byteArray).Replace("\0", ""); break; case RegisterEnum.MotorErrorCode: string error = ""; foreach (byte by in byteArray) { error += Convert.ToString(by, 2).PadLeft(8, '0'); } error = ToMotorError(error); if (error != "") { MESSIGEEventArgs ee = new MESSIGEEventArgs(); ee.MyType = MESSIGEEventArgs.MessageType.Error; ee.MyMessage = error; OnTRIGGERMESSIGE(ee); } break; case RegisterEnum.MotorPositionNow: strTemp = ""; if (byteArray.Length == 4) { for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } } MotorPositionNow = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorSpeedNow: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } MotorSpeedNow = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorObjectPosition: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } iObjectrPosition = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorObjectSpeed: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } iObjectSpeed = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorAcceleration: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } iAcceleration = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorDeceleration: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } iDeceleration = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorQuickStopDeceleration: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } iQuickStopDeceleration = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorHomeAddSubtract: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } iHomeAddSubtract = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorHomeMechanicsSpeed: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } iHomeMechanicsSpeed = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorHomeSpeed: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } iHomeSpeed = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorZeroOffset: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } iZeroOffset = Convert.ToInt32(strTemp, 16); break; case RegisterEnum.MotorState: strTemp = ""; for (int i = 0; i < byteArray.Length; i++) { strTemp += byteArray[i].ToString("X2"); } strTemp = Convert.ToString(Convert.ToInt32(strTemp, 16), 2).PadLeft(16, '0'); ToMotorState(strTemp); break; } } break; } }