Skip to content

Commit

Permalink
refactor: Replace DateTime.Length with Plc.DateTimeLength in Plc.Clock
Browse files Browse the repository at this point in the history
  • Loading branch information
mycroes committed Sep 4, 2023
1 parent cb24e9a commit 4764c99
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
21 changes: 15 additions & 6 deletions S7.Net/Plc.Clock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ partial class Plc
private const int PduErrOffset = 20;
private const int UserDataResultOffset = PduErrOffset + 2;

/// <summary>
/// The length in bytes of DateTime stored in the PLC.
/// </summary>
private const int DateTimeLength = 10;

private static byte[] BuildClockReadRequest()
{
var stream = new MemoryStream();
Expand All @@ -30,27 +35,31 @@ private static byte[] BuildClockReadRequest()
private static DateTime ParseClockReadResponse(byte[] message)
{
const int udLenOffset = UserDataResultOffset + 2;
const int udValueOffset = udLenOffset + 4;
const int udValueOffset = udLenOffset + 2;
const int dateTimeSkip = 2;

AssertPduResult(message);
AssertUserDataResult(message, 0xff);

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

return Types.DateTime.FromByteArray(message.Skip(udValueOffset).Take(Types.DateTime.Length).ToArray());
// Skip first 2 bytes from date time value because DateTime.FromByteArray doesn't parse them.
return Types.DateTime.FromByteArray(message.Skip(udValueOffset + dateTimeSkip)
.Take(DateTimeLength - dateTimeSkip).ToArray());
}

private static byte[] BuildClockWriteRequest(DateTime value)
{
var stream = new MemoryStream();

WriteUserDataRequest(stream, SzlFunctionGroupTimers, SzlSubFunctionWriteClock, 14);
stream.Write(new byte[] { 0xff, TransportSizeOctetString, 0x00, Types.DateTime.Length });
stream.Write(new byte[] { 0x00, 0x19 }); // Start of actual DateTime value, DateTime.ToByteArray is broken
stream.Write(new byte[] { 0xff, TransportSizeOctetString, 0x00, DateTimeLength });
// Start of DateTime value, DateTime.ToByteArray only serializes the final 8 bytes
stream.Write(new byte[] { 0x00, 0x19 });
stream.Write(Types.DateTime.ToByteArray(value));

stream.SetLength(stream.Position);
Expand Down
5 changes: 0 additions & 5 deletions S7.Net/Types/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ 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 4764c99

Please sign in to comment.