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;
port : DINT;
data : PTR;
size : INT;
iface : SINT;
END_VAR;
Example:
INCLUDE rtcu.inc
VAR_INPUT
send_message : BOOL R_EDGE;
END_VAR;
PROGRAM udp_client;
VAR
host : STRING := "my.domain.com";
line : STRING;
data : ARRAY[1..1540] OF SINT;
rip : DINT;
rc : INT;
port : INT := 2500;
END_VAR;
gsmPower(power := ON);
gprsOpen();
DebugMsg(message := "Ready to send message when input is activated.");
BEGIN
IF send_message THEN
rip := sockIPFromName(str := host);
line := "Sending a message through UDP";
strToMemory(dst := ADDR(data), str := line, len := strLen(str := line));
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;
|