mirror of
https://github.com/evopro-ag/Sharp7Reactive.git
synced 2026-02-04 07:42:53 +00:00
improve error messages
This commit is contained in:
@@ -104,11 +104,8 @@ internal class Sharp7Connector : IS7Connector
|
|||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
var result = await Task.Factory.StartNew(() => s7MultiVar.Read(), CancellationToken.None, TaskCreationOptions.None, scheduler);
|
var result = await Task.Factory.StartNew(() => s7MultiVar.Read(), CancellationToken.None, TaskCreationOptions.None, scheduler);
|
||||||
if (result != 0)
|
|
||||||
{
|
EnsureSuccessOrThrow(result, $"Error in MultiVar request for variables: {string.Join(",", variableNames)}");
|
||||||
var errorText = EvaluateErrorCode(result);
|
|
||||||
throw new S7CommunicationException($"Error in MultiVar request for variables: {string.Join(",", variableNames)} ({errorText})", result, errorText);
|
|
||||||
}
|
|
||||||
|
|
||||||
return buffers.ToDictionary(arg => arg.VariableName, arg => arg.Buffer);
|
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);
|
await Task.Factory.StartNew(() => sharp7.ReadArea(operand.ToArea(), dbNo, startByteAddress, bytesToRead, S7WordLength.Byte, buffer), token, TaskCreationOptions.None, scheduler);
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
if (result != 0)
|
EnsureSuccessOrThrow(result, $"Error reading {operand}{dbNo}:{startByteAddress}->{bytesToRead}");
|
||||||
{
|
|
||||||
var errorText = EvaluateErrorCode(result);
|
|
||||||
throw new S7CommunicationException($"Error reading {operand}{dbNo}:{startByteAddress}->{bytesToRead} ({errorText})", result, errorText);
|
|
||||||
}
|
|
||||||
|
|
||||||
return buffer;
|
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);
|
var result = await Task.Factory.StartNew(() => sharp7.WriteArea(operand.ToArea(), dbNo, offsetStart, 1, S7WordLength.Bit, buffer), token, TaskCreationOptions.None, scheduler);
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
if (result != 0)
|
EnsureSuccessOrThrow(result, $"Error writing {operand}{dbNo}:{startByteAddress} bit {bitAdress}");
|
||||||
{
|
|
||||||
var errorText = EvaluateErrorCode(result);
|
|
||||||
throw new S7CommunicationException($"Error writing {operand}{dbNo}:{startByteAddress} bit {bitAdress} ({errorText})", result, errorText);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task WriteBytes(Operand operand, ushort startByteAddress, byte[] data, ushort dbNo, CancellationToken token)
|
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);
|
var result = await Task.Factory.StartNew(() => sharp7.WriteArea(operand.ToArea(), dbNo, startByteAddress, data.Length, S7WordLength.Byte, data), token, TaskCreationOptions.None, scheduler);
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
if (result != 0)
|
EnsureSuccessOrThrow(result, $"Error writing {operand}{dbNo}:{startByteAddress}.{data.Length}");
|
||||||
{
|
|
||||||
var errorText = EvaluateErrorCode(result);
|
|
||||||
throw new S7CommunicationException($"Error writing {operand}{dbNo}:{startByteAddress}.{data.Length} ({errorText})", result, errorText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
if (sharp7 == null)
|
||||||
throw new InvalidOperationException("S7 driver is not initialized.");
|
throw new InvalidOperationException("S7 driver is not initialized.");
|
||||||
|
|
||||||
var errorText = sharp7.ErrorText(errorCode);
|
var errorText = $"0x{errorCode:X}: {sharp7.ErrorText(errorCode)}";
|
||||||
Logger?.LogError($"Error Code {errorCode} {errorText}");
|
Logger?.LogError($"S7 Error {errorText}");
|
||||||
|
|
||||||
if (S7ErrorCodes.AssumeConnectionLost(errorCode))
|
if (S7ErrorCodes.AssumeConnectionLost(errorCode))
|
||||||
SetConnectionLostState();
|
SetConnectionLostState();
|
||||||
|
|||||||
Reference in New Issue
Block a user