diff --git a/Sharp7.Rx/Sharp7Connector.cs b/Sharp7.Rx/Sharp7Connector.cs index 843794d..e7b4c52 100644 --- a/Sharp7.Rx/Sharp7Connector.cs +++ b/Sharp7.Rx/Sharp7Connector.cs @@ -12,6 +12,18 @@ namespace Sharp7.Rx; internal class Sharp7Connector : IS7Connector { + private static readonly IReadOnlyDictionary additionalErrorTexts = new Dictionary + { + {0xC00000, "This happens when the DB does not exist."}, + {0x900000, "This happens when the DB is not long enough."}, + { + 0x40000, """ + This error occurs when the DB is "optimized" or "PUT/GET communication" is not enabled. + See https://snap7.sourceforge.net/snap7_client.html#target_compatibility. + """ + } + }; + private readonly BehaviorSubject connectionStateSubject = new(Enums.ConnectionState.Initial); private readonly int cpuSlotNr; @@ -176,15 +188,6 @@ internal class Sharp7Connector : IS7Connector 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); - } - protected virtual void Dispose(bool disposing) { @@ -229,6 +232,19 @@ internal class Sharp7Connector : IS7Connector throw new InvalidOperationException("Plc is not connected"); } + private void EnsureSuccessOrThrow(int result, string message) + { + if (result == 0) return; + + var errorText = EvaluateErrorCode(result); + var completeMessage = $"{message}: {errorText}"; + + if (additionalErrorTexts.TryGetValue(result, out var additionalErrorText)) + completeMessage += Environment.NewLine + additionalErrorText; + + throw new S7CommunicationException(completeMessage, result, errorText); + } + private string EvaluateErrorCode(int errorCode) { if (errorCode == 0) @@ -237,7 +253,7 @@ internal class Sharp7Connector : IS7Connector if (sharp7 == null) throw new InvalidOperationException("S7 driver is not initialized."); - var errorText = $"0x{errorCode:X}: {sharp7.ErrorText(errorCode)}"; + var errorText = $"0x{errorCode:X}, {sharp7.ErrorText(errorCode)}"; Logger?.LogError($"S7 Error {errorText}"); if (S7ErrorCodes.AssumeConnectionLost(errorCode)) diff --git a/Sharp7.Rx/Sharp7Plc.cs b/Sharp7.Rx/Sharp7Plc.cs index 501b578..904294b 100644 --- a/Sharp7.Rx/Sharp7Plc.cs +++ b/Sharp7.Rx/Sharp7Plc.cs @@ -102,15 +102,6 @@ public class Sharp7Plc : IPlc }); } - private S7VariableAddress ParseAndVerify(string variableName, Type type) - { - var address = varaibleNameParser.Parse(variableName); - if (!address.MatchesType(type)) - throw new DataTypeMissmatchException($"Address \"{variableName}\" does not match type {type}.", type, address); - - return address; - } - public Task GetValue(string variableName) { return GetValue(variableName, CancellationToken.None); @@ -222,6 +213,15 @@ public class Sharp7Plc : IPlc return Unit.Default; } + private S7VariableAddress ParseAndVerify(string variableName, Type type) + { + var address = varaibleNameParser.Parse(variableName); + if (!address.MatchesType(type)) + throw new DataTypeMissmatchException($"Address \"{variableName}\" does not match type {type}.", type, address); + + return address; + } + private void PrintAndResetPerformanceStatistik() { if (performanceCoutner.Count == performanceCoutner.Capacity)