udpSend  (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.00 / 1.00.00


This sends data as a connectionless UDP packet.

 

 

Input:

 

ip : DINT

IP address of the peer node (receiver of the data).

 

port : DINT

Port number to send the data to on the peer node.

 

data : PTR

Address of the buffer that contains the data to be sent.

 

size : INT

Number of bytes to send from the buffer.

Maximum size is 1540 bytes.

 

iface : SINT (default 0)

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

Note: For backwards compatibility the Mobile network is used as default network.

 

Returns: INT

Number of bytes that were sent on the socket, or:

0

- Communication problem. No data sent.

-1

- Illegal parameters.

-2

- Memory allocation error.

 

Declaration:

FUNCTION udpSend : INT;
VAR_INPUT
  ip   : DINT;     // IP address to send data to.
  port : DINT;     // Port number to send data to
  data : PTR;     // Address of the buffer to send
  size : INT;     // Number of bytes to send

  iface : SINT;     // Interface to send on
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR_INPUT
  send_message : BOOL R_EDGE; | Sends a message on rising edge
END_VAR;
 
PROGRAM udp_client;
VAR
  host : STRING := "my.domain.com"; // host to retrieve data
  line : STRING;                 // ASCII string to send
  data : ARRAY[1..1540] OF SINT; // send data buffer
  rip   : DINT;                   // remote ip to receive data
  rc   : INT;                     // return code of command
  port : INT := 2500;             // remote host listener port
END_VAR;
 
  gsmPower(power := ON);
  gprsOpen();
 
  DebugMsg(message := "Ready to send message when input is activated.");
BEGIN
  IF send_message THEN
     // Return IP address (0 if none)
     rip := sockIPFromName(str := host);
 
     line := "Sending a message through UDP";
     // Copy contents of a string to memory
    strToMemory(dst := ADDR(data), str := line, len := strLen(str := line));
     // Send data on a UDP socket
     rc := udpSend(ip := rip, port := port, data := ADDR(data), size := strLen(str := line));
    IF rc > 0 THEN
        DebugFmt(message := "sent \1 Byte(s) of data.", v1 := rc);
    ELSE
        DebugFmt(message := "failed to send data error code '\1'", v1 := rc);
   END_IF;
  END_IF;
END;
 
END_PROGRAM;