00001 00010 #include "net\udp.h" 00011 00012 //The following code is for the Microchip MPLAB C18 compiler. For the 00013 //Hi-Tech compiler, the #pragma's are not required 00014 #pragma udata BUFFER1_256BYTES 00015 BYTE udpRxBuf[256]; 00016 #pragma udata //Return to default section 00017 00018 //Get and Put pointer for udpRxBuf circular buffer. If Get pointer = Put 00019 //pointer, the buffer is empty. 00020 BYTE udpRxBufGet, udpRxBufPut; 00021 00022 //Create a UDP socket for receiving and sending data 00023 static UDP_SOCKET udpSocketUser = INVALID_UDP_SOCKET; 00024 00025 00026 void main(void) 00027 { 00028 BYTE c; 00029 NODE_INFO udpServerNode; 00030 00031 //Set UDP Receive buffer to be empty 00032 udpRxBufGet = udpRxBufPut = 0; 00033 00034 //Initialize remote IP and MAC address of udpServerNode with 0, seeing that 00035 //we don't know them for the node that will send us an UDP message. The first 00036 //time a message is received addressed to this port, the remote IP and MAC 00037 //addresses are automatically updated with the addresses of the remote node. 00038 memclr(&udpServerNode, sizeof(udpServerNode)); 00039 00040 //Configure for local port 54123 and remote port INVALID_UDP_PORT. This 00041 //opens the socket to listed on the given port. 00042 udpSocketUser = UDPOpen(54123, &udpServerNode, INVALID_UDP_PORT); 00043 00044 //An error occurred during the UDPOpen() function 00045 if (udpSocketUser == INVALID_UDP_SOCKET) { 00046 //Take any additional action that is required when an error occurs 00047 } 00048 00049 //Infinite loop. Check if anything is received on UDP port 00050 while(1) 00051 { 00052 //Is there any data waiting for us on the UDP socket? Because of the 00053 //design of the Modtronix TCP/IP stack we have to consume all data 00054 //sent to us as soon as we detect it. Store all data to a buffer 00055 //as soon as it is detected 00056 if (UDPIsGetReady(udpSocketUser)) { 00057 00058 //Read all data from socket, and save it to UDP Receive buffer 00059 //(udpRxBuf) 00060 while(UDPGet(&c)) { 00061 //Store byte read from UDP socket to UDP buffer 00062 udpRxBuf[udpRxBufPut++] = c; 00063 00064 //If Put pointer caught up to Get pointer, our buffer is 00065 //full and we have lost data! Increment Get pointer. 00066 if (udpRxBufPut == udpRxBufGet) { 00067 udpRxBufGet++; 00068 } 00069 } 00070 00071 //Discard the socket buffer. 00072 UDPDiscard(); 00073 } 00074 00075 //Does the UDP receive buffer contain any data? If so, we sent this data back 00076 //to the same socket that just send us data. 00077 if(udpRxBufPut != udpRxBufGet) { 00078 //Checks if there is a transmit buffer ready for accepting data, and that the 00079 //given socket is valid (not equal to INVALID_UDP_SOCKET for example) 00080 if (UDPIsPutReady(udpSocketUser)) { 00081 00082 //Transmit entire buffer 00083 while (udpRxBufGet != udpRxBufPut) { 00084 UDPPut(udpRxBuf[udpRxBufGet++]); 00085 } 00086 00087 // Now transmit it. 00088 UDPFlush(); 00089 } 00090 } 00091 00092 //This task performs normal stack task including checking for incoming packet, 00093 //type of packet and calling appropriate stack entity to process it. 00094 StackTask(); 00095 } 00096 }
1.4.7