S7NetPlus / s7netplus

S7.NET+ -- A .NET library to connect to Siemens Step7 devices
MIT License
1.3k stars 580 forks source link

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host #457

Closed GronbergInfo closed 1 year ago

GronbergInfo commented 1 year ago

Hi, i have a S7-1214FC on my desk, and have followed the instriuctions to enable full access and enable PUT/GET, so far so good i think.

But as soon i try to connect to the plc i get the following exception, i have tried to reset hardware, reboot the plc. TIA Portal V15.1 Update 7 can connect "just fine", but im not sure whats actually "wrong" here.

PLCExcetion\r\nS7.Net.PlcException: Couldn't establish the connection to 192.168.0.65. Message: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state) at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.IO.Stream.<>c.b43_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state) at System.Threading.Tasks.TaskFactory1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func5 beginMethod, Func`3 endMethod) at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count) at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) at S7.Net.StreamExtensions.d1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at S7.Net.TPKT.d5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at S7.Net.COTP.TPDU.d9.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at S7.Net.Plc.d80.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at S7.Net.Plc.d56.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at S7.Net.Plc.d55.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at S7.Net.Plc.<>c__DisplayClass53_0.<b0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at S7.Net.Internal.TaskQueue.d2`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at S7.Net.Plc.d53.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at S7.Net.Plc.Open() --- End of inner exception stack trace --- at S7.Net.Plc.Open() at S7NetPLC.Program.ConnectToPLC() in C:\Users\stefan.gronberg\OneDrive\Documents\VSCode\S7Net\S7Net\Program.cs:line 34

My current .Net-code is

    internal class Program
    {
        static Plc splc = new Plc(CpuType.S71200, "192.168.0.65", 0, 2);
        static void Main(string[] args)
        {
            ConnectToPLC();

            Console.ReadKey();
            splc.Close();
        }

        static void ConnectToPLC()
        {

            try
            {

                //splc.CPU = CpuType.S71200;
                //splc.IP = "192.168.0.65";
                //splc.Rack = 0;
                //splc.Slot = 2;

                CWL("Connecting to PLC");
                splc.Open();
            }
            catch (PlcException ex)
            {
                CWL("PLCExcetion\\r\\n" + ex.ToString());
            }
            catch(Exception ex)
            {
                CWL("Unhandled Exception\\r\\n" + ex.ToString());
            }

        }

        static void CWL(String Text)
        {
            Console.WriteLine(Text);
        }
    }
GronbergInfo commented 1 year ago

Excuse my dim head, but i figured it out.. On S7-1200 its Rack 0, Slot 1, and now my connection works _:D I have been trying various other approaches the past 2-3 weeks, trying to use Siemens LCCP_ENet and LCCP_CipServer blocks, an anybus gateway between and other ways.

mycroes commented 1 year ago

No worries, it's not like we have great documentation that would help you.