netOpen (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

4.34 / 1.00.00


Opens a network interface and prepares it for use.

Must be called before any communication using the network interface can occur.

netOpen(iface := 1) is identical to gprsOpen(), and must be called after gsmPower.

 

Note that the connection to the network is not established when this function returns.

The netConnected() function will indicate when the connection to the network is established and ready for communication.

 

Input:

iface : SINT

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

 

 

Returns: INT

0

- Success.

-1

- Unsupported interface.

-2

- Interface is powered off.

-3

- Interface can not be opened at the selected CPU speed (see pmSetSpeed).

-4

- Interface is unable to connect because of the configuration.

-5

- Interface is not accessible. This may be because the cellular connection is using the highspeed link. See gsmPower().

 

 

Declaration:

FUNCTION netOpen : INT;
VAR_INPUT
  iface : SINT;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
//  These are the global variables of the program
VAR
  netInfo  : netGetInformation;
  iface    : SINT := _NET_IFACE_LAN1; // 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));
  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;