ex_udp.c

The following example will create a UDP socket that listens to port 54123. If anything is received on this port, it will check the first byte of the received data. If it is 1, it will set the system LED (Port B6), if 0, it will clear the system LED.

#include "net\udp.h"

//Create a UDP socket for receiving and sending data
static UDP_SOCKET udpSocketUser = INVALID_UDP_SOCKET;


void udpExample(void)
{
    BYTE c;
    NODE_INFO udpServerNode;

    //Initialize remote IP and MAC address of udpServerNode with 0, seeing that we
    //don't know them for the node that will send us an UDP message. The first time
    //a message is received addressed to this port, the remote IP and MAC addresses
    //are automatically updated with the addresses of the remote node.
    memclr(&udpServerNode, sizeof(udpServerNode));

    //Configure for local port 54123 and remote port INVALID_UDP_PORT. This opens
    //the socket to listen on the given port.
    udpSocketUser = UDPOpen(54123, &udpServerNode, INVALID_UDP_PORT);
    
    //An error occurred during the UDPOpen() function
    if (udpSocketUser == INVALID_UDP_SOCKET) {
        //Add user code here to take action if required!
    }

    //Infinite loop. Check if anything is received on UDP port
    while(1)
    {
        //Is there any data waiting for us on the UDP socket?
        //Because of the design of the Modtronix TCP/IP stack we have to consume
        //all data sent to us as soon as we detect it.
        if (UDPIsGetReady(udpSocketUser)) {

            //We are only interrested in the first byte of the message.
            UDPGet(&c);
            
            if (c == '0') LATB6 = 0;        //Switch system LED off
            else if (c == '1') LATB6 = 1;   //Switch system LED on

            //Discard the socket buffer.
            UDPDiscard();
        }

        //This task performs normal stack task including checking for incoming
        //packet, type of packet and calling appropriate stack entity to
        //process it.
        StackTask();
    }
}

Generated on Wed Feb 3 12:45:33 2010 for SBC65EC Web Server by  doxygen 1.5.8