netPing (Functionblock)

Top  Previous  Next

Architecture:

NX32L

Device support:

ALL

Firmware version:

1.52.00


This function sends ICMP-echo (ping) packets to a designated host IP interface and reports statistics on the reply.

For situation that do not require the detailed statistics, the simpler netPingS function can be used instead.

 

 

Input:

iface : SINT (default 2)

The network interface. 1 = Mobile network, 2 = LAN network, etc. (See Network)

 

IP : DINT

The IP address of the interface on the host to check if it answers.

 

Count : UINT (default 5)

How many ping packets to send.

 

Reply_timeout : UINT (default 1000)

The time to wait for a reply from the host in milliseconds.

 

 

Output:

Status : INT

1

- Success.

0

- Function is not supported.

-1

- General error.

-2

- Invalid input

-3

- Interface is not connected. Use netOpen.

 

Packets : INT

Number of ping packets received from the request.

 

Loss : INT

Number of ping packets with no reply.

 

TimeMin : FLOAT

Minimum round trip time in milliseconds.

 

TimeMax : FLOAT

Maximum round trip time in milliseconds.

 

TimeAv : FLOAT

Average round trip time in milliseconds.

 

 

Declaration:

FUNCTION_BLOCK netPing;
VAR_INPUT
  iface          : SINT := 2;
  IP             : DINT;
  Count          : UINT := 5;
  Reply_timeout  : UINT := 1000;
END_VAR;
VAR_OUTPUT
  Status         : INT;
  Packets        : INT;
  Loss           : INT;
  TimeMin        : FLOAT;
  TimeMax        : FLOAT;
  TimeAv         : FLOAT;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR
  ping     : netPing;
  netInfo  : netGetInformation;
  iface    : SINT := 2;
END_VAR;
 
FUNCTION printInfo
netInfo(iface:=iface);
DebugFmt(message:="Network interface \1:", v1 := iface);
CASE netInfo.status OF
    0 : DebugMsg(message:="Interface is not present.");
    1 : DebugMsg(message:="Interface is not opened.");
    2 : DebugMsg(message:="Interface is not connected.");
    3 : DebugMsg(message:="Interface is connected.");
    4 : DebugMsg(message:="Interface link is up and waiting for IP configuration.");
ELSE
    DebugFmt(message:="Illegal net status? (\1)", v1:=netInfo.status);
END_CASE;
IF netInfo.status > 1 THEN
  DebugMsg(message:=" Phy addr " + netInfo.phy_addr);
  IF netInfo.dhcp THEN
      DebugMsg(message:=" Dynamic IP address");
  ELSE
      DebugMsg(message:=" Static IP address");
  END_IF;
  DebugMsg(message:=" IP addr " + sockIPToName(ip := netInfo.ip));
  DebugMsg(message:=" Mask    " + sockIPToName(ip := netInfo.subnetMask));
  DebugMsg(message:=" Gateway " + sockIPToName(ip := netInfo.gateway));
  IF netInfo.AutoDNS THEN
      DebugMsg(message:=" Automatic DNS");
  ELSE
      DebugMsg(message:=" Manual DNS");
  END_IF;
  DebugMsg(message:=" DNS1    " + sockIPToName(ip := netInfo.DNS1));
  DebugMsg(message:=" DNS2    " + sockIPToName(ip := netInfo.DNS2));
END_IF;
END_FUNCTION;
 
FUNCTION PrintPing;
  DebugMsg(message:="---------------input-----------------------------");
  DebugFmt(message:="Network interface \1:", v1 := ping.iface);
  DebugMsg(message:="IP address is "+soAddrFromIP(ip:=ping.ip));
  DebugFmt(message:="Send \1 packets.", v1:=ping.count);
  DebugFmt(message:="Send for time: \1 seconds.", v1:=ping.timeout);
  DebugMsg(message:="---------------output----------------------------");
  IF ping.status > 0 THEN
    DebugFmt(message:="Received \1 packets.", v1:=ping.packets);
    DebugFmt(message:="Loss ratio was: \1", v1:=ping.loss);
    DebugMsg(message:="Minimum round time was "+floatToStr(v:=ping.timemin)+"ms.");
    DebugMsg(message:="Maximum round time was "+floatToStr(v:=ping.timemax)+"ms.");
    DebugMsg(message:="Average round time was "+floatToStr(v:=ping.timeav)+"ms.");
  ELSE
    DebugFmt(message:="Send status is \1", v1:=ping.status);
  END_IF;
  DebugMsg(message:="-------------------------------------------------");
END_FUNCTION;
 
PROGRAM pingtest;
VAR
  rc : INT;
END_VAR;
rc := netOpen(iface:=iface);
DebugFmt(message:="netOpen (rc=\1)",v1:=rc);
WHILE NOT netConnected(iface:=iface) DO
  Sleep(delay:=2000);
END_WHILE;
printinfo();
 
// Send 10 ping packets to host
ping(iface:=iface,
    ip:=sockIPFromName(str:="gw.rtcu.dk"),
    count:=10,
    reply_timeout:=500);
 
// Print ping input settings and output statistics
printPing();
END_PROGRAM;