Add DateTime support for read/write methods

This commit is contained in:
Michael Croes
2018-09-21 21:38:23 +02:00
parent 735f1d4533
commit 427d8124de
3 changed files with 22 additions and 1 deletions

View File

@@ -181,6 +181,11 @@
/// <summary>
/// Counter variable type
/// </summary>
Counter
Counter,
/// <summary>
/// DateTIme variable type
/// </summary>
DateTime
}
}

View File

@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DateTime = S7.Net.Types.DateTime;
namespace S7.Net
{
@@ -146,6 +147,15 @@ namespace S7.Net
{
return Bit.ToBitArray(bytes);
}
case VarType.DateTime:
if (varCount == 1)
{
return DateTime.FromByteArray(bytes);
}
else
{
return DateTime.ToArray(bytes);
}
default:
return null;
}
@@ -178,6 +188,8 @@ namespace S7.Net
case VarType.DInt:
case VarType.Real:
return varCount * 4;
case VarType.DateTime:
return varCount * 8;
default:
return 0;
}

View File

@@ -41,6 +41,8 @@ namespace S7.Net.Protocol
return Types.Double.ToByteArray((double)value);
case "Single":
return Types.Single.ToByteArray((float)value);
case "DateTime":
return Types.DateTime.ToByteArray((System.DateTime) value);
case "Byte[]":
return (byte[])value;
case "Int16[]":
@@ -60,6 +62,8 @@ namespace S7.Net.Protocol
// if the consumer does not pay attention to string length.
var stringVal = (string) value;
return Types.String.ToByteArray(stringVal, stringVal.Length);
case "DateTime[]":
return Types.DateTime.ToByteArray((System.DateTime[]) value);
default:
throw new InvalidVariableTypeException();
}