From c5f9612593713f70c11c341770822285c3f58c48 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Mon, 18 Jan 2016 11:33:25 +0100 Subject: [PATCH] - added methods to C# MmsValue class --- dotnet/IEC61850forCSharp/MmsValue.cs | 60 +++++++++++++++++++++++++++- dotnet/tests/Test.cs | 29 ++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/dotnet/IEC61850forCSharp/MmsValue.cs b/dotnet/IEC61850forCSharp/MmsValue.cs index d4e84436..1a9d1b6d 100644 --- a/dotnet/IEC61850forCSharp/MmsValue.cs +++ b/dotnet/IEC61850forCSharp/MmsValue.cs @@ -44,9 +44,15 @@ namespace IEC61850 [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern float MmsValue_toFloat (IntPtr self); + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + static extern void MmsValue_setFloat (IntPtr self, float value); + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern double MmsValue_toDouble (IntPtr self); + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + static extern void MmsValue_setDouble (IntPtr self, double newFloatValue); + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] static extern bool MmsValue_getBoolean (IntPtr self); @@ -91,6 +97,9 @@ namespace IEC61850 [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern ulong MmsValue_getUtcTimeInMs (IntPtr self); + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] + static extern void MmsValue_setUtcTimeMs (IntPtr self, ulong timeval); + [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern ulong MmsValue_getUtcTimeInMsWithUs(IntPtr self, [Out] uint usec); @@ -146,13 +155,14 @@ namespace IEC61850 [DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)] static extern void MmsValue_setBinaryTime (IntPtr self, UInt64 timestamp); - - internal MmsValue (IntPtr value) + // TODO make internal + public MmsValue (IntPtr value) { valueReference = value; this.responsableForDeletion = false; } + // TODO make internal internal MmsValue (IntPtr value, bool responsableForDeletion) { valueReference = value; @@ -395,6 +405,22 @@ namespace IEC61850 throw new MmsValueException ("Value is not a time type"); } + /// + /// Sets the timestamp value as UTC time in ms. + /// + /// + /// Sets the value as milliseconds since epoch (1.1.1970 UTC). + /// The value has to be of type MMS_UTC_TIME. + /// + /// This exception is thrown if the value has the wrong type. + public void SetUtcTimeMs(ulong timeval) + { + if (GetType () == MmsType.MMS_UTC_TIME) { + MmsValue_setUtcTimeMs(valueReference, timeval); + } else + throw new MmsValueException ("Value is not a time type"); + } + /// /// Gets the timestamp value as UTC time in ms. /// @@ -616,6 +642,21 @@ namespace IEC61850 throw new MmsValueException ("Value type is not float"); } + /// + /// Sets the float value of an MMS_FLOAT instance + /// + /// + /// the new value to set + /// + /// This exception is thrown if the value has the wrong type. + public void SetFloat (float value) + { + if (GetType () == MmsType.MMS_FLOAT) + MmsValue_setFloat (valueReference, value); + else + throw new MmsValueException ("Value type is not float"); + } + /// /// Gets the double value of an MMS_FLOAT instance /// @@ -630,6 +671,21 @@ namespace IEC61850 else throw new MmsValueException ("Value type is not float"); } + + /// + /// Sets the float/double value of an MMS_FLOAT instance + /// + /// + /// the new value to set + /// + /// This exception is thrown if the value has the wrong type. + public void SetDouble (double value) + { + if (GetType () == MmsType.MMS_FLOAT) + MmsValue_setDouble (valueReference, value); + else + throw new MmsValueException ("Value type is not float"); + } public override bool Equals (object obj) { diff --git a/dotnet/tests/Test.cs b/dotnet/tests/Test.cs index c5b03c02..ca1e4b6e 100644 --- a/dotnet/tests/Test.cs +++ b/dotnet/tests/Test.cs @@ -74,6 +74,35 @@ namespace tests Assert.AreEqual(octetString, secondOctetString); } + [Test ()] + public void MmsValueFloat() + { + var val = new MmsValue ((float) 1234.5678); + + Assert.AreEqual (val.ToFloat (), (float)1234.5678); + + val.SetFloat ((float) 0.1234); + + Assert.AreEqual (val.ToFloat (), (float) 0.1234); + + Assert.AreEqual (val.ToDouble (), (double) 0.1234, (double) 0.0001); + } + + [Test ()] + public void MmsValueDouble() + { + var val = new MmsValue ((double) 1234.5678); + + Assert.AreEqual (val.ToDouble (), (double)1234.5678); + + val.SetDouble ((double) 0.1234); + + Assert.AreEqual (val.ToDouble (), (double) 0.1234); + + Assert.AreEqual (val.ToFloat (), (float)0.1234); + } + + } }