diff --git a/S7.Net.UnitTest/Helpers/S7TestServer.cs b/S7.Net.UnitTest/Helpers/S7TestServer.cs index 57ed663..311d773 100644 --- a/S7.Net.UnitTest/Helpers/S7TestServer.cs +++ b/S7.Net.UnitTest/Helpers/S7TestServer.cs @@ -7,8 +7,9 @@ namespace S7.Net.UnitTest.Helpers { static S7Server Server; static private byte[] DB1 = new byte[512]; // Our DB1 - static private byte[] DB2 = new byte[1028]; // Our DB2 + static private byte[] DB2 = new byte[64000]; // Our DB2 static private byte[] DB3 = new byte[1024]; // Our DB3 + private static S7Server.TSrvCallback TheEventCallBack; // <== Static var containig the callback private static S7Server.TSrvCallback TheReadCallBack; // <== Static var containig the callback diff --git a/S7.Net.UnitTest/S7NetTests.cs b/S7.Net.UnitTest/S7NetTests.cs index 97e262a..d8c8976 100644 --- a/S7.Net.UnitTest/S7NetTests.cs +++ b/S7.Net.UnitTest/S7NetTests.cs @@ -330,6 +330,54 @@ namespace S7.Net.UnitTest Assert.AreEqual(tc.IntVariable111, tc2.IntVariable111); } + /// + /// Tests that a read and a write on addresses bigger than 8192 are executed correctly + /// + [TestMethod] + public void T08_WriteAndReadInt16VariableAddress8192() + { + Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor."); + + // To write a ushort i don't need any cast, only unboxing must be done + ushort val = 8192; + plc.Write("DB2.DBW8192", val); + ushort result = (ushort)plc.Read("DB2.DBW8192"); + Assert.AreEqual(val, result, "A ushort goes from 0 to 64512"); + + // To write a short i need to convert it to UShort, then i need to reconvert the readed value to get + // the negative sign back + // Depending if i'm writing on a DWORD or on a DEC, i will see ushort or short value in the plc + short value = -8192; + Assert.IsTrue(plc.IsConnected, "After connecting, IsConnected must be set to true"); + plc.Write("DB2.DBW8192", value.ConvertToUshort()); + short result2 = ((ushort)plc.Read("DB2.DBW8192")).ConvertToShort(); + Assert.AreEqual(value, result2, "A short goes from -32767 to 32766"); + } + + /// + /// Tests that a read and a write on addresses bigger than 8192 are executed correctly + /// + [TestMethod] + public void T09_WriteAndReadInt16VariableAddress16384() + { + Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor."); + + // To write a ushort i don't need any cast, only unboxing must be done + ushort val = 16384; + plc.Write("DB2.DBW16384", val); + ushort result = (ushort)plc.Read("DB2.DBW16384"); + Assert.AreEqual(val, result, "A ushort goes from 0 to 64512"); + + // To write a short i need to convert it to UShort, then i need to reconvert the readed value to get + // the negative sign back + // Depending if i'm writing on a DWORD or on a DEC, i will see ushort or short value in the plc + short value = -16384; + Assert.IsTrue(plc.IsConnected, "After connecting, IsConnected must be set to true"); + plc.Write("DB2.DBW16384", value.ConvertToUshort()); + short result2 = ((ushort)plc.Read("DB2.DBW16384")).ConvertToShort(); + Assert.AreEqual(value, result2, "A short goes from -32767 to 32766"); + } + #endregion #region Private methods diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index bf63dda..9374fe9 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -243,8 +243,9 @@ namespace S7.Net package.Add(Types.Word.ToByteArray((ushort) (count))); package.Add(Types.Word.ToByteArray((ushort) (DB))); package.Add((byte) dataType); - package.Add((byte) 0); - switch (dataType) + var overflow = (int)(startByteAdr * 8 / 0xffffU); // handles words with address bigger than 8191 + package.Add((byte)overflow); + switch (dataType) { case DataType.Timer: case DataType.Counter: @@ -552,7 +553,8 @@ namespace S7.Net package.Add(Types.Word.ToByteArray((ushort)varCount)); package.Add(Types.Word.ToByteArray((ushort)(db))); package.Add((byte)dataType); - package.Add((byte)0); + var overflow = (int) (startByteAdr*8/0xffffU); // handles words with address bigger than 8191 + package.Add((byte)overflow); package.Add(Types.Word.ToByteArray((ushort)(startByteAdr * 8))); package.Add(new byte[] { 0, 4 }); package.Add(Types.Word.ToByteArray((ushort)(varCount * 8)));