/// <summary> /// 根据类型地址以及需要写入的数据来生成指令头 /// </summary> /// <param name="address">起始地址</param> /// <param name="value">实际的数据信息</param> /// <returns>带有成功标志的指令数据</returns> public static OperateResult <byte[]> BuildWriteWordCommand(string address, byte[] value) { var addressResult = FxCalculateWordStartAddress(address); if (!addressResult.IsSuccess) { return(OperateResult.CreateFailedResult <byte[]>(addressResult)); } // 字节数据转换成ASCII格式 if (value != null) { value = MelsecHelper.BuildBytesFromData(value); } ushort startAddress = addressResult.Content; byte[] _PLCCommand = new byte[11 + value.Length]; _PLCCommand[0] = 0x02; // STX _PLCCommand[1] = 0x31; // Read _PLCCommand[2] = MelsecHelper.BuildBytesFromData(startAddress)[0]; // Offect Address _PLCCommand[3] = MelsecHelper.BuildBytesFromData(startAddress)[1]; _PLCCommand[4] = MelsecHelper.BuildBytesFromData(startAddress)[2]; _PLCCommand[5] = MelsecHelper.BuildBytesFromData(startAddress)[3]; _PLCCommand[6] = MelsecHelper.BuildBytesFromData((byte)(value.Length / 2))[0]; // Read Length _PLCCommand[7] = MelsecHelper.BuildBytesFromData((byte)(value.Length / 2))[1]; Array.Copy(value, 0, _PLCCommand, 8, value.Length); _PLCCommand[_PLCCommand.Length - 3] = 0x03; // ETX MelsecHelper.FxCalculateCRC(_PLCCommand).CopyTo(_PLCCommand, _PLCCommand.Length - 2); // CRC return(OperateResult.CreateSuccessResult(_PLCCommand)); }
/// <summary> /// 根据类型地址长度确认需要读取的指令头 /// </summary> /// <param name="address">起始地址</param> /// <param name="length">长度</param> /// <returns>带有成功标志的指令数据</returns> public static OperateResult <byte[]> BuildReadWordCommand(string address, ushort length) { var addressResult = FxCalculateWordStartAddress(address); if (!addressResult.IsSuccess) { return(OperateResult.CreateFailedResult <byte[]>(addressResult)); } length = (ushort)(length * 2); ushort startAddress = addressResult.Content; byte[] _PLCCommand = new byte[11]; _PLCCommand[0] = 0x02; // STX _PLCCommand[1] = 0x30; // Read _PLCCommand[2] = MelsecHelper.BuildBytesFromData(startAddress)[0]; // 偏移地址 _PLCCommand[3] = MelsecHelper.BuildBytesFromData(startAddress)[1]; _PLCCommand[4] = MelsecHelper.BuildBytesFromData(startAddress)[2]; _PLCCommand[5] = MelsecHelper.BuildBytesFromData(startAddress)[3]; _PLCCommand[6] = MelsecHelper.BuildBytesFromData((byte)length)[0]; // 读取长度 _PLCCommand[7] = MelsecHelper.BuildBytesFromData((byte)length)[1]; _PLCCommand[8] = 0x03; // ETX MelsecHelper.FxCalculateCRC(_PLCCommand).CopyTo(_PLCCommand, 9); // CRC return(OperateResult.CreateSuccessResult(_PLCCommand)); // Return }
/// <summary> /// 根据类型地址长度确认需要读取的指令头 /// </summary> /// <param name="address">起始地址</param> /// <param name="length">bool数组长度</param> /// <returns>带有成功标志的指令数据</returns> public static OperateResult <byte[], int> BuildReadBoolCommand(string address, ushort length) { var addressResult = FxCalculateBoolStartAddress(address); if (!addressResult.IsSuccess) { return(OperateResult.CreateFailedResult <byte[], int>(addressResult)); } // 计算下实际需要读取的数据长度 ushort length2 = (ushort)((addressResult.Content2 + length - 1) / 8 - (addressResult.Content2 / 8) + 1); ushort startAddress = addressResult.Content1; byte[] _PLCCommand = new byte[11]; _PLCCommand[0] = 0x02; // STX _PLCCommand[1] = 0x30; // Read _PLCCommand[2] = MelsecHelper.BuildBytesFromData(startAddress)[0]; // 偏移地址 _PLCCommand[3] = MelsecHelper.BuildBytesFromData(startAddress)[1]; _PLCCommand[4] = MelsecHelper.BuildBytesFromData(startAddress)[2]; _PLCCommand[5] = MelsecHelper.BuildBytesFromData(startAddress)[3]; _PLCCommand[6] = MelsecHelper.BuildBytesFromData((byte)length2)[0]; // 读取长度 _PLCCommand[7] = MelsecHelper.BuildBytesFromData((byte)length2)[1]; _PLCCommand[8] = 0x03; // ETX MelsecHelper.FxCalculateCRC(_PLCCommand).CopyTo(_PLCCommand, 9); // CRC return(OperateResult.CreateSuccessResult(_PLCCommand, (int)addressResult.Content3)); }
/// <summary> /// 生成位写入的数据报文信息,该报文可直接用于发送串口给PLC /// </summary> /// <param name="address">地址信息,每个地址存在一定的范围,需要谨慎传入数据。举例:M10,S10,X5,Y10,C10,T10</param> /// <param name="value"><c>True</c>或是<c>False</c></param> /// <returns>带报文信息的结果对象</returns> public static OperateResult <byte[]> BuildWriteBoolPacket(string address, bool value) { var analysis = FxAnalysisAddress(address); if (!analysis.IsSuccess) { return(OperateResult.CreateFailedResult <byte[]>(analysis)); } // 二次运算起始地址偏移量,根据类型的不同,地址的计算方式不同 ushort startAddress = analysis.Content2; if (analysis.Content1 == MelsecMcDataType.M) { if (startAddress >= 8000) { startAddress = (ushort)(startAddress - 8000 + 0x0F00); } else { startAddress = (ushort)(startAddress + 0x0800); } } else if (analysis.Content1 == MelsecMcDataType.S) { startAddress = (ushort)(startAddress + 0x0000); } else if (analysis.Content1 == MelsecMcDataType.X) { startAddress = (ushort)(startAddress + 0x0400); } else if (analysis.Content1 == MelsecMcDataType.Y) { startAddress = (ushort)(startAddress + 0x0500); } else if (analysis.Content1 == MelsecMcDataType.CS) { startAddress += (ushort)(startAddress + 0x01C0); } else if (analysis.Content1 == MelsecMcDataType.CC) { startAddress += (ushort)(startAddress + 0x03C0); } else if (analysis.Content1 == MelsecMcDataType.CN) { startAddress += (ushort)(startAddress + 0x0E00); } else if (analysis.Content1 == MelsecMcDataType.TS) { startAddress += (ushort)(startAddress + 0x00C0); } else if (analysis.Content1 == MelsecMcDataType.TC) { startAddress += (ushort)(startAddress + 0x02C0); } else if (analysis.Content1 == MelsecMcDataType.TN) { startAddress += (ushort)(startAddress + 0x0600); } else { return(new OperateResult <byte[]>(StringResources.Language.MelsecCurrentTypeNotSupportedBitOperate)); } byte[] _PLCCommand = new byte[9]; _PLCCommand[0] = 0x02; // STX _PLCCommand[1] = value ? (byte)0x37 : (byte)0x38; // Read _PLCCommand[2] = MelsecHelper.BuildBytesFromData(startAddress)[2]; // 偏移地址 _PLCCommand[3] = MelsecHelper.BuildBytesFromData(startAddress)[3]; _PLCCommand[4] = MelsecHelper.BuildBytesFromData(startAddress)[0]; _PLCCommand[5] = MelsecHelper.BuildBytesFromData(startAddress)[1]; _PLCCommand[6] = 0x03; // ETX MelsecHelper.FxCalculateCRC(_PLCCommand).CopyTo(_PLCCommand, 7); // CRC return(OperateResult.CreateSuccessResult(_PLCCommand)); }