CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

Ping with SysSockPing and Connection Time Out
https://forum.codesys.com/viewtopic.php?f=11&t=9466
Page 1 of 1

Author:  LeonardoP- [ Wed Jan 23, 2019 12:10 pm ]
Post subject:  Ping with SysSockPing and Connection Time Out

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 ?


Code:
  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


Code:
 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

Author:  dFx [ Thu Jan 24, 2019 2:07 pm ]
Post subject:  Re: Ping with SysSockPing and Connection Time Out

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

This is my working code :
Global Vars
Code:
VAR_GLOBAL
// Diagnostic of TCP link
PingOk : BOOL;
END_VAR

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

Program
Code:
VAR
   PingTime: UDINT;
END_VAR

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

Author:  LeonardoP- [ Fri Jan 25, 2019 1:08 pm ]
Post subject:  Re: Ping with SysSockPing and Connection Time Out

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

This is my working code :
Global Vars
Code:
VAR_GLOBAL
// Diagnostic of TCP link
PingOk : BOOL;
END_VAR

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

Program
Code:
VAR
   PingTime: UDINT;
END_VAR

Code:
// 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 :?

Author:  dFx [ Tue Jan 29, 2019 9:33 am ]
Post subject:  Re: Ping with SysSockPing and Connection Time Out

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)

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/