public static string Make(FxCommandConst cmd, FxAddress addr, int length, string data) { Debug.Assert(length == data.Length, "length(长度)必须与给定的数据实际一致"); StringBuilder sb = new StringBuilder(64 + data.Length); sb.Append((char)FxControlCode._STX); sb.Append((char)cmd); sb.Append(addr.ToAddressHexString()); sb.Append(FxConvert.DecToHex((uint)(length / 2), 2)); sb.Append(data); sb.Append((char)FxControlCode._ETX); sb.Append(FxConvert.DecToHex(GetCheckSum(sb.ToString(), 1), 2)); return(sb.ToString()); }
/// <summary> /// 构建PLC FX命令 /// 支持各种“读数据”或“查询”类命令,支持多种数据类型,例如 UInt16DataType,UIntDataType /// </summary> /// <param name="response">命令字</param> /// <param name="addr">起始地址</param> /// <param name="length">数据长度,以字节为单位。例如:如果是16bits则为2、32bits则为4</param> /// <returns>返回构造完成的命令串</returns> public static string Make(FxCommandConst cmd, FxAddress addr, int length) { StringBuilder sb = new StringBuilder(64); sb.Append((char)FxControlCode._STX); sb.Append((char)cmd); sb.Append(addr.ToAddressHexString()); if (length > 0) { sb.Append(FxConvert.DecToHex((uint)length, 2)); } sb.Append((char)FxControlCode._ETX); sb.Append(FxConvert.DecToHex(GetCheckSum(sb.ToString(), 1), 2)); return(sb.ToString()); }