We can see, UDP and TCP protocols, respectively, in fact, very easy to use, as long as you can call the different functions.
Note the code is as follows:
#include
#include
#include
#include
#include
#include "../header/types.h"
#include "../header/serial.h"
#include "../header/socket.h"
#include "../header/w5100.h"
#define MAX_SIZE 2048
#define RX_BUF 0x1100
#define TX_BUF 0x1900
void main()
{
cli(); // disable interrupt
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;
EIFR=0x00;
MCUCR = 0x80; // enable external ram
XMCRA=0x40; //define lower sector of memory for EXternal ram and upper sector for W5100C and others
sei(); // enable interrupts
uart_init(0,7);
lcd_init();
uint8 destip[4];
uint8 addr[6];
iinchip_init();
sysinit(0x55,0x55); //
addr[0] = 192; addr[1] = 168; addr[2] = 1; addr[3] = 20;
setSIPR(addr);
addr[0] = 192; addr[1] = 168; addr[2] = 1; addr[3] = 1;
setGAR(addr);
addr[0] = 255; addr[1] = 255; addr[2] = 255; addr[3] = 0;
setSUBR(addr);
memcpy(addr, "\x00\x08\xDC\x00\x00\x00",6);
setSHAR(addr);
uint8 lcd[50];
sprintf(lcd,"IP: %d.%d.%d.%d",IINCHIP_READ(SIPR0),IINCHIP_READ(SIPR0+1),IINCHIP_READ(SIPR0+2),IINCHIP_READ(SIPR0+3));
lcd_puts(lcd); //print LCD string into LCD
uint8 len;
// set UDP mode
socket(0, Sn_MR_UDP, 5000, 0);
// listen(0); //UDP no need to listen
if( getSn_SR(0) == SOCK_UDP)
{
printf("UDP Mode"); // Check socket status register, it is in UDP status
}
while(getSn_RX_RSR(0) ==0 ); // if got data, RX buffer over 0, while loop will end
printf("got data \r\n");
len = getSn_RX_RSR(0); // get length of data received
uint8 * TX_BUFFER = (uint8 *) TX_BUF;
uint8 * RX_BUFFER = (uint8 *) RX_BUF; // make transmit and receive buffer
if( len > MAX_SIZE)
len = MAX_SIZE; // limit the length of data to be copy into TX/RX buffer as MAX
//extern uint16 send(SOCKET s, const uint8 * buf, uint16 len); // Send data (TCP)
//extern uint16 recv(SOCKET s, uint8 * buf, uint16 len); // Receive data (TCP)
//extern uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port); // Send data (UDP/IP RAW)
//extern uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port); // Receive data (UDP/IP RAW)
//sendto and recvfrom are used for UDP, in contrast, TCP uses send and recv function
// because TCP is connection oriented, once connection is established, no need to assign send to which or receive from which client
len = recvfrom(0,RX_BUFFER,len, destip, 5000); //receive into RX buffer
printf("the size is %u \r\n",len);
printf("%s",RX_BUFFER);
// printf("%02x",*(RX_BUFFER + 1));
// printf("%02x",*(RX_BUFFER +2) );
// in case of more than one character, print 0, 1, 2 address
sendto(0,RX_BUFFER,len,destip, 5000); //send RX buffer to client
}
Translated from Johnny Yan,posted on 28/09/2009
The direct link (Chinese Version): http://blog.sina.com.cn/s/blog_61b952280100f8qj.html
No comments:
Post a Comment