Keil有自带的例程,里面改写TCP CALLBACK函数,TCP_EVT_DATA在这个状态下,表示已经接受到数据,可以进行处理,处理的时候直接发送就OK 了
U16 tcp_callback (U8 soc, U8 evt, U8 *ptr, U16 par) {
/* This function is called by the TCP module on TCP event */
/* Check the 'Net_Config.h' for possible events. */
par = par;
if (soc != socket_tcp) {
return (0);
}
switch (evt) {
case TCP_EVT_DATA:
/* TCP data frame has arrived, data is located at *par1, */
/* data length is par2. Allocate buffer to send reply. */
procrec(ptr);
break;
case TCP_EVT_CONREQ:
/* Remote peer requested connect, accept it */
return (1);
case TCP_EVT_CONNECT:
/* The TCP socket is connected */
return (1);
}
return (0);
}
void procrec (U8 *buf) {
switch (buf[0]) {
case BLINKLED:
LED_out (buf[1]);
break;
case READADC:
send_data(disp_buf);
break;
/*--------------------------- Process received data ------------------------*/
void procrec (U8 *buf,U16 len) {
switch (buf[0]) {
case BLINKLED:
LED_out (buf[1]);
break;
case ECHO:
send_data('3');
break;
}
}
U16 tcp_callback (U8 soc, U8 evt, U8 *ptr, U16 par) {
/* This function is called by the TCP module on TCP event */
/* Check the 'Net_Config.h' for possible events. */
par = par;
if (soc != socket_tcp) {
return (0);
}
switch (evt) {
case TCP_EVT_DATA:
/* TCP data frame has arrived, data is located at *par1, */
/* data length is par2. Allocate buffer to send reply. */
procrec(ptr,par);
break;
case TCP_EVT_CONREQ:
Rem_IP[0]=ptr[0];
Rem_IP[1]=ptr[1];
Rem_IP[2]=ptr[2];
Rem_IP[3]=ptr[3];
Rem_Port=par;
/* Remote peer requested connect, accept it */
return (1);
case TCP_EVT_CONNECT:
/* The TCP socket is connected */
return (1);
case TCP_EVT_CONREQ:
soc_state=4;
default:
Rem_IP[4]=evt;
break;
}
return (0);
}