mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 22:38:27 +08:00
fix Issue #38: now read and write bytes handles address overflow correctly.
Signed-off-by: Michele Cattafesta <michele.cattafesta@mesta-automation.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -330,6 +330,54 @@ namespace S7.Net.UnitTest
|
||||
Assert.AreEqual(tc.IntVariable111, tc2.IntVariable111);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that a read and a write on addresses bigger than 8192 are executed correctly
|
||||
/// </summary>
|
||||
[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");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that a read and a write on addresses bigger than 8192 are executed correctly
|
||||
/// </summary>
|
||||
[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
|
||||
|
||||
@@ -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)));
|
||||
|
||||
Reference in New Issue
Block a user