From 2bb7ac7d2ac45ed557c5b2d2901cd9e201c3093e Mon Sep 17 00:00:00 2001 From: Serge Camille Date: Mon, 17 Aug 2020 22:30:37 +0200 Subject: [PATCH] Write large byte array in S7NetTests to check for MaxPDU problems on Snap7. For me the tests work fine even when adjust the "chunk size" in WriteBytesAsync. So Snap7 seems to be fine, at least the current version. This should probably be tested with some live PLC's as well. --- S7.Net.UnitTest/S7NetTestsAsync.cs | 20 ++++++++++++++++++++ S7.Net.UnitTest/S7NetTestsSync.cs | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/S7.Net.UnitTest/S7NetTestsAsync.cs b/S7.Net.UnitTest/S7NetTestsAsync.cs index 38f12af..e03a490 100644 --- a/S7.Net.UnitTest/S7NetTestsAsync.cs +++ b/S7.Net.UnitTest/S7NetTestsAsync.cs @@ -128,6 +128,26 @@ namespace S7.Net.UnitTest Assert.AreEqual(val3, result3); } + /// + /// Write/Read a large amount of data to test PDU max + /// + [TestMethod] + public async Task Test_Async_WriteLargeByteArray() + { + Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor."); + + var randomEngine = new Random(); + var data = new byte[8192]; + var db = 2; + randomEngine.NextBytes(data); + + await plc.WriteBytesAsync(DataType.DataBlock, db, 0, data); + + var readData = await plc.ReadBytesAsync(DataType.DataBlock, db, 0, data.Length); + + CollectionAssert.AreEqual(data, readData); + } + /// /// Read/Write a class that has the same properties of a DB with the same field in the same order /// diff --git a/S7.Net.UnitTest/S7NetTestsSync.cs b/S7.Net.UnitTest/S7NetTestsSync.cs index 3b7589b..e2e563e 100644 --- a/S7.Net.UnitTest/S7NetTestsSync.cs +++ b/S7.Net.UnitTest/S7NetTestsSync.cs @@ -743,6 +743,26 @@ namespace S7.Net.UnitTest Assert.AreEqual(tc.ShortVariable04.ShortVarialbe00, tc2.ShortVariable04.ShortVarialbe00); } + /// + /// Write/Read a large amount of data to test PDU max + /// + [TestMethod] + public void T33_WriteLargeByteArray() + { + Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor."); + + var randomEngine = new Random(); + var data = new byte[8192]; + var db = 2; + randomEngine.NextBytes(data); + + plc.WriteBytes(DataType.DataBlock, db, 0, data); + + var readData = plc.ReadBytes(DataType.DataBlock, db, 0, data.Length); + + CollectionAssert.AreEqual(data, readData); + } + [TestMethod, ExpectedException(typeof(PlcException))] public void T18_ReadStructThrowsIfPlcIsNotConnected() {