mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 14:28:25 +08:00
refactor: Replace DateTime.Length with Plc.DateTimeLength in Plc.Clock
This commit is contained in:
@@ -16,6 +16,11 @@ partial class Plc
|
|||||||
private const int PduErrOffset = 20;
|
private const int PduErrOffset = 20;
|
||||||
private const int UserDataResultOffset = PduErrOffset + 2;
|
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()
|
private static byte[] BuildClockReadRequest()
|
||||||
{
|
{
|
||||||
var stream = new MemoryStream();
|
var stream = new MemoryStream();
|
||||||
@@ -30,18 +35,21 @@ partial class Plc
|
|||||||
private static DateTime ParseClockReadResponse(byte[] message)
|
private static DateTime ParseClockReadResponse(byte[] message)
|
||||||
{
|
{
|
||||||
const int udLenOffset = UserDataResultOffset + 2;
|
const int udLenOffset = UserDataResultOffset + 2;
|
||||||
const int udValueOffset = udLenOffset + 4;
|
const int udValueOffset = udLenOffset + 2;
|
||||||
|
const int dateTimeSkip = 2;
|
||||||
|
|
||||||
AssertPduResult(message);
|
AssertPduResult(message);
|
||||||
AssertUserDataResult(message, 0xff);
|
AssertUserDataResult(message, 0xff);
|
||||||
|
|
||||||
var len = Word.FromByteArray(message.Skip(udLenOffset).Take(2).ToArray());
|
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)
|
private static byte[] BuildClockWriteRequest(DateTime value)
|
||||||
@@ -49,8 +57,9 @@ partial class Plc
|
|||||||
var stream = new MemoryStream();
|
var stream = new MemoryStream();
|
||||||
|
|
||||||
WriteUserDataRequest(stream, SzlFunctionGroupTimers, SzlSubFunctionWriteClock, 14);
|
WriteUserDataRequest(stream, SzlFunctionGroupTimers, SzlSubFunctionWriteClock, 14);
|
||||||
stream.Write(new byte[] { 0xff, TransportSizeOctetString, 0x00, Types.DateTime.Length });
|
stream.Write(new byte[] { 0xff, TransportSizeOctetString, 0x00, DateTimeLength });
|
||||||
stream.Write(new byte[] { 0x00, 0x19 }); // Start of actual DateTime value, DateTime.ToByteArray is broken
|
// 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.Write(Types.DateTime.ToByteArray(value));
|
||||||
|
|
||||||
stream.SetLength(stream.Position);
|
stream.SetLength(stream.Position);
|
||||||
|
|||||||
@@ -8,11 +8,6 @@ namespace S7.Net.Types
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class DateTime
|
public static class DateTime
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The length in bytes of DateTime stored in the PLC.
|
|
||||||
/// </summary>
|
|
||||||
public const int Length = 10;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The minimum <see cref="T:System.DateTime"/> value supported by the specification.
|
/// The minimum <see cref="T:System.DateTime"/> value supported by the specification.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user