/// <summary> /// 根据类型地址长度确认需要读取的指令头 /// </summary> /// <param name="address">起始地址</param> /// <param name="length">长度</param> /// <param name="isBit">指示是否按照位成批的读出</param> /// <param name="plcNumber">PLC编号</param> /// <returns>带有成功标志的指令数据</returns> public static OperateResult <byte[]> BuildReadCommand(string address, ushort length, bool isBit, byte plcNumber) { var analysis = MelsecHelper.McA1EAnalysisAddress(address); if (!analysis.IsSuccess) { return(OperateResult.CreateFailedResult <byte[]>(analysis)); } // 默认信息----注意:高低字节交错 // byte subtitle = analysis.Content1.DataType == 0x01 ? (byte)0x00 : (byte)0x01; byte subtitle = isBit ? (byte)0x00 : (byte)0x01; byte[] _PLCCommand = new byte[12]; _PLCCommand[0] = subtitle; // 副标题 _PLCCommand[1] = plcNumber; // PLC号 _PLCCommand[2] = 0x0A; // CPU监视定时器(L)这里设置为0x00,0x0A,等待CPU返回的时间为10*250ms=2.5秒 _PLCCommand[3] = 0x00; // CPU监视定时器(H) _PLCCommand[4] = (byte)(analysis.Content2 % 256); // 起始软元件(开始读取的地址) _PLCCommand[5] = (byte)(analysis.Content2 / 256); _PLCCommand[6] = 0x00; _PLCCommand[7] = 0x00; _PLCCommand[8] = analysis.Content1.DataCode[1]; // 软元件代码(L) _PLCCommand[9] = analysis.Content1.DataCode[0]; // 软元件代码(H) _PLCCommand[10] = (byte)(length % 256); // 软元件点数 _PLCCommand[11] = 0x00; return(OperateResult.CreateSuccessResult(_PLCCommand)); }
/// <summary> /// 根据类型地址以及需要写入的数据来生成指令头 /// </summary> /// <param name="address">起始地址</param> /// <param name="value">数据值</param> /// <param name="plcNumber">PLC编号</param> /// <returns>带有成功标志的指令数据</returns> public static OperateResult <byte[]> BuildWriteCommand(string address, byte[] value, byte plcNumber) { var analysis = MelsecHelper.McA1EAnalysisAddress(address); if (!analysis.IsSuccess) { return(OperateResult.CreateFailedResult <byte[]>(analysis)); } int length = -1; if (analysis.Content1.DataType == 1) { // 按照位写入的操作,数据需要重新计算 length = value.Length; value = MelsecHelper.TransBoolArrayToByteData(value); } // 默认信息----注意:高低字节交错 byte subtitle = analysis.Content1.DataType == 0x01 ? (byte)0x02 : (byte)0x03; byte[] _PLCCommand = new byte[12 + value.Length]; _PLCCommand[0] = subtitle; // 副标题 _PLCCommand[1] = plcNumber; // PLC号 _PLCCommand[2] = 0x0A; // CPU监视定时器(L)这里设置为0x00,0x0A,等待CPU返回的时间为10*250ms=2.5秒 _PLCCommand[3] = 0x00; // CPU监视定时器(H) _PLCCommand[4] = (byte)(analysis.Content2 % 256); // 起始软元件(开始读取的地址) _PLCCommand[5] = (byte)(analysis.Content2 / 256); _PLCCommand[6] = 0x00; _PLCCommand[7] = 0x00; _PLCCommand[8] = analysis.Content1.DataCode[1]; // 软元件代码(L) _PLCCommand[9] = analysis.Content1.DataCode[0]; // 软元件代码(H) _PLCCommand[10] = (byte)(length % 256); // 软元件点数 _PLCCommand[11] = 0x00; // 判断是否进行位操作 if (analysis.Content1.DataType == 0x01) { if (length > 0) { _PLCCommand[10] = (byte)(length % 256); // 软元件点数 } else { _PLCCommand[10] = (byte)(value.Length * 2 % 256); // 软元件点数 } } else { _PLCCommand[10] = (byte)(value.Length / 2 % 256); // 软元件点数 } Array.Copy(value, 0, _PLCCommand, 12, value.Length); // 将具体的要写入的数据附加到写入命令后面 return(OperateResult.CreateSuccessResult(_PLCCommand)); }