Use socket instead of TcpClient and stream.

Async implemented with wrapper.
This commit is contained in:
Thomas Jäger
2018-05-16 11:24:21 +02:00
parent b8b890977e
commit 1b34716a30
10 changed files with 399 additions and 183 deletions

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using S7.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;
@@ -13,18 +14,22 @@ namespace S7.Net.UnitTest
[TestClass]
public class ProtocolUnitTest
{
[TestMethod]
public void TPKT_Read()
{
var m = new MemoryStream(StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd"));
var t = TPKT.Read(m);
Assert.AreEqual(0x03, t.Version);
Assert.AreEqual(0x29, t.Length);
m.Position = 0;
t = TPKT.ReadAsync(m).Result;
Assert.AreEqual(0x03, t.Version);
Assert.AreEqual(0x29, t.Length);
}
/*
* Not sure how to implment these tests cleanly now. Probably need to spin up a TcpServer.
[TestMethod]
public void TPKT_Read()
{
Socket s = new Socket(SocketType.Stream, ProtocolType.Tcp);
s.Send(StringToByteArray("0300002902f0803203000000010002001400000401ff0400807710000100000103000000033f8ccccd"));
//Socket m = new MemoryStream();
var t = TPKT.Read(s);
Assert.AreEqual(0x03, t.Version);
Assert.AreEqual(0x29, t.Length);
//m.Position = 0;
//t = TPKT.ReadAsync(m).Result;
//Assert.AreEqual(0x03, t.Version);
//Assert.AreEqual(0x29, t.Length);
}
[TestMethod]
[ExpectedException(typeof(TPKTInvalidException))]
@@ -53,7 +58,7 @@ namespace S7.Net.UnitTest
t = COTP.TSDU.ReadAsync(m).Result;
Assert.IsTrue(expected.SequenceEqual(t));
}
*/
private static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
@@ -61,5 +66,7 @@ namespace S7.Net.UnitTest
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}
}

View File

@@ -162,13 +162,15 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClass tc = new TestClass();
tc.BitVariable00 = true;
tc.BitVariable10 = true;
tc.DIntVariable = -100000;
tc.IntVariable = -15000;
tc.RealVariable = -154.789;
tc.DWordVariable = 850;
TestClass tc = new TestClass
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
RealVariable = -154.789,
DWordVariable = 850
};
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
@@ -189,13 +191,15 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestStruct tc = new TestStruct();
tc.BitVariable00 = true;
tc.BitVariable10 = true;
tc.DIntVariable = -100000;
tc.IntVariable = -15000;
tc.RealVariable = -154.789;
tc.DWordVariable = 850;
TestStruct tc = new TestStruct
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
RealVariable = -154.789,
DWordVariable = 850
};
plc.WriteStruct(tc, DB2);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
TestStruct tc2 = (TestStruct)plc.ReadStruct(typeof(TestStruct), DB2);
@@ -215,31 +219,33 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestLongStruct tc = new TestLongStruct();
tc.IntVariable0 = 0;
tc.IntVariable1 = 1;
tc.IntVariable10 = 10;
tc.IntVariable11 = 11;
tc.IntVariable20 = 20;
tc.IntVariable21 = 21;
tc.IntVariable30 = 30;
tc.IntVariable31 = 31;
tc.IntVariable40 = 40;
tc.IntVariable41 = 41;
tc.IntVariable50 = 50;
tc.IntVariable51 = 51;
tc.IntVariable60 = 60;
tc.IntVariable61 = 61;
tc.IntVariable70 = 70;
tc.IntVariable71 = 71;
tc.IntVariable80 = 80;
tc.IntVariable81 = 81;
tc.IntVariable90 = 90;
tc.IntVariable91 = 91;
tc.IntVariable100 = 100;
tc.IntVariable101 = 101;
tc.IntVariable110 = 200;
tc.IntVariable111 = 201;
TestLongStruct tc = new TestLongStruct
{
IntVariable0 = 0,
IntVariable1 = 1,
IntVariable10 = 10,
IntVariable11 = 11,
IntVariable20 = 20,
IntVariable21 = 21,
IntVariable30 = 30,
IntVariable31 = 31,
IntVariable40 = 40,
IntVariable41 = 41,
IntVariable50 = 50,
IntVariable51 = 51,
IntVariable60 = 60,
IntVariable61 = 61,
IntVariable70 = 70,
IntVariable71 = 71,
IntVariable80 = 80,
IntVariable81 = 81,
IntVariable90 = 90,
IntVariable91 = 91,
IntVariable100 = 100,
IntVariable101 = 101,
IntVariable110 = 200,
IntVariable111 = 201
};
plc.WriteStruct(tc, DB2);
Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
@@ -279,31 +285,33 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestLongClass tc = new TestLongClass();
tc.IntVariable0 = 0;
tc.IntVariable1 = 1;
tc.IntVariable10 = 10;
tc.IntVariable11 = 11;
tc.IntVariable20 = 20;
tc.IntVariable21 = 21;
tc.IntVariable30 = 30;
tc.IntVariable31 = 31;
tc.IntVariable40 = 40;
tc.IntVariable41 = 41;
tc.IntVariable50 = 50;
tc.IntVariable51 = 51;
tc.IntVariable60 = 60;
tc.IntVariable61 = 61;
tc.IntVariable70 = 70;
tc.IntVariable71 = 71;
tc.IntVariable80 = 80;
tc.IntVariable81 = 81;
tc.IntVariable90 = 90;
tc.IntVariable91 = 91;
tc.IntVariable100 = 100;
tc.IntVariable101 = 101;
tc.IntVariable110 = 200;
tc.IntVariable111 = 201;
TestLongClass tc = new TestLongClass
{
IntVariable0 = 0,
IntVariable1 = 1,
IntVariable10 = 10,
IntVariable11 = 11,
IntVariable20 = 20,
IntVariable21 = 21,
IntVariable30 = 30,
IntVariable31 = 31,
IntVariable40 = 40,
IntVariable41 = 41,
IntVariable50 = 50,
IntVariable51 = 51,
IntVariable60 = 60,
IntVariable61 = 61,
IntVariable70 = 70,
IntVariable71 = 71,
IntVariable80 = 80,
IntVariable81 = 81,
IntVariable90 = 90,
IntVariable91 = 91,
IntVariable100 = 100,
IntVariable101 = 101,
IntVariable110 = 200,
IntVariable111 = 201
};
plc.WriteClass(tc, DB2);
Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
@@ -477,13 +485,15 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClassWithPrivateSetters tc = new TestClassWithPrivateSetters();
tc.BitVariable00 = true;
tc.BitVariable10 = true;
tc.DIntVariable = -100000;
tc.IntVariable = -15000;
tc.RealVariable = -154.789;
tc.DWordVariable = 850;
TestClassWithPrivateSetters tc = new TestClassWithPrivateSetters
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
RealVariable = -154.789,
DWordVariable = 850
};
plc.WriteClass(tc, DB2);
@@ -525,13 +535,15 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClass tc = new TestClass();
tc.BitVariable00 = true;
tc.BitVariable10 = true;
tc.DIntVariable = -100000;
tc.IntVariable = -15000;
tc.RealVariable = -154.789;
tc.DWordVariable = 850;
TestClass tc = new TestClass
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
RealVariable = -154.789,
DWordVariable = 850
};
plc.WriteClass(tc, DB2);
@@ -566,13 +578,15 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClass tc = new TestClass();
tc.BitVariable00 = true;
tc.BitVariable10 = true;
tc.DIntVariable = -100000;
tc.IntVariable = -15000;
tc.RealVariable = -154.789;
tc.DWordVariable = 850;
TestClass tc = new TestClass
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
RealVariable = -154.789,
DWordVariable = 850
};
plc.WriteClass(tc, DB2);
@@ -619,13 +633,15 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestStruct ts = new TestStruct();
ts.BitVariable00 = true;
ts.BitVariable10 = true;
ts.DIntVariable = -100000;
ts.IntVariable = -15000;
ts.RealVariable = -154.789;
ts.DWordVariable = 850;
TestStruct ts = new TestStruct
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
RealVariable = -154.789,
DWordVariable = 850
};
plc.WriteStruct(ts, DB2);
@@ -662,13 +678,15 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClass tc = new TestClass();
tc.BitVariable00 = true;
tc.BitVariable10 = true;
tc.DIntVariable = -100000;
tc.IntVariable = -15000;
tc.RealVariable = -154.789;
tc.DWordVariable = 850;
TestClass tc = new TestClass
{
BitVariable00 = true,
BitVariable10 = true,
DIntVariable = -100000,
IntVariable = -15000,
RealVariable = -154.789,
DWordVariable = 850
};
plc.WriteClass(tc, DB2);
int expectedReadBytes = Types.Class.GetClassSize(tc);
@@ -685,8 +703,10 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClassWithArrays tc = new TestClassWithArrays();
tc.Bool = true;
TestClassWithArrays tc = new TestClassWithArrays
{
Bool = true
};
tc.BoolValues[1] = true;
tc.Int = int.MinValue;
tc.Ints[0] = int.MinValue;
@@ -730,9 +750,11 @@ namespace S7.Net.UnitTest
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
TestClassWithCustomType tc = new TestClassWithCustomType();
tc.Int = int.MinValue;
tc.CustomType = new CustomType();
TestClassWithCustomType tc = new TestClassWithCustomType
{
Int = int.MinValue,
CustomType = new CustomType()
};
tc.CustomType.Bools[1] = true;
tc.CustomTypes[0] = new CustomType();
tc.CustomTypes[1] = new CustomType();