mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 22:38:27 +08:00
add Port Setting for PLC.
is required for port forwarding
This commit is contained in:
@@ -594,7 +594,7 @@ namespace S7.Net.UnitTest
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadBytesReturnsNullIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
TestClass tc = new TestClass();
|
||||
@@ -640,7 +640,7 @@ namespace S7.Net.UnitTest
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadClassWithGenericReturnsNullIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
|
||||
TestClass tc = await notConnectedPlc.ReadClassAsync<TestClass>(DB2);
|
||||
@@ -681,7 +681,7 @@ namespace S7.Net.UnitTest
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadClassWithGenericAndClassFactoryThrowsExceptionPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255",102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
TestClass tc = await notConnectedPlc.ReadClassAsync(() => new TestClass(), DB2);
|
||||
@@ -714,7 +714,7 @@ namespace S7.Net.UnitTest
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadStructThrowsExceptionPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
object tsObj = await notConnectedPlc.ReadStructAsync(typeof(TestStruct), DB2);
|
||||
@@ -757,7 +757,7 @@ namespace S7.Net.UnitTest
|
||||
[ExpectedException(typeof(NullReferenceException))]
|
||||
public async Task Test_Async_ReadStructWithGenericThrowsExceptionIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
object tsObj = await notConnectedPlc.ReadStructAsync<TestStruct>(DB2);
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace S7.Net.UnitTest
|
||||
/// </summary>
|
||||
public S7NetTests()
|
||||
{
|
||||
plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);
|
||||
plc = new Plc(CpuType.S7300, "127.0.0.1", 102, 0, 2);
|
||||
//ConsoleManager.Show();
|
||||
ShutDownServiceS7oiehsx64();
|
||||
|
||||
@@ -602,7 +602,7 @@ namespace S7.Net.UnitTest
|
||||
[TestMethod, ExpectedException(typeof(PlcException))]
|
||||
public void T13_ReadBytesThrowsIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
TestClass tc = new TestClass();
|
||||
@@ -643,7 +643,7 @@ namespace S7.Net.UnitTest
|
||||
[TestMethod, ExpectedException(typeof(PlcException))]
|
||||
public void T15_ReadClassWithGenericThrowsIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");
|
||||
|
||||
@@ -685,7 +685,7 @@ namespace S7.Net.UnitTest
|
||||
[TestMethod, ExpectedException(typeof(PlcException))]
|
||||
public void T17_ReadClassWithGenericAndClassFactoryThrowsIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
|
||||
@@ -746,7 +746,7 @@ namespace S7.Net.UnitTest
|
||||
[TestMethod, ExpectedException(typeof(PlcException))]
|
||||
public void T18_ReadStructThrowsIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
|
||||
@@ -788,7 +788,7 @@ namespace S7.Net.UnitTest
|
||||
[TestMethod, ExpectedException(typeof(PlcException))]
|
||||
public void T20_ReadStructThrowsIfPlcIsNotConnected()
|
||||
{
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
|
||||
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 0))
|
||||
{
|
||||
Assert.IsFalse(notConnectedPlc.IsConnected);
|
||||
|
||||
@@ -909,7 +909,7 @@ namespace S7.Net.UnitTest
|
||||
plc.Close();
|
||||
S7TestServer.Stop();
|
||||
|
||||
var unreachablePlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 2);
|
||||
var unreachablePlc = new Plc(CpuType.S7300, "255.255.255.255", 102, 0, 2);
|
||||
Assert.IsFalse(unreachablePlc.IsAvailable);
|
||||
}
|
||||
|
||||
@@ -920,7 +920,7 @@ namespace S7.Net.UnitTest
|
||||
S7TestServer.Stop();
|
||||
S7TestServer.Start();
|
||||
|
||||
var reachablePlc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);
|
||||
var reachablePlc = new Plc(CpuType.S7300, "127.0.0.1", 102, 0, 2);
|
||||
Assert.IsTrue(reachablePlc.IsAvailable);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,11 @@ namespace S7.Net
|
||||
/// </summary>
|
||||
public string IP { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// PORT Number of the PLC, default is 102
|
||||
/// </summary>
|
||||
public int PORT { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// CPU type of the PLC
|
||||
/// </summary>
|
||||
@@ -107,7 +112,34 @@ namespace S7.Net
|
||||
catch { return false; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a PLC object with all the parameters needed for connections.
|
||||
/// For S7-1200 and S7-1500, the default is rack = 0 and slot = 0.
|
||||
/// You need slot > 0 if you are connecting to external ethernet card (CP).
|
||||
/// For S7-300 and S7-400 the default is rack = 0 and slot = 2.
|
||||
/// </summary>
|
||||
/// <param name="cpu">CpuType of the PLC (select from the enum)</param>
|
||||
/// <param name="ip">Ip address of the PLC</param>
|
||||
/// <param name="port">Port address of the PLC, default 102</param>
|
||||
/// <param name="rack">rack of the PLC, usually it's 0, but check in the hardware configuration of Step7 or TIA portal</param>
|
||||
/// <param name="slot">slot of the CPU of the PLC, usually it's 2 for S7300-S7400, 0 for S7-1200 and S7-1500.
|
||||
/// If you use an external ethernet card, this must be set accordingly.</param>
|
||||
public Plc(CpuType cpu, string ip,int port, Int16 rack, Int16 slot)
|
||||
{
|
||||
if (!Enum.IsDefined(typeof(CpuType), cpu))
|
||||
throw new ArgumentException($"The value of argument '{nameof(cpu)}' ({cpu}) is invalid for Enum type '{typeof(CpuType).Name}'.", nameof(cpu));
|
||||
|
||||
if (string.IsNullOrEmpty(ip))
|
||||
throw new ArgumentException("IP address must valid.", nameof(ip));
|
||||
|
||||
CPU = cpu;
|
||||
IP = ip;
|
||||
PORT = port;
|
||||
Rack = rack;
|
||||
Slot = slot;
|
||||
MaxPDUSize = 240;
|
||||
}
|
||||
/// <summary>
|
||||
/// Creates a PLC object with all the parameters needed for connections.
|
||||
/// For S7-1200 and S7-1500, the default is rack = 0 and slot = 0.
|
||||
@@ -129,6 +161,7 @@ namespace S7.Net
|
||||
|
||||
CPU = cpu;
|
||||
IP = ip;
|
||||
PORT = 102;
|
||||
Rack = rack;
|
||||
Slot = slot;
|
||||
MaxPDUSize = 240;
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace S7.Net
|
||||
{
|
||||
tcpClient = new TcpClient();
|
||||
ConfigureConnection();
|
||||
await tcpClient.ConnectAsync(IP, 102);
|
||||
await tcpClient.ConnectAsync(IP, PORT);
|
||||
stream = tcpClient.GetStream();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user