Skip to content

Commit

Permalink
feat: Implement clock reading
Browse files Browse the repository at this point in the history
  • Loading branch information
mycroes committed Aug 29, 2023
1 parent eada47c commit 07325db
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
53 changes: 53 additions & 0 deletions S7.Net/Plc.Clock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.IO;
using System.Linq;
using S7.Net.Helper;
using S7.Net.Types;
using DateTime = System.DateTime;

namespace S7.Net;

partial class Plc
{
private const byte SzlFunctionGroupTimers = 0x07;
private const byte SzlSubFunctionReadClock = 0x01;

private static byte[] BuildClockReadRequest()
{
var stream = new MemoryStream();

WriteSzlRequestHeader(stream, SzlFunctionGroupTimers, SzlSubFunctionReadClock, 4);
stream.Write(new byte[] { 0x0a, 0x00, 0x00, 0x00 });

stream.SetLength(stream.Position);
return stream.ToArray();
}

private static DateTime ParseClockReadResponse(byte[] message)
{
const int pduErrOffset = 20;
const int dtResultOffset = pduErrOffset + 2;
const int dtLenOffset = dtResultOffset + 2;
const int dtValueOffset = dtLenOffset + 4;

var pduErr = Word.FromByteArray(message.Skip(pduErrOffset).Take(2).ToArray());
if (pduErr != 0)
{
throw new Exception($"Response from PLC indicates error 0x{pduErr:X4}.");
}

var dtResult = message[dtResultOffset];
if (dtResult != 0xff)
{
throw new Exception($"Response from PLC indicates error 0x{dtResult:X2}.");
}

var len = Word.FromByteArray(message.Skip(dtLenOffset).Take(2).ToArray());
if (len != Types.DateTime.Length)
{
throw new Exception($"Unexpected response length {len}, expected {Types.DateTime.Length}.");
}

return Types.DateTime.FromByteArray(message.Skip(dtValueOffset).Take(Types.DateTime.Length).ToArray());
}
}
5 changes: 4 additions & 1 deletion S7.Net/PlcAsynchronous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ public async Task<List<DataItem>> ReadMultipleVarsAsync(List<DataItem> dataItems
/// <returns>A task that represents the asynchronous operation, with it's result set to the current PLC time on completion.</returns>
public async Task<System.DateTime> ReadClockAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
var request = BuildClockReadRequest();
var response = await RequestTsduAsync(request, cancellationToken);

return ParseClockReadResponse(response);
}

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion S7.Net/PlcSynchronous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,10 @@ public void ReadMultipleVars(List<DataItem> dataItems)
/// <returns>The current PLC time.</returns>
public System.DateTime ReadClock()
{
throw new NotImplementedException();
var request = BuildClockReadRequest();
var response = RequestTsdu(request);

return ParseClockReadResponse(response);
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions S7.Net/Types/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace S7.Net.Types
/// </summary>
public static class DateTime
{
/// <summary>
/// The length in bytes of DateTime stored in the PLC.
/// </summary>
public const int Length = 10;

/// <summary>
/// The minimum <see cref="T:System.DateTime"/> value supported by the specification.
/// </summary>
Expand Down

0 comments on commit 07325db

Please sign in to comment.