最近搞个STM32F107,做服务器程序,遇到点问题,请大家帮忙看看,现在其他地方考个程序,可以收 ,在初始化的时候可以发,或者客户端断开连接 在连上的时候可以发一下数据 。以后就不知道怎么发数据了、、这主程序
int main(void)
{
struct tcp_pcb *pcb;
/* Setup STM32 system (clocks, Ethernet, GPIO, NVIC) and STM3210C-EVAL resources */
System_Setup();//
/* Initilaize the LwIP satck */
LwIP_Init(); //到这里可以ping通
/* Initilaize the HelloWorld module */
HelloWorld_init();//这个函数后面有说明,里面有发送数据的函数,可以发送
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);// 改变指定管脚的映射 GPIO_Remap_SWJ_Disable SWJ 完全禁用(JTAG+SW-DP)
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);// 改变指定管脚的映射 GPIO_Remap_SWJ_JTAGDisable ,JTAG-DP 禁用 + SW-DP 使能
while (1)
{
/* Periodic tasks */
System_Periodic_Handle();
}
}
这个函数有发送数据的代码可以发送,初始化的时候有调用可以发送
void HelloWorld_init(void)
{
struct tcp_pcb *pcb;
/* Create a new TCP control block */
pcb = tcp_new();
/* Assign to the new pcb a local IP address and a port number */
/* Using IP_ADDR_ANY allow the pcb to be used by any local interface */
tcp_bind(pcb, IP_ADDR_ANY, 23);
/* Set the connection to the LISTEN state */
pcb = tcp_listen(pcb);
/* Specify the function to be called when a connection is established */
tcp_accept(pcb, HelloWorld_accept1); //具体是这个发送
}
看下这个函数HelloWorld_accept1,这是1个回调函数,这个在HelloWorld_init(void)调用了一下 ,有个问题不明白,就是客户端断开连接在连接 也会进入这个函数(不知道怎么进入的),这个函数可以发送数据 ,客户端也可以收到,但是以后单独调用这个的时候 就不可以了 单片机会跑飞。死机。
static err_t HelloWorld_accept1(void *arg, struct tcp_pcb *pcb, err_t err)//发送数据进这个
{
/* Tell LwIP to associate this structure with this connection. */
tcp_arg(pcb, mem_calloc(sizeof(struct name), 1)); //告诉他们 已经建立连接
/* Configure LwIP to use our call back functions. */
tcp_err(pcb, HelloWorld_conn_err); //错误回调函数
tcp_write(pcb, GREETING, strlen(GREETING), 0); //输出hello
tcp_recv(pcb, HelloWorld_recv);//指定接收到数据后的回调函数
/* Send out the first message */
return ERR_OK;
}
这接收数据的函数 服务器发来的数据可以收到里面,但是也搞不清是怎么进入的
static err_t HelloWorld_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)//收到数据会进这个函数
{
struct pbuf *q;
struct name *name = (struct name *)arg;
int done;
char *c;
int i;
/* We perform here any necessary processing on the pbuf */
if (p != NULL)
{
/* We call this function to tell the LwIp that we have processed the data */
/* This lets the stack advertise a larger window, so more data can be received*/
tcp_recved(pcb, p->tot_len);
/* Check the name if NULL, no data passed, return withh illegal argument error */
if(!name)
{
pbuf_free(p);
return ERR_ARG;
}
done = 0;
for(q=p; q != NULL; q = q->next)
{
c = q->payload;
if(c[0]=='1')
tcp_write(pcb, "aaaa", 4, 1);
for(i=0; i<q->len && !done; i++)
{
done = ((c == '\r') || (c == '\n'));
if(name->length < MAX_NAME_SIZE)
{
name->bytes[name->length++] = c;
infalg=1;
inbuf= c;
}
}
}
if(done)
{
if(name->bytes[name->length-2] != '\r' || name->bytes[name->length-1] != '\n')
{
if((name->bytes[name->length-1] == '\r' || name->bytes[name->length-1] == '\n') && (name->length+1 <= MAX_NAME_SIZE))
{
name->length += 1;
}
else if(name->length+2 <= MAX_NAME_SIZE)
{
name->length += 2;
}
else
{
name->length = MAX_NAME_SIZE;
}
name->bytes[name->length-2] = '\r';
name->bytes[name->length-1] = '\n';
}
tcp_write(pcb, HELLO, strlen(HELLO), 1);
tcp_write(pcb, name->bytes, name->length, TCP_WRITE_FLAG_COPY);
tcp_write(pcb, GREETING, strlen(GREETING), 0);
name->length = 0;
}
/* End of processing, we free the pbuf */
pbuf_free(p);
}
else if (err == ERR_OK)
{
/* When the pbuf is NULL and the err is ERR_OK, the remote end is closing the connection. */
/* We free the allocated memory and we close the connection */
mem_free(name);
return tcp_close(pcb);
}
return ERR_OK;
}
想知道的问题就是 做为服务器端 我怎么给客户端发数据???????????
那俩个函数是怎么调用的,为什么会进入那2个函数?????????????
中断里面可能有调用下面是中断部分
void ETH_IRQHandler(void)
{
/* Handles all the received frames */
while(ETH_GetRxPktSize() != 0)
{
LwIP_Pkt_Handle();
}
/* Clear the Eth DMA Rx IT pending bits */
ETH_DMAClearITPendingBit(ETH_DMA_IT_R);
ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);
}
中断里面的函数
void LwIP_Pkt_Handle(void)
{
/* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
ethernetif_input(&netif);
}
上面函数里面的函数
err_t
ethernetif_input(struct netif *netif)
{
err_t err;
struct pbuf *p;
/* move received packet into a new pbuf */
p = low_level_input(netif);
/* no packet could be read, silently ignore this */
if (p == NULL) return ERR_MEM;
err = netif->input(p, netif);//ethernet_input
if (err != ERR_OK)
{
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
pbuf_free(p);
p = NULL;
}
return err;
}
这都没看到调用 不太明白那个收发函数是哪里怎么调用的
|
|