#define MAX_BUF_SIZE 2048 // define maximum buffer size
uint8 xdata pDestaddr[4]; //xdata using external ram
uint16 xdata pPort; // destination port
uint8 xdata rcvLen; // length of data received
uint16 xdata length; // the length of data received
uint8 xdata RXdata[MAX_BUF_SIZE]; // RX data received
uint8 xdata TXData[MAX_BUF_SIZE]; // TX data send
uint16 xdata sentLen; // length of data sent
uint16 xdata TXSize; // size of string sent
Init_iMCU(); // Initialize iMCUW7100
Init_Network(); // Initialize Network Configuration
//Delay for link
wait_1ms(7000);
// 1. create socket for udp
if(socket(0, Sn_MR_UDP,5000, 0) == 1) // create socket with UDP port 5000
{
PutString("\r\n opened socket");
// 2. receive data
while(getSn_RX_RSR(0) == 0) ; // check if there is data in socket
PutString("\r\n got data");
rcvLen = getSn_RX_RSR(0);
if(rcvLen > MAX_BUF_SIZE) // check if data received is bigger than size of buffer string
rcvLen = MAX_BUF_SIZE; // assign max buffer size to the received length
PutString("\r\n RX data size is "); PutITOA(rcvLen); PutString(" Bytes.");
length = recvfrom(0, RXdata, rcvLen, pDestaddr, &pPort); // receive data
RXdata[length] = 0; // indicate the end of string
PutString("\r\n RX data : ");
PutString(RXdata); // print received data
// 3. trasnmit data
//set Dest. IP address & Port
pDestaddr[0]= 192;
pDestaddr[1]= 168;
pDestaddr[2]= 1;
pDestaddr[3]= 3;
pPort = 5000;
PutString("\r\n TX data size is ");PutITOA(TXSize);
sentLen = sendto(0, TXData, TXSize, pDestaddr, pPort); // transfer data
if (sentLen == TXSize)
PutString("\r\n success to transmit");
else
PutString("\r\n Failed to transmit");
}
else // if socket is fail
PutString("\r\n Failed to open socket");
No comments:
Post a Comment