Skip to content

Commit 41368ee

Browse files
committed
Add TimespanArray and TimestampArray
1 parent 46b319f commit 41368ee

File tree

1 file changed

+157
-7
lines changed

1 file changed

+157
-7
lines changed

src/S7CommPlusDriver/Core/PValue.cs

Lines changed: 157 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public static PValue Deserialize(Stream buffer, bool disableVlq = false)
105105
case Datatype.LReal:
106106
return ValueLRealArray.Deserialize(buffer, flags, disableVlq);
107107
case Datatype.Timestamp:
108-
throw new NotImplementedException();
108+
return ValueTimestampArray.Deserialize(buffer, flags, disableVlq);
109109
case Datatype.Timespan:
110-
throw new NotImplementedException();
110+
return ValueTimespanArray.Deserialize(buffer, flags, disableVlq);
111111
case Datatype.RID:
112112
return ValueRIDArray.Deserialize(buffer, flags, disableVlq);
113113
case Datatype.AID:
@@ -269,7 +269,7 @@ public static ValueBool Deserialize(Stream buffer, byte flags)
269269

270270
/// <summary>
271271
/// ValueBoolArray: Important: The length of the array is always a multiple of 8.
272-
/// E.g. reading an Array [0..2] of Bool will be transmitted with 8 elements with actal values at index 0, 1, 2.
272+
/// E.g. reading an Array [0..2] of Bool will be transmitted with 8 elements with actual values at index 0, 1, 2.
273273
/// An Array[0..9] will be transmitted with 16 elements and so on.
274274
/// At this time, serialize doesn't respect the padding elements, must be done on a higher level.
275275
/// </summary>
@@ -2104,7 +2104,7 @@ public override int Serialize(Stream buffer)
21042104
return ret;
21052105
}
21062106

2107-
public override string ToString()
2107+
public static string ToString(UInt64 Value)
21082108
{
21092109
DateTime dt = new DateTime(1970, 1, 1);
21102110
ulong v, ns;
@@ -2131,9 +2131,15 @@ public override string ToString()
21312131
}
21322132
else
21332133
{
2134-
return "<Value type=\"Timestamp\">" + dt.ToString() + "</Value>";
2134+
return dt.ToString();
21352135
}
2136-
return "<Value type=\"Timestamp\">" + String.Format(fmt, dt.ToString(), ns) + "</Value>";
2136+
return String.Format(fmt, dt.ToString(), ns);
2137+
}
2138+
2139+
public override string ToString()
2140+
{
2141+
string str = ToString(Value);
2142+
return "<Value type=\"Timestamp\">" + str + "</Value>";
21372143
}
21382144

21392145
public static ValueTimestamp Deserialize(Stream buffer, byte flags)
@@ -2144,6 +2150,69 @@ public static ValueTimestamp Deserialize(Stream buffer, byte flags)
21442150
}
21452151
}
21462152

2153+
public class ValueTimestampArray : PValue
2154+
{
2155+
UInt64[] Value;
2156+
2157+
public ValueTimestampArray(UInt64[] value) : this(value, 0)
2158+
{
2159+
}
2160+
2161+
public ValueTimestampArray(UInt64[] value, byte flags)
2162+
{
2163+
DatatypeFlags = flags;
2164+
Value = value;
2165+
}
2166+
2167+
public UInt64[] GetValue()
2168+
{
2169+
return Value;
2170+
}
2171+
2172+
public override int Serialize(Stream buffer)
2173+
{
2174+
int ret = 0;
2175+
ret += S7p.EncodeByte(buffer, DatatypeFlags);
2176+
ret += S7p.EncodeByte(buffer, Datatype.Timestamp);
2177+
for (int i = 0; i < Value.Length; i++)
2178+
{
2179+
ret += S7p.EncodeUInt64(buffer, Value[i]);
2180+
}
2181+
return ret;
2182+
}
2183+
2184+
public override string ToString()
2185+
{
2186+
string s = "<Value type =\"TimestampArray\" size=\"" + Value.Length.ToString() + "\">";
2187+
for (int i = 0; i < Value.Length; i++)
2188+
{
2189+
s += String.Format("<Value>{0}</Value>", ValueTimestamp.ToString(Value[i]));
2190+
}
2191+
s += "</Value>";
2192+
return s;
2193+
}
2194+
2195+
public static ValueTimestampArray Deserialize(Stream buffer, byte flags, bool disableVlq)
2196+
{
2197+
UInt64[] value;
2198+
UInt32 size = 0;
2199+
if (!disableVlq)
2200+
{
2201+
S7p.DecodeUInt32Vlq(buffer, out size);
2202+
}
2203+
else
2204+
{
2205+
S7p.DecodeUInt32(buffer, out size);
2206+
}
2207+
value = new UInt64[size];
2208+
for (int i = 0; i < size; i++)
2209+
{
2210+
S7p.DecodeUInt64(buffer, out value[i]);
2211+
}
2212+
return new ValueTimestampArray(value, flags);
2213+
}
2214+
}
2215+
21472216
public class ValueTimespan : PValue
21482217
{
21492218
Int64 Value;
@@ -2172,7 +2241,7 @@ public override int Serialize(Stream buffer)
21722241
return ret;
21732242
}
21742243

2175-
public override string ToString()
2244+
public static string ToString(Int64 Value)
21762245
{
21772246
string str;
21782247
long[] divs = { 86400000000000, 3600000000000, 60000000000, 1000000000, 1000000, 1000, 1 };
@@ -2214,6 +2283,12 @@ public override string ToString()
22142283
}
22152284
}
22162285
}
2286+
return str;
2287+
}
2288+
2289+
public override string ToString()
2290+
{
2291+
string str = ToString(Value);
22172292
return ("<Value type=\"Timespan\">" + str + "</Value>");
22182293
}
22192294

@@ -2232,6 +2307,81 @@ public static ValueTimespan Deserialize(Stream buffer, byte flags, bool disableV
22322307
}
22332308
}
22342309

2310+
public class ValueTimespanArray : PValue
2311+
{
2312+
Int64[] Value;
2313+
2314+
public ValueTimespanArray(Int64[] value) : this(value, FLAGS_ARRAY)
2315+
{
2316+
}
2317+
2318+
public ValueTimespanArray(Int64[] value, byte flags)
2319+
{
2320+
DatatypeFlags = flags;
2321+
if (value != null)
2322+
{
2323+
Value = new Int64[value.Length];
2324+
Array.Copy(value, Value, value.Length);
2325+
}
2326+
}
2327+
2328+
public Int64[] GetValue()
2329+
{
2330+
return Value;
2331+
}
2332+
2333+
public override int Serialize(Stream buffer)
2334+
{
2335+
int ret = 0;
2336+
ret += S7p.EncodeByte(buffer, DatatypeFlags);
2337+
ret += S7p.EncodeByte(buffer, Datatype.LReal);
2338+
ret += S7p.EncodeUInt32Vlq(buffer, (uint)Value.Length);
2339+
for (int i = 0; i < Value.Length; i++)
2340+
{
2341+
ret += S7p.EncodeInt64Vlq(buffer, Value[i]);
2342+
}
2343+
return ret;
2344+
}
2345+
2346+
public override string ToString()
2347+
{
2348+
string s = "<Value type =\"ValueTimespanArray\" size=\"" + Value.Length.ToString() + "\">";
2349+
for (int i = 0; i < Value.Length; i++)
2350+
{
2351+
s += String.Format("<Value>{0}</Value>", ValueTimespan.ToString(Value[i]));
2352+
}
2353+
s += "</Value>";
2354+
return s;
2355+
}
2356+
2357+
public static ValueTimespanArray Deserialize(Stream buffer, byte flags, bool disableVlq)
2358+
{
2359+
Int64[] value;
2360+
UInt32 size = 0;
2361+
if (!disableVlq)
2362+
{
2363+
S7p.DecodeUInt32Vlq(buffer, out size);
2364+
}
2365+
else
2366+
{
2367+
S7p.DecodeUInt32(buffer, out size);
2368+
}
2369+
value = new Int64[size];
2370+
for (int i = 0; i < size; i++)
2371+
{
2372+
if (!disableVlq)
2373+
{
2374+
S7p.DecodeInt64Vlq(buffer, out value[i]);
2375+
}
2376+
else
2377+
{
2378+
S7p.DecodeInt64(buffer, out value[i]);
2379+
}
2380+
}
2381+
return new ValueTimespanArray(value, flags);
2382+
}
2383+
}
2384+
22352385
public class ValueRID : PValue
22362386
{
22372387
UInt32 Value;

0 commit comments

Comments
 (0)