udpStartListen (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.00 / 1.00.00


This starts to listen for data received as a connectionless UDP packet on the specified local port.

The data will then be received with the updReceive function block.

 

Note: many GSM networks are using a firewall so that inbound connections to the RTCU are blocked when using the mobile network interface.

 

 

Input:

port : DINT

Port number listening on.

 

rip : DINT

Remote IP address accepted. 0 = all IP addresses accepted (default).

 

iface : SINT (default 0)

The network interface to listen on. 0 = all networks (default), 1 = Mobile network, 2 = LAN network, etc. (See Network)

 

 

Returns: SINT

ID for the connection or 0 if an error occurred (out of resources).

 

Declaration:

FUNCTION udpStartListen : SINT;
VAR_INPUT
  port : DINT;     // Port number to listen on
  rip   : DINT :=0; // Accept from this remote IP only. 0=all.

  iface : SINT :=0; // Interface to listen on
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
PROGRAM udp_server;
VAR
  fbReceive : udpReceive;           // Receive data on a UDP socket
  port     : INT := 2500;           // port to receive data on
  udp_id   : SINT;                 // listener ID
  host     : STRING;               // translated IP
  line     : STRING;               // ASCII string to send
  data     : ARRAY[1..255] OF SINT; // received data buffer
  rc       : INT;                   // return code of command
END_VAR;
 
gsmPower(power := ON);
gprsOpen();
 
// Wait for the cellular connection to be established
WHILE NOT gprsConnected() DO
  DebugMsg(message := "Waiting for cellular connection");
  Sleep(delay := 2500);
END_WHILE;
 
// Listen for incoming connection on a UDP port
udp_id := udpStartListen(port := port, rip := 0);
BEGIN
  // update udp received data
  fbReceive(id := udp_id, data := ADDR(data), maxsize := SIZEOF(data));
 
  IF fbReceive.ready THEN
     host := sockIPToName(ip := fbReceive.ip);
     DebugFmt(message := "Received '\1' bytes from '" + host + "'", v1 := fbReceive.size);
    line := strFromMemory(src := ADDR(data), len := fbReceive.size);
    DebugMsg(message := "Message : " + line);
  END_IF;
END;
END_PROGRAM;