/// <summary> /// Read data from MCU /// </summary> /// <param name="commandName">Name of command</param> /// <param name="readValue">Data read from MCU</param> /// <returns></returns> public Errors_t ReadData(Commands_t commandName, out float readValue) { // To avoid conflict CheckSP(); // All command can be read string command = ConstructCommand(commandName, 0.0f, false); #region Serial Port Open Section this.sp.Open(); this.sp.Write(command); string commandBack = this.ReadSP(); this.sp.Close(); #endregion Errors_t error = IsError(commandBack); if (error != Errors_t.NoError) { readValue = 0.0f; } else { readValue = float.Parse(commandBack.Substring(5)); } return(error); }
/// <summary> /// Send data to MCU /// </summary> /// <param name="commandName">Name of ommand</param> /// <param name="value">Value of Command</param> public Errors_t SendData(Commands_t commandName, float value) { // To avoid conflict CheckSP(); // Improve: Check if can remove this and commandRW when development finishes. // If the command cannot be write, throw an exception if (cmdRW[(int)commandName] != "w") { Exception e = new Exception (String.Format("This parameter {0} cannot be write", Enum.GetName(commandName.GetType(), commandName))); throw e; } string command = ConstructCommand(commandName, value, true); #region Serial Port Open Section this.sp.Open(); this.sp.Write(command); Errors_t error = IsError(this.ReadSP()); this.sp.Close(); #endregion return(error); }
/// <summary> /// Construct command using given name and value /// </summary> /// <param name="commandName">Name of command</param> /// <param name="value">Value of command</param> /// <param name="W_R">True for write, false for read</param> /// <returns></returns> private string ConstructCommand(Commands_t commandName, float value, bool W_R) { string command = ""; if (W_R) { command += cmdHead_W; command += cmdWords[(int)commandName]; command += value.ToString(cmdFormats[(int)commandName]); command += cmdFinish; command += BCCCal(command, false); command += cmdEnd; } else { command += cmdHead_R; command += cmdWords[(int)commandName]; command += cmdFinish; command += BCCCal(command, false); command += cmdEnd; } return(command); }