/// <summary> /// 检查当前的Modbus-Ascii响应是否是正确的 /// </summary> /// <param name="send">发送的数据信息</param> /// <returns>带是否成功的结果数据</returns> protected override OperateResult <byte[]> CheckModbusTcpResponse(byte[] send) { // 转ascii byte[] modbus_ascii = ModbusInfo.TransRtuToAsciiPackCommand(send); // 核心交互 OperateResult <byte[]> result = ReadBase(modbus_ascii); if (!result.IsSuccess) { return(result); } // 还原modbus报文 OperateResult <byte[]> modbus_core = ModbusInfo.TransAsciiPackCommandToRtu(result.Content); if (!modbus_core.IsSuccess) { return(modbus_core); } // 发生了错误 if ((send[1] + 0x80) == modbus_core.Content[1]) { return(new OperateResult <byte[]>(modbus_core.Content[2], ModbusInfo.GetDescriptionByErrorCode(modbus_core.Content[2]))); } // 成功的消息 return(OperateResult.CreateSuccessResult(modbus_core.Content)); }
/// <summary> /// 检查当前的Modbus-Tcp响应是否是正确的 /// </summary> /// <param name="send">发送的数据信息</param> /// <returns>带是否成功的结果数据</returns> private OperateResult <byte[]> CheckModbusTcpResponse(byte[] send) { OperateResult <byte[]> resultBytes = ReadFromCoreServer(send); if (resultBytes.IsSuccess) { if ((send[7] + 0x80) == resultBytes.Content[7]) { // 发生了错误 resultBytes.IsSuccess = false; resultBytes.Message = ModbusInfo.GetDescriptionByErrorCode(resultBytes.Content[8]); resultBytes.ErrorCode = resultBytes.Content[8]; } } return(resultBytes); }
/// <summary> /// 检查当前的Modbus-Rtu响应是否是正确的 /// </summary> /// <param name="send">发送的数据信息</param> /// <returns>带是否成功的结果数据</returns> protected virtual OperateResult <byte[]> CheckModbusTcpResponse(byte[] send) { // 核心交互 OperateResult <byte[]> result = ReadBase(send); if (!result.IsSuccess) { return(result); } // 长度校验 if (result.Content.Length < 5) { return(new OperateResult <byte[]>(StringResources.Language.ReceiveDataLengthTooShort + "5")); } // 检查crc if (!SoftCRC16.CheckCRC16(result.Content)) { return(new OperateResult <byte[]>(StringResources.Language.ModbusCRCCheckFailed)); } // 发生了错误 if ((send[1] + 0x80) == result.Content[1]) { return(new OperateResult <byte[]>(result.Content[2], ModbusInfo.GetDescriptionByErrorCode(result.Content[2]))); } if (send[1] != result.Content[1]) { return(new OperateResult <byte[]>(result.Content[1], $"Receive Command Check Failed: ")); } // 移除CRC校验 byte[] buffer = new byte[result.Content.Length - 2]; Array.Copy(result.Content, 0, buffer, 0, buffer.Length); return(OperateResult.CreateSuccessResult(buffer)); }