/// <summary> /// 从地址,长度,是否位读取进行创建读取Ascii格式的MC的核心报文 /// </summary> /// <param name="address">三菱的地址信息,具体格式参照<seealso cref="MelsecMcNet"/> 的注释说明</param> /// <param name="length">读取的长度信息</param> /// <param name="isBit">是否进行了位读取操作</param> /// <param name="analysisAddress">对地址分析的委托方法</param> /// <returns>带有成功标识的报文对象</returns> public static OperateResult <byte[]> BuildAsciiReadMcCoreCommand(string address, ushort length, bool isBit, Func <string, OperateResult <MelsecMcDataType, int> > analysisAddress) { OperateResult <MelsecMcDataType, int> analysis = analysisAddress(address); if (!analysis.IsSuccess) { return(OperateResult.CreateFailedResult <byte[]>(analysis)); } byte[] command = new byte[20]; command[0] = 0x30; // 批量读取数据命令 command[1] = 0x34; command[2] = 0x30; command[3] = 0x31; command[4] = 0x30; // 以点为单位还是字为单位成批读取 command[5] = 0x30; command[6] = 0x30; command[7] = isBit ? (byte)0x31 : (byte)0x30; command[8] = Encoding.ASCII.GetBytes(analysis.Content1.AsciiCode)[0]; // 软元件类型 command[9] = Encoding.ASCII.GetBytes(analysis.Content1.AsciiCode)[1]; command[10] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[0]; // 起始地址的地位 command[11] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[1]; command[12] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[2]; command[13] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[3]; command[14] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[4]; command[15] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[5]; command[16] = MelsecHelper.BuildBytesFromData(length)[0]; // 软元件点数 command[17] = MelsecHelper.BuildBytesFromData(length)[1]; command[18] = MelsecHelper.BuildBytesFromData(length)[2]; command[19] = MelsecHelper.BuildBytesFromData(length)[3]; return(OperateResult.CreateSuccessResult(command)); }
/// <summary> /// 以字为单位,创建ASCII数据写入的核心报文 /// </summary> /// <param name="address">三菱的地址信息,具体格式参照<seealso cref="MelsecMcNet"/> 的注释说明</param> /// <param name="value">实际的原始数据信息</param> /// <param name="analysisAddress">对地址分析的委托方法</param> /// <returns>带有成功标识的报文对象</returns> public static OperateResult <byte[]> BuildAsciiWriteWordCoreCommand(string address, byte[] value, Func <string, OperateResult <MelsecMcDataType, int> > analysisAddress) { OperateResult <MelsecMcDataType, int> analysis = analysisAddress(address); if (!analysis.IsSuccess) { return(OperateResult.CreateFailedResult <byte[]>(analysis)); } if (value == null) { value = new byte[0]; } byte[] buffer = new byte[value.Length * 2]; for (int i = 0; i < value.Length / 2; i++) { MelsecHelper.BuildBytesFromData(BitConverter.ToUInt16(value, i * 2)).CopyTo(buffer, 4 * i); } value = buffer; byte[] command = new byte[20 + value.Length]; command[0] = 0x31; // 批量写入的命令 command[1] = 0x34; command[2] = 0x30; command[3] = 0x31; command[4] = 0x30; // 子命令 command[5] = 0x30; command[6] = 0x30; command[7] = 0x30; command[8] = Encoding.ASCII.GetBytes(analysis.Content1.AsciiCode)[0]; // 软元件类型 command[9] = Encoding.ASCII.GetBytes(analysis.Content1.AsciiCode)[1]; command[10] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[0]; // 起始地址的地位 command[11] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[1]; command[12] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[2]; command[13] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[3]; command[14] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[4]; command[15] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[5]; command[16] = MelsecHelper.BuildBytesFromData((ushort)(value.Length / 4))[0]; // 软元件点数 command[17] = MelsecHelper.BuildBytesFromData((ushort)(value.Length / 4))[1]; command[18] = MelsecHelper.BuildBytesFromData((ushort)(value.Length / 4))[2]; command[19] = MelsecHelper.BuildBytesFromData((ushort)(value.Length / 4))[3]; value.CopyTo(command, 20); return(OperateResult.CreateSuccessResult(command)); }
/// <summary> /// 以位为单位,创建ASCII数据写入的核心报文 /// </summary> /// <param name="address">三菱的地址信息,具体格式参照<seealso cref="MelsecMcNet"/> 的注释说明</param> /// <param name="value">原始的bool数组数据</param> /// <param name="analysisAddress">对地址分析的委托方法</param> /// <returns>带有成功标识的报文对象</returns> public static OperateResult <byte[]> BuildAsciiWriteBitCoreCommand(string address, bool[] value, Func <string, OperateResult <MelsecMcDataType, int> > analysisAddress) { OperateResult <MelsecMcDataType, int> analysis = analysisAddress(address); if (!analysis.IsSuccess) { return(OperateResult.CreateFailedResult <byte[]>(analysis)); } if (value == null) { value = new bool[0]; } byte[] buffer = value.Select(m => m ? (byte)0x31 : (byte)0x30).ToArray( ); byte[] command = new byte[20 + buffer.Length]; command[0] = 0x31; // 批量写入的命令 command[1] = 0x34; command[2] = 0x30; command[3] = 0x31; command[4] = 0x30; // 子命令 command[5] = 0x30; command[6] = 0x30; command[7] = 0x31; command[8] = Encoding.ASCII.GetBytes(analysis.Content1.AsciiCode)[0]; // 软元件类型 command[9] = Encoding.ASCII.GetBytes(analysis.Content1.AsciiCode)[1]; command[10] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[0]; // 起始地址的地位 command[11] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[1]; command[12] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[2]; command[13] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[3]; command[14] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[4]; command[15] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[5]; command[16] = MelsecHelper.BuildBytesFromData((ushort)(value.Length))[0]; // 软元件点数 command[17] = MelsecHelper.BuildBytesFromData((ushort)(value.Length))[1]; command[18] = MelsecHelper.BuildBytesFromData((ushort)(value.Length))[2]; command[19] = MelsecHelper.BuildBytesFromData((ushort)(value.Length))[3]; buffer.CopyTo(command, 20); return(OperateResult.CreateSuccessResult(command)); }