public void FromString(string OriginString) { var List = OriginString.Replace("]", "").Replace("[", "").Replace("\"", "").Split(','); if (List.Count() != 7) { throw new Exception("Wrong number para recieved!"); } string strCmd = List[0]; if (int.TryParse(strCmd, out int intCmd)) { if (Enum.IsDefined(typeof(EnumRobotCmd), int.Parse(strCmd))) { I_Cmd = (EnumRobotCmd)int.Parse(strCmd); for (int i = 0; i < 6; i++) { Paras[i] = List[i + 1]; } ReadProfile(); } else { throw new Exception($"Wrong cmd found: {I_Cmd.ToString()}"); } } else { throw new Exception("Wrong cmd found when parse intCmd"); } }
public virtual byte[] ToByteArray() { SetProfile(); string strCmd = string.Format("[\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\"]", I_Cmd.ToString(), Paras[0], Paras[1], Paras[2], Paras[3], Paras[4], Paras[5]); List <byte> ByteList = new List <byte>(); foreach (var ch in strCmd) { ByteList.Add((byte)ch); } return(ByteList.ToArray()); }