Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for string/wstring in a class #458

Merged
merged 1 commit into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions S7.Net.UnitTest/Helpers/TestClass.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

using S7.Net.Types;

namespace S7.Net.UnitTest.Helpers
{
class TestClass
Expand Down Expand Up @@ -51,5 +53,16 @@ class TestClass
/// DB1.DBD16
/// </summary>
public ushort DWordVariable { get; set; }

/// <summary>
/// DB1.DBX20.0
/// </summary>
[S7String(S7StringType.S7WString, 10)]
public string WStringVariable { get; set; }
/// <summary>
/// DB1.DBX44.0
/// </summary>
[S7String(S7StringType.S7String, 10)]
public string StringVariable { get; set; }
}
}
29 changes: 24 additions & 5 deletions S7.Net.UnitTest/S7NetTestsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using S7.UnitTest.Helpers;
using System.Threading.Tasks;
using System.Threading;
using System.Security.Cryptography;

#endregion

Expand Down Expand Up @@ -154,7 +155,9 @@ public async Task Test_Async_ReadAndWriteClass()
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};

await plc.WriteClassAsync(tc, DB2);
Expand All @@ -168,6 +171,8 @@ public async Task Test_Async_ReadAndWriteClass()
Assert.AreEqual(tc.LRealVariable, tc2.LRealVariable);
Assert.AreEqual(tc.RealVariable, tc2.RealVariable);
Assert.AreEqual(tc.DWordVariable, tc2.DWordVariable);
Assert.AreEqual(tc.WStringVariable, tc2.WStringVariable);
Assert.AreEqual(tc.StringVariable, tc2.StringVariable);
}

[TestMethod]
Expand Down Expand Up @@ -580,7 +585,9 @@ public async Task Test_Async_ReadClassIgnoresNonPublicSetters()
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};

await plc.WriteClassAsync(tc, DB2);
Expand Down Expand Up @@ -628,7 +635,10 @@ public async Task Test_Async_ReadClassWithGenericReturnsSameResultAsReadClassWit
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"

};

await plc.WriteClassAsync(tc, DB2);
Expand All @@ -646,6 +656,9 @@ public async Task Test_Async_ReadClassWithGenericReturnsSameResultAsReadClassWit
Assert.AreEqual(Math.Round(tc2.LRealVariable, 3), Math.Round(tc2Generic.LRealVariable, 3));
Assert.AreEqual(tc2.RealVariable, tc2Generic.RealVariable);
Assert.AreEqual(tc2.DWordVariable, tc2Generic.DWordVariable);
Assert.AreEqual(tc2.WStringVariable, tc2Generic.WStringVariable);
Assert.AreEqual(tc2.StringVariable, tc2Generic.StringVariable);

}

[TestMethod]
Expand All @@ -671,7 +684,9 @@ public async Task Test_Async_ReadClassWithGenericAndClassFactoryReturnsSameResul
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};

await plc.WriteClassAsync(tc, DB2);
Expand All @@ -686,6 +701,8 @@ public async Task Test_Async_ReadClassWithGenericAndClassFactoryReturnsSameResul
Assert.AreEqual(Math.Round(tc2Generic.LRealVariable, 3), Math.Round(tc2GenericWithClassFactory.LRealVariable, 3));
Assert.AreEqual(tc2Generic.RealVariable, tc2GenericWithClassFactory.RealVariable);
Assert.AreEqual(tc2Generic.DWordVariable, tc2GenericWithClassFactory.DWordVariable);
Assert.AreEqual(tc2Generic.WStringVariable, tc2GenericWithClassFactory.WStringVariable);
Assert.AreEqual(tc2Generic.StringVariable, tc2GenericWithClassFactory.StringVariable);
}

[TestMethod]
Expand Down Expand Up @@ -792,7 +809,9 @@ public async Task Test_Async_ReadClassReturnsNumberOfReadBytesFromThePlc()
IntVariable = -15000,
LRealVariable = -154.789,
RealVariable = -154.789f,
DWordVariable = 850
DWordVariable = 850,
WStringVariable = "ÄÜÉÊéà",
StringVariable = "Hallo"
};
plc.WriteClass(tc, DB2);

Expand Down
19 changes: 19 additions & 0 deletions S7.Net.UnitTest/S7NetTestsSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using S7.Net.UnitTest.Helpers;
using S7.Net.Types;
using S7.UnitTest.Helpers;
using System.Security.Cryptography;

#endregion

Expand Down Expand Up @@ -183,6 +184,9 @@ public void T04_ReadAndWriteClass()
tc.LRealVariable = -154.789;
tc.RealVariable = -154.789f;
tc.DWordVariable = 850;
tc.WStringVariable = "ÄÜÉÊéà";
tc.StringVariable = "Hallo";

plc.WriteClass(tc, DB2);
TestClass tc2 = new TestClass();
// Values that are read from a class are stored inside the class itself, that is passed by reference
Expand All @@ -194,6 +198,8 @@ public void T04_ReadAndWriteClass()
Assert.AreEqual(tc.LRealVariable, tc2.LRealVariable);
Assert.AreEqual(tc.RealVariable, tc2.RealVariable);
Assert.AreEqual(tc.DWordVariable, tc2.DWordVariable);
Assert.AreEqual(tc.WStringVariable, tc2.WStringVariable);
Assert.AreEqual(tc.StringVariable, tc2.StringVariable);
}

/// <summary>
Expand Down Expand Up @@ -577,6 +583,8 @@ public void T12_ReadClassIgnoresNonPublicSetters()
tc.LRealVariable = -154.789;
tc.RealVariable = -154.789f;
tc.DWordVariable = 850;
tc.WStringVariable = "ÄÜÉÊéà";
tc.StringVariable = "Hallo";

plc.WriteClass(tc, DB2);

Expand Down Expand Up @@ -622,6 +630,8 @@ public void T14_ReadClassWithGenericReturnsSameResultAsReadClassWithoutGeneric()
tc.LRealVariable = -154.789;
tc.RealVariable = -154.789f;
tc.DWordVariable = 850;
tc.WStringVariable = "ÄÜÉÊéà";
tc.StringVariable = "Hallo";

plc.WriteClass(tc, DB2);

Expand All @@ -637,6 +647,8 @@ public void T14_ReadClassWithGenericReturnsSameResultAsReadClassWithoutGeneric()
Assert.AreEqual(Math.Round(tc2.LRealVariable, 3), Math.Round(tc2Generic.LRealVariable, 3));
Assert.AreEqual(tc2.RealVariable, tc2Generic.RealVariable);
Assert.AreEqual(tc2.DWordVariable, tc2Generic.DWordVariable);
Assert.AreEqual(tc2.WStringVariable, tc2Generic.WStringVariable);
Assert.AreEqual(tc2.StringVariable, tc2Generic.StringVariable);
}

[TestMethod, ExpectedException(typeof(PlcException))]
Expand Down Expand Up @@ -665,6 +677,8 @@ public void T16_ReadClassWithGenericAndClassFactoryReturnsSameResultAsReadClassW
tc.LRealVariable = -154.789;
tc.RealVariable = -154.789f;
tc.DWordVariable = 850;
tc.WStringVariable = "ÄÜÉÊéà";
tc.StringVariable = "Hallo";

plc.WriteClass(tc, DB2);

Expand All @@ -679,6 +693,8 @@ public void T16_ReadClassWithGenericAndClassFactoryReturnsSameResultAsReadClassW
Assert.AreEqual(Math.Round(tc2Generic.LRealVariable, 3), Math.Round(tc2GenericWithClassFactory.LRealVariable, 3));
Assert.AreEqual(tc2Generic.RealVariable, tc2GenericWithClassFactory.RealVariable);
Assert.AreEqual(tc2Generic.DWordVariable, tc2GenericWithClassFactory.DWordVariable);
Assert.AreEqual(tc2Generic.WStringVariable, tc2GenericWithClassFactory.WStringVariable);
Assert.AreEqual(tc2Generic.StringVariable, tc2GenericWithClassFactory.StringVariable);
}

[TestMethod, ExpectedException(typeof(PlcException))]
Expand Down Expand Up @@ -837,6 +853,9 @@ public void T21_ReadClassReturnsNumberOfReadBytesFromThePlc()
tc.LRealVariable = -154.789;
tc.RealVariable = -154.789f;
tc.DWordVariable = 850;
tc.WStringVariable = "ÄÜÉÊéà";
tc.StringVariable = "Hallo";

plc.WriteClass(tc, DB2);

int expectedReadBytes = (int)Types.Class.GetClassSize(tc);
Expand Down
Loading