Home > database >  TwinCat3 - TCP/IP disconnects randomly
TwinCat3 - TCP/IP disconnects randomly

Time:11-08

I am using following code to set up a TCP IP server. I am connecting to it from my laptop by using hercules. For a reason I don't understand, it disconnects after a random amount of seconds between 1 and 20. The error I get is "Socket handle is invalid " (32770) on the fbSocketReceive.

What I would expect:

cycle should stay in step 3, and remain connected unless the connection is closed remotely or cable is disconnected. If something is sent, it should be processed.

What is happening:

cycle stays in step 3 as soon as a client connects, and after a random amount of seconds (1 to 20) I get the 32770 error on the fbSocketReceive.

I cannot figure out why it loses the Socket handle...

    // Init Step to close all sockets after login PLC with download or when starting PLC
    IF bInit
    THEN
        fbSocketCloseAll(
            sSrvNetId:='', 
            bExecute:=TRUE, 
            tTimeout:=T#3S, 
            bBusy=> , 
            bError=>bSocketCloseError, 
            nErrId=>nSocketCloseError);
        IF NOT (fbSocketCloseAll.bBusy OR fbSocketCloseAll.bError)
        THEN
            bInit:=FALSE;
            fbSocketCloseAll(bExecute:=FALSE);
        END_IF
        RETURN;
    END_IF
    
// Reset Flag
    IF bDataTxChanged
    THEN
        bDataTxChanged:=FALSE;
    END_IF
    
// Reset Flag
    IF bNewDataReceived
    THEN
        bNewDataReceived:=FALSE;
    END_IF



// Cycle
    CASE iState OF
        
        0:  // Init State
        
            IF bEnable
            THEN
                fbSocketListen(bExecute:=FALSE);
                fbSocketAccept(bExecute:=FALSE);
                fbSocketCloseAll(bExecute:=FALSE);
                fbSocketClose(bExecute:=FALSE);
                bBusy:=TRUE;
                iState:=1;
            ELSE
                bBusy:=FALSE;
            END_IF

        1:  // Open Listener-Socket

            fbSocketListen(
                sSrvNetId:='', 
                sLocalHost:=sLocalHost, 
                nLocalPort:=nLocalPort, 
                bExecute:=TRUE, 
                tTimeout:=T#14S, 
                bBusy=> , 
                bError=>bError, 
                nErrId=>nErrorID,
                hListener=>hListener);

            IF hListener.handle <> 0
            THEN
                iState:=2;
            ELSIF bError
            THEN
                iState:=99;
            END_IF

        2:  // Accept Client connection

            fbSocketAccept(
                sSrvNetId:='', 
                hListener:=hListener, 
                bExecute:= bAcceptExecute:= NOT bAcceptExecute, 
                tTimeout:=T#14S, 
                bAccepted=> , 
                bBusy=> , 
                bError=>bError, 
                nErrId=>nErrorID,
                hSocket=>hSocket);

            IF bError
            THEN
                iState:=99;
            ELSIF hSocket.handle <> 0
            THEN
                iState:=3;
                bConnected:=TRUE;
            END_IF

        3:  // client connected, send and receive data
            
            fbSocketReceive(
                sSrvNetId:='', 
                hSocket:=hSocket, 
                cbLen:=SIZEOF(stDataRx), 
                pDest:=ADR(stDataRx), 
                bExecute:= bReceiveExecute:= NOT bReceiveExecute,                   // Check for new client telegrams every second cycle
                tTimeout:=T#4S, 
                bBusy=> , 
                bError=>bError, 
                nErrId=>nErrorID,
                nRecBytes=> );
                    
            IF fbSocketReceive.nRecBytes <> 0                                       // Switch to send-state when data are received
            THEN
                fbSocketSend(bExecute:=FALSE);
                nCntRx:=nCntRx 1;
                bNewDataReceived:=TRUE;
            END_IF
                                
            IF fbSocketReceive.bError OR(NOT bEnable)                           // Close connection when error is occuring or trigger is set                    
            THEN
                iState:=99;
            END_IF


        99: // Error Handling

            fbSocketClose(                                                          // Close Listener-Socket
                sSrvNetId:='', 
                hSocket:=hListener, 
                bExecute:=TRUE, 
                tTimeout:=T#4S, 
                bBusy=> , 
                bError=>bError, 
                nErrId=>nErrorID);

            IF (NOT fbSocketClose.bBusy) OR fbSocketClose.bError
            THEN
                hListener.handle:=0;
                iState:=100;
            END_IF

        100:

            fbSocketClose(bExecute := FALSE);
            iState:=101;

        101:
            fbSocketClose(                                                          // Close Connection Socket
                sSrvNetId:='', 
                hSocket:=hSocket, 
                bExecute:=TRUE, 
                tTimeout:=T#4S, 
                bBusy=> , 
                bError=>bError, 
                nErrId=>nErrorID);

            IF NOT (fbSocketClose.bBusy) OR fbSocketClose.bError THEN
                hSocket.handle:=0;  
                iState:=0;
                bConnected:=FALSE;
            END_IF

    END_CASE

CodePudding user response:

I have found the problem.

In another FB, the FB Tcp_CloseAllSocket was used. this killed also the sockets from this FB.

  • Related