Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Ping with SysSockPing and Connection Time Out

Anonymous
2019-01-23
2019-01-29
  • Anonymous - 2019-01-23

    Originally created by: LeonardoP-

    Hello community

    1º - I'm trying to test the Internet conection in my device. I got the idea of "doing a ping" to google, for example.
    Apparently I can't do it directly, so i tried to open a socket in my application and use the function "SysSockPing" from SysSocket.lib. But always than a tried run it returns "1", in the CmpErrors it means "ERROR FAILED". For my surprise if i connect then send a mensage and then receive it actually works. What I'm doing wrong ?

    2º- The time out of SysSockConnect is too long (1 minute approximately). Can i change it ?

      PROGRAM PingTest
    VAR
     //// Create TCP socket ////
     diSocket : RTS_IEC_HANDLE;
     diAddressFamily : INT := SOCKET_AF_INET;
     diType : DINT := SOCKET_STREAM;
     diProtocol : DINT := SOCKET_IPPROTO_TCP;
     pResult: RTS_IEC_RESULT;
     
     //// Non-Blocking mode ////
     pResult6 : RTS_IEC_RESULT;
     diCommand : DINT;
     pdiParameter : POINTER TO DINT;
     
     //// Connect to server ////
      sockAddr : SOCKADDRESS;
      result: RTS_IEC_RESULT;
      pResult3: SYSSOCKET.RTS_IEC_RESULT;
      //pInAddr: INT;
     
       
     //sockAddr.sin_family := SOCKET_AF_INET;
    // sockAddr.sin_addr := SysSockInetAddr('192.168.4.102');
     //sockAddr.sin_port := SysSockHtons(19998);
     
     //// PING /////
       PingTime: UDINT;
       pResult2: RTS_IEC_RESULT;
       
     //// SEND  /////
       send: DINT;
       hSocket : RTS_IEC_HANDLE;
       pResult4: RTS_IEC_RESULT;
       pbyBuffer: POINTER TO BYTE;
       diBufferSize: INT;
       diFlags: INT;
       Str : BYTE :=66; //caractere ASCII em DEC
    ///// REC /////   
       rec : DINT;
       Str2 : BYTE;
       pResult5 : RTS_IEC_RESULT;
    //// TESTE //// 
       state : BOOL := FALSE;   
       state2 : BOOL := FALSE;
       checkfail : INT;
       cont: INT; 
    END_VAR
    
     cont := 0;
     checkfail := 0;
     GVL.ResetCode := 1;
     
     //Create TCP socket
    diSocket := SysSockCreate(diAddressFamily, diType, diProtocol, ADR(pResult));
    //Non-Blocking mode
    pResult5 := SysSockIoctl( hSocket := diSocket, diCommand := SOCKET_FIONBIO, ADR(pdiParameter));
    //Connect to server
    sockAddr.sin_family := SOCKET_AF_INET;
    pResult3 := SysSockInetAddr('192.168.3.73', ADR(sockAddr.sin_addr));
    sockAddr.sin_port := SysSockHtons(1234);
    result := SysSockConnect(diSocket, ADR(sockAddr), SIZEOF(sockAddr));
    WHILE (cont < 10) DO
       
    IF (result =0) THEN
    //Ping server
     pResult2 := SysSockPing(szIPAddress:='192.168.3.78', ulTimeout:=500,  ADR(PingTime));
    // Send Byte
     send := SysSockSend(hSocket := diSocket , pbyBuffer :=ADR(Str), diBufferSize := 1, diFlags:=0 , ADR(pResult4)); 
    // Rec Byte
     rec := SysSockRecv(hSocket := diSocket, pbyBuffer:= ADR(Str2), diBufferSize := 1, diFlags :=0, ADR(pResult5)); 
    END_IF
    //TesteSend = Rec
    IF (Str2 = Str) THEN
       Str2 := 0;
       state := TRUE;
       checkfail := 0;
    ELSE
       state := FALSE;
       checkfail := (checkfail + 1); 
    END_IF
    cont := (cont + 1);
    END_WHILE
     //Close connection
       SysSockClose(diSocket);
    IF (checkfail >= 5)THEN
       state2 := TRUE; //teste conexão após os 10 ciclos, sendo true = conexão fail
       GVL.ResetCode := 0;
       ELSE
       GVL.ResetCode := 1;
       state2 := FALSE;
       END_IF
    
     
  • dFx

    dFx - 2019-01-24

    Had to issue a ping for an app, using festo hardware running codesys v3.5 SP10

    This is my working code :
    Global Vars

    VAR_GLOBAL
    // Diagnostic of TCP link
    PingOk : BOOL;
    END_VAR
    

    Global constants

    {attribute 'qualified_only'}
    VAR_GLOBAL CONSTANT
       _IPSTRING: STRING := '192.168.1.1'; // IP to ping
    END_VAR
    

    Program

    VAR
       PingTime: UDINT;
    END_VAR
    
    // Ping command
    IF CmpErrors.Errors.ERR_OK = SysSockPing(szIPAddress:=Const._IPSTRING, ulTimeout:=500, pulReplyTime:= ADR(PingTime)) THEN
       PingOk := TRUE;   
    ELSE
       PingOk := FALSE;   
    END_IF
    
     
  • Anonymous - 2019-01-25

    Originally created by: LeonardoP-

    dFx hat geschrieben:
    Had to issue a ping for an app, using festo hardware running codesys v3.5 SP10
    This is my working code :
    Global Vars

    VAR_GLOBAL
    // Diagnostic of TCP link
    PingOk : BOOL;
    END_VAR
    

    Global constants

    {attribute 'qualified_only'}
    VAR_GLOBAL CONSTANT
       _IPSTRING: STRING := '192.168.1.1'; // IP to ping
    END_VAR
    

    Program

    VAR
       PingTime: UDINT;
    END_VAR
    
    // Ping command
    IF CmpErrors.Errors.ERR_OK = SysSockPing(szIPAddress:=Const._IPSTRING, ulTimeout:=500, pulReplyTime:= ADR(PingTime)) THEN
       PingOk := TRUE;   
    ELSE
       PingOk := FALSE;   
    END_IF
    

    I tried the same code and it continues returning cmperror =1

     
  • dFx

    dFx - 2019-01-29

    Did you tryied pinging a local IP address (on the same subnet) ?

    Please check your gateway settings. If it is ok, and your Gateway can reach internet, you should be able to ping 8.8.8.8 (google dns server)

     

Log in to post a comment.