CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Tue Sep 17, 2019 9:21 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Jan 23, 2019 12:10 pm 
Offline

Joined: Mon Jan 14, 2019 7:03 pm
Posts: 2
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


Top
   
PostPosted: Thu Jan 24, 2019 2:07 pm 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 151
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


Top
   
PostPosted: Fri Jan 25, 2019 1:08 pm 
Offline

Joined: Mon Jan 14, 2019 7:03 pm
Posts: 2
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 :?


Top
   
PostPosted: Tue Jan 29, 2019 9:33 am 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 151
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)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 4 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited