diff --git a/S7.Net.UnitTest/S7NetTestsAsync.cs b/S7.Net.UnitTest/S7NetTestsAsync.cs
index 5d59882..2033884 100644
--- a/S7.Net.UnitTest/S7NetTestsAsync.cs
+++ b/S7.Net.UnitTest/S7NetTestsAsync.cs
@@ -1006,7 +1006,7 @@ namespace S7.Net.UnitTest
var db = 2;
randomEngine.NextBytes(data);
- cancellationSource.CancelAfter(TimeSpan.FromMilliseconds(5));
+ cancellationSource.CancelAfter(System.TimeSpan.FromMilliseconds(5));
try
{
await plc.WriteBytesAsync(DataType.DataBlock, db, 0, data, cancellationToken);
@@ -1045,7 +1045,7 @@ namespace S7.Net.UnitTest
var db = 2;
randomEngine.NextBytes(data.Span);
- cancellationSource.CancelAfter(TimeSpan.FromMilliseconds(5));
+ cancellationSource.CancelAfter(System.TimeSpan.FromMilliseconds(5));
try
{
await plc.WriteBytesAsync(DataType.DataBlock, db, 0, data, cancellationToken);
diff --git a/S7.Net/Enums.cs b/S7.Net/Enums.cs
index fc412d3..080dce6 100644
--- a/S7.Net/Enums.cs
+++ b/S7.Net/Enums.cs
@@ -206,6 +206,11 @@
///
/// DateTimeLong variable type
///
- DateTimeLong
+ DateTimeLong,
+
+ ///
+ /// S7 TIME variable type - serialized as S7 DInt and deserialized as C# TimeSpan
+ ///
+ Time
}
}
diff --git a/S7.Net/PLCHelpers.cs b/S7.Net/PLCHelpers.cs
index c0fbd78..69fc890 100644
--- a/S7.Net/PLCHelpers.cs
+++ b/S7.Net/PLCHelpers.cs
@@ -167,6 +167,15 @@ namespace S7.Net
{
return DateTimeLong.ToArray(bytes);
}
+ case VarType.Time:
+ if (varCount == 1)
+ {
+ return TimeSpan.FromByteArray(bytes);
+ }
+ else
+ {
+ return TimeSpan.ToArray(bytes);
+ }
default:
return null;
}
@@ -200,6 +209,7 @@ namespace S7.Net
case VarType.DWord:
case VarType.DInt:
case VarType.Real:
+ case VarType.Time:
return varCount * 4;
case VarType.LReal:
case VarType.DateTime:
diff --git a/S7.Net/Types/TimeSpan.cs b/S7.Net/Types/TimeSpan.cs
new file mode 100644
index 0000000..37515d8
--- /dev/null
+++ b/S7.Net/Types/TimeSpan.cs
@@ -0,0 +1,95 @@
+using System;
+using System.Collections.Generic;
+
+namespace S7.Net.Types
+{
+ public static class TimeSpan
+ {
+ ///
+ /// The minimum value supported by the specification.
+ ///
+ public static readonly System.TimeSpan SpecMinimumTimeSpan = System.TimeSpan.FromMilliseconds(int.MinValue);
+
+ ///
+ /// The maximum value supported by the specification.
+ ///
+ public static readonly System.TimeSpan SpecMaximumTimeSpan = System.TimeSpan.FromMilliseconds(int.MaxValue);
+
+ ///
+ /// Parses a value from bytes.
+ ///
+ /// Input bytes read from PLC.
+ /// A object representing the value read from PLC.
+ /// Thrown when the length of
+ /// is not 4 or any value in
+ /// is outside the valid range of values.
+ public static System.TimeSpan FromByteArray(byte[] bytes)
+ {
+ var milliseconds = DInt.FromByteArray(bytes);
+ return System.TimeSpan.FromMilliseconds(milliseconds);
+ }
+
+ ///
+ /// Parses an array of values from bytes.
+ ///
+ /// Input bytes read from PLC.
+ /// An array of objects representing the values read from PLC.
+ /// Thrown when the length of
+ /// is not a multiple of 4 or any value in
+ /// is outside the valid range of values.
+ public static System.TimeSpan[] ToArray(byte[] bytes)
+ {
+ const int singleTimeSpanLength = 4;
+
+ if (bytes.Length % singleTimeSpanLength != 0)
+ throw new ArgumentOutOfRangeException(nameof(bytes), bytes.Length,
+ $"Parsing an array of {nameof(System.TimeSpan)} requires a multiple of {singleTimeSpanLength} bytes of input data, input data is '{bytes.Length}' long.");
+
+ var cnt = bytes.Length / singleTimeSpanLength;
+ var result = new System.TimeSpan[bytes.Length / singleTimeSpanLength];
+
+ var milliseconds = DInt.ToArray(bytes);
+ for (var i = 0; i < milliseconds.Length; i++)
+ result[i] = System.TimeSpan.FromMilliseconds(milliseconds[i]);
+
+ return result;
+ }
+
+ ///
+ /// Converts a value to a byte array.
+ ///
+ /// The DateTime value to convert.
+ /// A byte array containing the S7 date time representation of .
+ /// Thrown when the value of
+ /// is before
+ /// or after .
+ public static byte[] ToByteArray(System.TimeSpan timeSpan)
+ {
+ if (timeSpan < SpecMinimumTimeSpan)
+ throw new ArgumentOutOfRangeException(nameof(timeSpan), timeSpan,
+ $"Time span '{timeSpan}' is before the minimum '{SpecMinimumTimeSpan}' supported in S7 time representation.");
+
+ if (timeSpan > SpecMaximumTimeSpan)
+ throw new ArgumentOutOfRangeException(nameof(timeSpan), timeSpan,
+ $"Time span '{timeSpan}' is after the maximum '{SpecMaximumTimeSpan}' supported in S7 time representation.");
+
+ return DInt.ToByteArray(Convert.ToInt32(timeSpan.TotalMilliseconds));
+ }
+
+ ///
+ /// Converts an array of values to a byte array.
+ ///
+ /// The DateTime values to convert.
+ /// A byte array containing the S7 date time representations of .
+ /// Thrown when any value of
+ /// is before
+ /// or after .
+ public static byte[] ToByteArray(System.TimeSpan[] timeSpans)
+ {
+ var bytes = new List(timeSpans.Length * 4);
+ foreach (var timeSpan in timeSpans) bytes.AddRange(ToByteArray(timeSpan));
+
+ return bytes.ToArray();
+ }
+ }
+}
\ No newline at end of file