feat(s7): STRING/WSTRING/CHAR/WCHAR and DTL/DATE/TOD/TIME/S5TIME codecs, TSAP/connection-type override, negotiated PDU surfaced (#632 #288 #289 #294 #295)
New S7DataType members WString/Char/WChar/Dtl/Date/Tod/Time/S5Time mapped onto existing DriverDataType (String / DateTime / Int32 ms); writes refuse out-of-range with BadOutOfRange. Optional localTsap/remoteTsap/connectionType through options, factory JSON, probe and AdminUI form. NegotiatedPduSize snapshotted at connect and exposed via GetDiagnostics (DriverHealth has no bag). Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
@@ -29,6 +29,13 @@ public interface IS7Plc : IDisposable
|
||||
/// <summary>Closes the S7 connection. Best-effort and idempotent.</summary>
|
||||
void Close();
|
||||
|
||||
/// <summary>
|
||||
/// PDU size negotiated with the CPU during <see cref="OpenAsync"/>, in bytes (typically 240 on S7-300/400
|
||||
/// and S7-1200/1500 defaults, 480/960 with extended PDU); 0 when unknown / not yet connected. Default
|
||||
/// interface member so a test fake that predates it keeps compiling.
|
||||
/// </summary>
|
||||
int MaxPduSize => 0;
|
||||
|
||||
/// <summary>Reads a scalar value by S7 address string (e.g. <c>DB1.DBW0</c>); the boxed type follows the size suffix.</summary>
|
||||
/// <param name="address">The S7 address string.</param>
|
||||
/// <param name="cancellationToken">Cancellation token for the read.</param>
|
||||
@@ -94,6 +101,16 @@ public interface IS7PlcFactory
|
||||
/// <param name="timeout">Connect + per-operation timeout, applied to the underlying read/write timeouts.</param>
|
||||
/// <returns>A new, not-yet-opened <see cref="IS7Plc"/>.</returns>
|
||||
IS7Plc Create(S7CpuType cpuType, string host, int port, short rack, short slot, TimeSpan timeout);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a connection from the full driver options, so an optional TSAP / connection-type override
|
||||
/// reaches the handshake. The default forwards to the six-argument overload (no override); the
|
||||
/// real factory honours <see cref="S7DriverOptions.LocalTsap"/> and friends.
|
||||
/// </summary>
|
||||
/// <param name="options">The driver options.</param>
|
||||
/// <returns>A new, not-yet-opened <see cref="IS7Plc"/>.</returns>
|
||||
IS7Plc Create(S7DriverOptions options) =>
|
||||
Create(options.CpuType, options.Host, options.Port, options.Rack, options.Slot, options.Timeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -114,8 +131,23 @@ internal sealed class S7PlcAdapter : IS7Plc
|
||||
/// <param name="slot">CPU slot.</param>
|
||||
/// <param name="timeout">Connect + per-operation timeout.</param>
|
||||
public S7PlcAdapter(S7CpuType cpuType, string host, int port, short rack, short slot, TimeSpan timeout)
|
||||
: this(new Plc(S7CpuTypeMap.ToS7Net(cpuType), host, port, rack, slot), timeout)
|
||||
{
|
||||
_plc = new Plc(S7CpuTypeMap.ToS7Net(cpuType), host, port, rack, slot);
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance with an explicit TSAP pair (<c>LocalTsap</c> / <c>RemoteTsap</c> / <c>ConnectionType</c> override).</summary>
|
||||
/// <param name="host">PLC IP address or hostname.</param>
|
||||
/// <param name="port">TCP port.</param>
|
||||
/// <param name="tsap">The effective TSAP pair.</param>
|
||||
/// <param name="timeout">Connect + per-operation timeout.</param>
|
||||
public S7PlcAdapter(string host, int port, global::S7.Net.Protocol.TsapPair tsap, TimeSpan timeout)
|
||||
: this(new Plc(host, port, tsap), timeout)
|
||||
{
|
||||
}
|
||||
|
||||
private S7PlcAdapter(Plc plc, TimeSpan timeout)
|
||||
{
|
||||
_plc = plc;
|
||||
_operationTimeout = timeout;
|
||||
// S7netplus writes timeouts into the underlying TcpClient via ReadTimeout / WriteTimeout
|
||||
// (milliseconds). Set before OpenAsync so the handshake itself honours the bound — matches
|
||||
@@ -132,6 +164,9 @@ internal sealed class S7PlcAdapter : IS7Plc
|
||||
/// <inheritdoc />
|
||||
public bool IsConnected => _plc.IsConnected;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int MaxPduSize => _plc.MaxPDUSize;
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task OpenAsync(CancellationToken cancellationToken) => _plc.OpenAsync(cancellationToken);
|
||||
|
||||
@@ -187,4 +222,10 @@ internal sealed class S7PlcFactory : IS7PlcFactory
|
||||
/// <inheritdoc />
|
||||
public IS7Plc Create(S7CpuType cpuType, string host, int port, short rack, short slot, TimeSpan timeout) =>
|
||||
new S7PlcAdapter(cpuType, host, port, rack, slot, timeout);
|
||||
|
||||
/// <inheritdoc />
|
||||
public IS7Plc Create(S7DriverOptions options) =>
|
||||
S7Tsap.Resolve(options) is { } tsap
|
||||
? new S7PlcAdapter(options.Host, options.Port, tsap, options.Timeout)
|
||||
: new S7PlcAdapter(options.CpuType, options.Host, options.Port, options.Rack, options.Slot, options.Timeout);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user