UDP management functions

The following functions enable management of UDP sockets for building client or server applications over ETHERNET network:

udpCreate        create a UDP socket
udpAddrMake     
build an address buffer for UDP functions
updSendTo       
send a telegram
udpRcvFrom      
receive a telegram
udpClose        
close a socket
udpIsValid      
test if a socket is valid

Each socket is identified in the application by a unique handle manipulated as a DINT value.

Important notes

  • Although the system provides a simplified interface, you must be familiar with the socket interface such as existing in other programming languages such as "C".
  • Socket management may be not available on some targets. Please refer to OEM instructions for further details about available features.

udpCreate: create a UDP socket

SOCK := udpCreate (PORT);

PORT : DINT     TCP port number to be attached to the server socket or 0 for a client socket
SOCK : DINT    
ID of the new server socket

This functions creates a new UDP socket. If the PORT argument is not 0, the socket is bound to the port and thus can be used as a server socket.

udpAddrMake: build an address buffer for UDP functions

OK := udpAddrMake (IPADDR, PORT, ADD);

IPADDR : STRING IP address in form xxx.xxx.xxx.xxx
PORT : DINT    
IP port number
ADD : USINT[32]
buffer where to store the UDP address (filled on output)
OK : BOOL      
TRUE if successful

This functions is required for building a internal "UDP" address to be passed to the udpSendTo function in case of UDP client processing.

udpSendTo: send a UDP telegram

OK := udpSendTo (SOCK, NB, ADD, DATA);

SOCK : DINT     ID of the client socket
NB : DINT       number of characters to send
ADD : USINT[32] buffer containing the UDP address (on input)
DATA : STRING  
characters to send
OK : BOOL       TRUE if successful

The "ADD" buffer must contain a valid UDP address either constructed by the udpAddrMake function or returned by the udpRcvFrom function.

udpRcvFrom: receive a UDP telegram

OK := udpRcvFrom (SOCK, NB, ADD, DATA);

SOCK : DINT     ID of the client socket
NB : DINT       maximum number of characters received
ADD : USINT[32] buffer containing the UDP address of the transmitter (filled on output)
DATA : STRING  
buffer where to store received characters
Q : DINT        number of actually received characters

If characters are received, the function fills the ADD argument with the internal "UDP" of the sender. This buffer can then be passed to the udpSendTo function to send the answer.

udpClose: release a socket

OK := udpClose (SOCK);

SOCK : DINT   ID of any socket
OK : BOOL     TRUE if successful

You are responsible for closing any socket created by tcpListen, tcpAccept or tcpConnect functions, even if they have become invalid.

udpIsValid: test if a socket is valid

OK := udpIsValid (SOCK);

SOCK : DINT   ID of the socket
OK : BOOL     TRUE if specified socket is still valid