From bfc9c93c803754d21754eafbfd06be979a364084 Mon Sep 17 00:00:00 2001 From: Peter Butzhammer Date: Fri, 9 Feb 2024 10:26:54 +0100 Subject: [PATCH] improve error messages --- Sharp7.Rx/Sharp7Connector.cs | 38 +++++++++++++++--------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/Sharp7.Rx/Sharp7Connector.cs b/Sharp7.Rx/Sharp7Connector.cs index 3fca9a7..843794d 100644 --- a/Sharp7.Rx/Sharp7Connector.cs +++ b/Sharp7.Rx/Sharp7Connector.cs @@ -104,11 +104,8 @@ internal class Sharp7Connector : IS7Connector .ToArray(); var result = await Task.Factory.StartNew(() => s7MultiVar.Read(), CancellationToken.None, TaskCreationOptions.None, scheduler); - if (result != 0) - { - var errorText = EvaluateErrorCode(result); - throw new S7CommunicationException($"Error in MultiVar request for variables: {string.Join(",", variableNames)} ({errorText})", result, errorText); - } + + EnsureSuccessOrThrow(result, $"Error in MultiVar request for variables: {string.Join(",", variableNames)}"); return buffers.ToDictionary(arg => arg.VariableName, arg => arg.Buffer); } @@ -150,11 +147,7 @@ internal class Sharp7Connector : IS7Connector await Task.Factory.StartNew(() => sharp7.ReadArea(operand.ToArea(), dbNo, startByteAddress, bytesToRead, S7WordLength.Byte, buffer), token, TaskCreationOptions.None, scheduler); token.ThrowIfCancellationRequested(); - if (result != 0) - { - var errorText = EvaluateErrorCode(result); - throw new S7CommunicationException($"Error reading {operand}{dbNo}:{startByteAddress}->{bytesToRead} ({errorText})", result, errorText); - } + EnsureSuccessOrThrow(result, $"Error reading {operand}{dbNo}:{startByteAddress}->{bytesToRead}"); return buffer; } @@ -170,11 +163,7 @@ internal class Sharp7Connector : IS7Connector var result = await Task.Factory.StartNew(() => sharp7.WriteArea(operand.ToArea(), dbNo, offsetStart, 1, S7WordLength.Bit, buffer), token, TaskCreationOptions.None, scheduler); token.ThrowIfCancellationRequested(); - if (result != 0) - { - var errorText = EvaluateErrorCode(result); - throw new S7CommunicationException($"Error writing {operand}{dbNo}:{startByteAddress} bit {bitAdress} ({errorText})", result, errorText); - } + EnsureSuccessOrThrow(result, $"Error writing {operand}{dbNo}:{startByteAddress} bit {bitAdress}"); } public async Task WriteBytes(Operand operand, ushort startByteAddress, byte[] data, ushort dbNo, CancellationToken token) @@ -184,11 +173,16 @@ internal class Sharp7Connector : IS7Connector var result = await Task.Factory.StartNew(() => sharp7.WriteArea(operand.ToArea(), dbNo, startByteAddress, data.Length, S7WordLength.Byte, data), token, TaskCreationOptions.None, scheduler); token.ThrowIfCancellationRequested(); - if (result != 0) - { - var errorText = EvaluateErrorCode(result); - throw new S7CommunicationException($"Error writing {operand}{dbNo}:{startByteAddress}.{data.Length} ({errorText})", result, errorText); - } + EnsureSuccessOrThrow(result, $"Error writing {operand}{dbNo}:{startByteAddress}.{data.Length}"); + } + + private void EnsureSuccessOrThrow(int result, string message) + { + if (result == 0) return; + + var errorText = EvaluateErrorCode(result); + // 0x40000: Maybe the DB is optimized or PUT/GET communication is not enabled. + throw new S7CommunicationException($"{message} ({errorText})", result, errorText); } @@ -243,8 +237,8 @@ internal class Sharp7Connector : IS7Connector if (sharp7 == null) throw new InvalidOperationException("S7 driver is not initialized."); - var errorText = sharp7.ErrorText(errorCode); - Logger?.LogError($"Error Code {errorCode} {errorText}"); + var errorText = $"0x{errorCode:X}: {sharp7.ErrorText(errorCode)}"; + Logger?.LogError($"S7 Error {errorText}"); if (S7ErrorCodes.AssumeConnectionLost(errorCode)) SetConnectionLostState();