/// <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> /// Determine i there is any error in communication /// </summary> /// <returns>Is Error?</returns> private Errors_t IsError(string command) { Errors_t error = Errors_t.NoError; if (command[3] == errorFlag) { error = (Errors_t)(Array.IndexOf(errorWords, command[4].ToString()) + 1); } return(error); }