netGetInformation (Functionblock)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

4.34 / 1.00.00


This returns information about a network interface.

Depending on the kind of interface and the status, some variables may be empty.

 

The format of an IP address is the same as returned by the sockGetLocalIP function.

 

Input:

iface : SINT

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

 

Output:

status : SINT;

0 = Interface is not present.

1 = Interface is not opened.

2 = Interface is not connected.

3 = Interface is connected.

4 = Interface link is up and waiting for IP configuration.

 

phy_addr : STRING

The physical address. For example the MAC address of the LAN interface.

 

IP : DINT

The local IP address.

 

SubnetMask : DINT

The subnet mask.

 

Gateway : DINT

The network gateway address.

 

DHCP : BOOL

Whether DHCP is used to get IP configuration.

 

dhcp_addr : DINT

The IP address of the DHCP server used.

 

AutoDNS : BOOL

If the DNS server addresses are automatically configured.

 

DNS1 : DINT

The IP address of the primary DNS server.

 

DNS2 : DINT

The IP address of the secondary DNS server.

 

SSID : STRING

The SSID of the wireless network.

 

index_ap : INT

The index of the used wireless network settings.

 

NAT_gw : BOOL

Does this interface act as a gateway to the internet?

 

NAT_fwd : BOOL

Is NAT forwarding activated for this interface?

 

 

DHCP_enable : BOOL

The DHCP server is enabled on this interface if true, otherwise not-

 

DHCP_lease : INT

THe lease time in hours of the DHCP addresses [1h..24h].

 

DHCP_begin : DINT

The low end of the DHCP server IP pool range.

 

DHCP_end : DINT

The high end of the DHCP server IP pool range.

 

DHCP_staticDNS : BOOL

IF true, the DHCP server configures the client with the following static DNS IP addresses.

Otherwise the DNS server configuration from the gateway is used. See netSetLANParam and netGetLANParam.

 

DHCP_DNS1 : DINT

The first priority static DNS server IP address.

 

DHCP_DNS2 : DINT

The second priority static DNS server IP address.

 

 

Declaration:

 
FUNCTION_BLOCK netGetInformation;
VAR_INPUT
  iface         : MANDATORY SINT;
END_VAR;
VAR_OUTPUT
  status         : SINT;
  phy_addr       : STRING;
  IP             : DINT;
  SubnetMask     : DINT;
  Gateway        : DINT;
  DHCP           : BOOL;
  dhcp_addr      : DINT;
  AutoDNS        : BOOL;
  DNS1           : DINT;
  DNS2           : DINT;
  SSID           : STRING;
  index_ap       : INT;
  NAT_gw         : BOOL;
  NAT_fwd        : BOOL;
  DHCP_enable    : BOOL;
  DHCP_lease     : INT;
  DHCP_begin     : DINT;
  DHCP_end       : DINT;
  DHCP_staticDNS : BOOL;
  DHCP_DNS1      : DINT;
  DHCP_DNS2      : DINT;
END_VAR;
 

Example:

INCLUDE rtcu.inc
VAR
  netInfo  : netGetInformation;
  iface    : SINT := 2; // Network interface to use: 1 for Mobile, 2 for LAN.
END_VAR;
 
FUNCTION printInfo
  netInfo(iface:=iface);
  DebugFmt(message:="Network interface \1:", v1 := iface);
  DebugFmt(message:=" Status \1", v1 := netInfo.status);
  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));
    IF netInfo.NAT_gw THEN
        DebugMsg(message:=" This interface acts as gateway for NAT");
    ELSE
        DebugMsg(message:=" This interface is not a NAT gateway");
    END_IF;
    IF netInfo.NAT_fwd THEN
        DebugMsg(message:=" NAT forwarding is active");
    ELSE
        DebugMsg(message:=" NAT forwarding is not active");
    END_IF;
    IF netInfo.DHCP_enable THEN
        DebugMsg(message:=" DHCP server is active");
        DebugFmt(message:=" DHCP server IP address lease time is \1 hours", v1 := netInfo.DHCP_lease);
        DebugMsg(message:=" DHCP IP pool range start    " + sockIPToName(ip := netInfo.DHCP_begin));
        DebugMsg(message:=" DHCP IP pool range end      " + sockIPToName(ip := netInfo.DHCP_end));
        IF netInfo.DHCP_staticDNS THEN
          DebugMsg(message:=" DHCP server uses following static IP addresses to configure the client:");
          DebugMsg(message:=" DHCP DNS1    " + sockIPToName(ip := netInfo.DHCP_DNS1));
          DebugMsg(message:=" DHCP DNS2    " + sockIPToName(ip := netInfo.DHCP_DNS2));
        ELSE
          DebugMsg(message:=" DHCP server DNS configuration is from the gateway.");
        END_IF;
    ELSE
        DebugMsg(message:=" DHCP server is not active");
    END_IF;
  END_IF;
END_FUNCTION;
 
PROGRAM test;
VAR
  rc : INT;
END_VAR;
  IF iface = 1 THEN
     // Turn on power to GSM module
    gsmPower(power:=ON);
  END_IF;
  IF NOT netPresent(iface:=iface) THEN
    DebugFmt(message:="Network interface \1 is not available", v1:=iface);
    WHILE TRUE DO
        Sleep(delay:=5000);
    END_WHILE;
  END_IF;
  rc := netOpen(iface:=iface);
  DebugFmt(message:="netOpen: \1", v1:=rc);
  WHILE NOT netConnected(iface:=iface) DO
    DebugMsg(message:="Waiting for network connection");
    Sleep(delay:=2500);
  END_WHILE;
  // Print network information
  printInfo();
  // Network is ready
BEGIN
 
  //...
 
END;
END_PROGRAM;