Merge branch 'master' into master

This commit is contained in:
Raul Hidalgo Caballero
2018-06-28 10:40:11 +02:00
committed by GitHub
7 changed files with 152 additions and 86 deletions

View File

@@ -0,0 +1,82 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using S7.Net.Protocol;
namespace S7.Net.UnitTest
{
[TestClass]
public class ConnectionRequestTest
{
[TestMethod]
public void Test_ConnectionRequest_S7_200()
{
CollectionAssert.AreEqual(MakeConnectionRequest(16, 0, 16, 0),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S7200, 0, 0));
}
[TestMethod]
public void Test_ConnectionRequest_S7_300()
{
CollectionAssert.AreEqual(MakeConnectionRequest(1, 0, 3, 0),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S7300, 0, 0));
CollectionAssert.AreEqual(MakeConnectionRequest(1, 0, 3, 1),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S7300, 0, 1));
CollectionAssert.AreEqual(MakeConnectionRequest(1, 0, 3, 33),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S7300, 1, 1));
}
[TestMethod]
public void Test_ConnectionRequest_S7_400()
{
CollectionAssert.AreEqual(MakeConnectionRequest(1, 0, 3, 0),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S7400, 0, 0));
CollectionAssert.AreEqual(MakeConnectionRequest(1, 0, 3, 1),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S7400, 0, 1));
CollectionAssert.AreEqual(MakeConnectionRequest(1, 0, 3, 33),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S7400, 1, 1));
}
[TestMethod]
public void Test_ConnectionRequest_S7_1200()
{
CollectionAssert.AreEqual(MakeConnectionRequest(1, 0, 3, 0),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S71200, 0, 0));
CollectionAssert.AreEqual(MakeConnectionRequest(1, 0, 3, 1),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S71200, 0, 1));
CollectionAssert.AreEqual(MakeConnectionRequest(1, 0, 3, 33),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S71200, 1, 1));
}
[TestMethod]
public void Test_ConnectionRequest_S7_1500()
{
CollectionAssert.AreEqual(MakeConnectionRequest(0x10, 0x2, 3, 0),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S71500, 0, 0));
CollectionAssert.AreEqual(MakeConnectionRequest(0x10, 0x2, 3, 1),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S71500, 0, 1));
CollectionAssert.AreEqual(MakeConnectionRequest(0x10, 0x2, 3, 33),
ConnectionRequest.GetCOTPConnectionRequest(CpuType.S71500, 1, 1));
}
private static byte[] MakeConnectionRequest(byte sourceTsap1, byte sourceTsap2, byte destTsap1, byte destTsap2)
{
return new byte[]
{
3, 0, 0, 22, //TPKT
17, //COTP Header Length
224, //Connect Request
0, 0, //Destination Reference
0, 46, //Source Reference
0, //Flags
193, //Parameter Code (src-tasp)
2, //Parameter Length
sourceTsap1, sourceTsap2, //Source TASP
194, //Parameter Code (dst-tasp)
2, //Parameter Length
destTsap1, destTsap2, //Destination TASP
192, //Parameter Code (tpdu-size)
1, //Parameter Length
9 //TPDU Size (2^9 = 512)
};
}
}
}

View File

@@ -6,7 +6,7 @@
<ProjectGuid>{303CCED6-9ABC-4899-A509-743341AAA804}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>S7.UnitTest</RootNamespace>
<RootNamespace>S7.Net.UnitTest</RootNamespace>
<AssemblyName>S7Net.UnitTest</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
@@ -60,6 +60,7 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="ConnectionRequestTest.cs" />
<Compile Include="ConvertersUnitTest.cs" />
<Compile Include="ProtocolTests.cs" />
<Compile Include="Helpers\ConsoleManager.cs" />
@@ -119,4 +120,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

View File

@@ -31,7 +31,7 @@ using S7.UnitTest.Helpers;
*
*/
//This file contains tests for the obsolete synchronous methods
//This file contains tests for the synchronous methods
#pragma warning disable CS0618
namespace S7.Net.UnitTest
{
@@ -877,7 +877,7 @@ namespace S7.Net.UnitTest
for (int x = 0; x < count; x++)
{
Assert.AreEqual(x % 256, res[x]);
Assert.AreEqual(x % 256, res[x], $"Mismatch at offset {x}, expected {x % 256}, actual {res[x]}.");
}
}

View File

@@ -357,87 +357,10 @@ namespace S7.Net
}
}
private byte[] GetCOPTConnectionRequest(CpuType CPU)
{
byte[] bSend1 = {
3, 0, 0, 22, //TPKT
17, //COTP Header Length
224, //Connect Request
0, 0, //Destination Reference
0, 46, //Source Reference
0, //Flags
193, //Parameter Code (src-tasp)
2, //Parameter Length
1, 0, //Source TASP
194, //Parameter Code (dst-tasp)
2, //Parameter Length
3, 0, //Destination TASP
192, //Parameter Code (tpdu-size)
1, //Parameter Length
9 //TPDU Size (2^9 = 512)
};
switch (CPU)
{
case CpuType.S7200:
//S7200: Chr(193) & Chr(2) & Chr(16) & Chr(0) 'Eigener Tsap
bSend1[11] = 193;
bSend1[12] = 2;
bSend1[13] = 16;
bSend1[14] = 0;
//S7200: Chr(194) & Chr(2) & Chr(16) & Chr(0) 'Fremder Tsap
bSend1[15] = 194;
bSend1[16] = 2;
bSend1[17] = 16;
bSend1[18] = 0;
break;
case CpuType.S71200:
case CpuType.S7300:
//S7300: Chr(193) & Chr(2) & Chr(1) & Chr(0) 'Eigener Tsap
bSend1[11] = 193;
bSend1[12] = 2;
bSend1[13] = 1;
bSend1[14] = 0;
//S7300: Chr(194) & Chr(2) & Chr(3) & Chr(2) 'Fremder Tsap
bSend1[15] = 194;
bSend1[16] = 2;
bSend1[17] = 3;
bSend1[18] = (byte)(Rack * 2 * 16 + Slot);
break;
case CpuType.S7400:
//S7400: Chr(193) & Chr(2) & Chr(1) & Chr(0) 'Eigener Tsap
bSend1[11] = 193;
bSend1[12] = 2;
bSend1[13] = 1;
bSend1[14] = 0;
//S7400: Chr(194) & Chr(2) & Chr(3) & Chr(3) 'Fremder Tsap
bSend1[15] = 194;
bSend1[16] = 2;
bSend1[17] = 3;
bSend1[18] = (byte)(Rack * 2 * 16 + Slot);
break;
case CpuType.S71500:
// Eigener Tsap
bSend1[11] = 193;
bSend1[12] = 2;
bSend1[13] = 0x10;
bSend1[14] = 0x2;
// Fredmer Tsap
bSend1[15] = 194;
bSend1[16] = 2;
bSend1[17] = 0x3;
bSend1[18] = (byte)(Rack * 2 * 16 + Slot);
break;
default:
throw new Exception("Wrong CPU Type Secified");
}
return bSend1;
}
private byte[] GetS7ConnectionSetup()
{
return new byte[] { 3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 255, 255, 0, 8, 0, 0, 240, 0, 0, 3, 0, 3,
7, 80 //Try 1920 PDU Size. Same as libnodave.
7, 128 //Try 1920 PDU Size. Same as libnodave.
};
}

View File

@@ -24,7 +24,7 @@ namespace S7.Net
{
await ConnectAsync();
await stream.WriteAsync(GetCOPTConnectionRequest(CPU), 0, 22);
await stream.WriteAsync(ConnectionRequest.GetCOTPConnectionRequest(CPU, Rack, Slot), 0, 22);
var response = await COTP.TPDU.ReadAsync(stream);
if (response.PDUType != 0xd0) //Connect Confirm
{

View File

@@ -8,7 +8,7 @@ using System.Net.Sockets;
using System.Threading.Tasks;
using S7.Net.Protocol;
//Implement obsolete synchronous methods here
//Implement synchronous methods here
namespace S7.Net
{
public partial class Plc
@@ -26,7 +26,7 @@ namespace S7.Net
}
try
{
stream.Write(GetCOPTConnectionRequest(CPU), 0, 22);
stream.Write(ConnectionRequest.GetCOTPConnectionRequest(CPU, Rack, Slot), 0, 22);
var response = COTP.TPDU.Read(stream);
if (response.PDUType != 0xd0) //Connect Confirm
{
@@ -519,7 +519,6 @@ namespace S7.Net
/// DataItems must not be more than 20 (protocol restriction) and bytes must not be more than 200 + 22 of header (protocol restriction).
/// </summary>
/// <param name="dataItems">List of dataitems that contains the list of variables that must be read. Maximum 20 dataitems are accepted.</param>
[Obsolete("Use ReadMultipleVarsAsync. Note: different function signature")]
public void ReadMultipleVars(List<DataItem> dataItems)
{
int cntBytes = dataItems.Sum(dataItem => VarTypeToByteLength(dataItem.VarType, dataItem.Count));

View File

@@ -0,0 +1,61 @@
using System;
namespace S7.Net.Protocol
{
internal static class ConnectionRequest
{
public static byte[] GetCOTPConnectionRequest(CpuType cpu, Int16 rack, Int16 slot)
{
byte[] bSend1 = {
3, 0, 0, 22, //TPKT
17, //COTP Header Length
224, //Connect Request
0, 0, //Destination Reference
0, 46, //Source Reference
0, //Flags
193, //Parameter Code (src-tasp)
2, //Parameter Length
1, 0, //Source TASP
194, //Parameter Code (dst-tasp)
2, //Parameter Length
3, 0, //Destination TASP
192, //Parameter Code (tpdu-size)
1, //Parameter Length
9 //TPDU Size (2^9 = 512)
};
switch (cpu)
{
case CpuType.S7200:
//S7200: Chr(193) & Chr(2) & Chr(16) & Chr(0) 'Eigener Tsap
bSend1[13] = 0x10;
bSend1[14] = 0x00;
//S7200: Chr(194) & Chr(2) & Chr(16) & Chr(0) 'Fremder Tsap
bSend1[17] = 0x10;
bSend1[18] = 0x00;
break;
case CpuType.S71200:
case CpuType.S7300:
case CpuType.S7400:
//S7300: Chr(193) & Chr(2) & Chr(1) & Chr(0) 'Eigener Tsap
bSend1[13] = 0x01;
bSend1[14] = 0x00;
//S7300: Chr(194) & Chr(2) & Chr(3) & Chr(2) 'Fremder Tsap
bSend1[17] = 0x03;
bSend1[18] = (byte) ((rack << 5) | (int) slot);
break;
case CpuType.S71500:
// Eigener Tsap
bSend1[13] = 0x10;
bSend1[14] = 0x02;
// Fredmer Tsap
bSend1[17] = 0x03;
bSend1[18] = (byte) ((rack << 5) | (int) slot);
break;
default:
throw new Exception("Wrong CPU Type Secified");
}
return bSend1;
}
}
}