diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.S7/S7Driver.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.S7/S7Driver.cs
index cbf94e6b8..c6f6b83e5 100644
--- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.S7/S7Driver.cs
+++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.S7/S7Driver.cs
@@ -2322,7 +2322,17 @@ public sealed class S7Driver
// match the address's size-suffix type: DBX=bool, DBB=byte, DBW=ushort, DBD=uint.
// Our S7DataType lets the caller pass short/int/float; convert to the unsigned
// wire representation before handing off.
- var boxed = BoxValueForWrite(tag.DataType, value);
+ object boxed;
+ try { boxed = BoxValueForWrite(tag.DataType, value); }
+ catch (OverflowException ex)
+ {
+ // #986 (S-5): a client value outside the narrow type's range is a refusal, not a comms fault.
+ throw new S7ValueRangeException($"value out of range for S7 {tag.DataType}: {ex.Message}");
+ }
+ catch (Exception ex) when (ex is FormatException or InvalidCastException)
+ {
+ throw new S7WriteValueException($"value is not convertible to S7 {tag.DataType}: {ex.Message}");
+ }
await plc.WriteAsync(tag.Address, boxed, ct).ConfigureAwait(false);
}
diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.S7.Tests/S7ArrayWriteAndHealthTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.S7.Tests/S7ArrayWriteAndHealthTests.cs
index 27c5e20a6..5720708e6 100644
--- a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.S7.Tests/S7ArrayWriteAndHealthTests.cs
+++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.S7.Tests/S7ArrayWriteAndHealthTests.cs
@@ -167,6 +167,49 @@ public sealed class S7ArrayWriteAndHealthTests
drv.GetHealth().State.ShouldBe(DriverState.Healthy);
}
+ // ---- #986 S-5: bad scalar values are refusals ----
+
+ private const uint BadOutOfRange = 0x803C0000u;
+
+ ///
+ /// Out-of-range / unconvertible values to narrow scalars are BadOutOfRange / BadTypeMismatch,
+ /// never reach the PLC and leave health Healthy.
+ ///
+ [Theory]
+ [InlineData(S7DataType.Int16, "DB1.DBW0", 70000, BadOutOfRange)]
+ [InlineData(S7DataType.Byte, "DB1.DBB0", 300, BadOutOfRange)]
+ [InlineData(S7DataType.UInt16, "DB1.DBW0", -1, BadOutOfRange)]
+ [InlineData(S7DataType.Int32, "DB1.DBD0", "abc", BadTypeMismatch)]
+ [InlineData(S7DataType.Bool, "DB1.DBX0.0", "abc", BadTypeMismatch)]
+ public async Task Write_bad_scalar_value_is_a_refusal_not_a_comms_fault(S7DataType dt, string addr, object value, uint expected)
+ {
+ var factory = new RecordingFactory();
+ using var drv = new S7Driver(Options(new S7TagDefinition("T", addr, dt, Writable: true)), "s7-bad-scalar", factory);
+ await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
+
+ var res = await drv.WriteAsync([new WriteRequest("T", value)], TestContext.Current.CancellationToken);
+
+ res[0].StatusCode.ShouldBe(expected);
+ factory.Created[0].ScalarWrites.ShouldBe(0);
+ drv.GetHealth().State.ShouldBe(DriverState.Healthy);
+ }
+
+ /// Positive control: a real connection failure on a valid value still degrades health.
+ [Fact]
+ public async Task Write_valid_value_with_connection_failure_still_degrades()
+ {
+ var factory = new RecordingFactory();
+ using var drv = new S7Driver(Options(new S7TagDefinition("T", "DB1.DBW0", S7DataType.Int16, Writable: true)), "s7-conn-fail", factory);
+ await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
+ factory.Created[0].WriteFaults["DB1.DBW0"] = new PlcException(ErrorCode.SendData, "device glitch");
+
+ var res = await drv.WriteAsync([new WriteRequest("T", 5)], TestContext.Current.CancellationToken);
+
+ res[0].StatusCode.ShouldNotBe(0u);
+ factory.Created[0].ScalarWrites.ShouldBe(1);
+ drv.GetHealth().State.ShouldBe(DriverState.Degraded);
+ }
+
// ---- STAB-21 connect backoff ----
///