基于STM32 LWIP协议栈实现TCP以太网数据通信

在做以太网的数据传输,要把AD采到的数据通过网口发送给上位机(客户端),我采用的是LWIP协议栈,实现了此功能。不管做项目时间多紧,也要先熟悉一下LWIP协议栈,TCP 及UDP传输协议。我采用的是TCP协议数据传输,好处是传输可靠。直接贴代码,从main开始:

int main(void)
{
SystemInit();
System_Setup();
GpioLed_Init();
Init_Usart();
GPIO_Configuration();
GPIO_Configuration_SPI();
RCC_Configuration();
NVIC_Configuration(); 
Time_Configuration();
SPI_Configuration(); 
LwIP_Init();
HelloWorld_init();
while(1)
 { 
 TI_ADC128S022_ADC_vout();
 printf("Frequency1= %d HZ.\r\n",Frequency1);
 printf("Frequency2= %d HZ.\r\n",Frequency2); 
 printf("V=%fmv\r\n",V);
 MCU_to_TCP();
 /* Periodic tasks */
 System_Periodic_Handle();
 } 
}

说一下LwIP_Init();函数,主要是LWIP协议栈,IP和MAC初始化,

void LwIP_Init(void)
{
 struct ip_addr ipaddr;
 struct ip_addr netmask;
 struct ip_addr gw;
 uint8_t macaddress[6]={0,0,0,0,0,1};
 /* Initializes the dynamic memory heap defined by MEM_SIZE.*/
 mem_init();
 /* Initializes the memory pools defined by MEMP_NUM_x.*/
 memp_init();
IP4_ADDR(&ipaddr, 192, 168, 1, 198);
 IP4_ADDR(&netmask, 255, 255, 255, 0);
 IP4_ADDR(&gw, 192, 168, 1, 1);
 Set_MAC_Address(macaddress);
netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
 /*Registers the default network interface.*/
 netif_set_default(&netif);
netif_set_up(&netif);
}

HelloWorld_init();是建立TCP端口,

void HelloWorld_init(void)
{
 /* 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, 3007);
 /* 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_accept); 
}

tcp_bind(pcb, IP_ADDR_ANY, 3007);是绑定你的端口号和IP地址, pcb = tcp_listen(pcb);进入监听,检查连接,申请TCP_PCB内存,tcp_accept(pcb, HelloWorld_accept);客户端连接以后的回调函数,可以收发数据。

static err_t HelloWorld_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
 tcp_arg(pcb, mem_calloc(sizeof(struct name), 1));//回传建立连接
 tcp_err(pcb, HelloWorld_conn_err);//错误回调函数
 tcp_recv(pcb, HelloWorld_recv);//指定收到数据的回调函数
 return ERR_OK;
}

void MCU_to_TCP(void)函数是给单片机(服务器)传输数据函数,每次传输数据完之后要调用tcp_output(cpcb);函数,用于TCP输出。我之前调用tcp_write函数,每次只能收到一次数据,需要新创建一个新的pcb协议控制块,tcp_write之后要调用tcp_output函数,才能不断发送数据。

void MCU_to_TCP(void)
{
struct tcp_pcb *cpcb;
 for(cpcb = tcp_active_pcbs;cpcb != NULL; cpcb = cpcb->next)
 {
 memset(GpcBufFileRead, 0x00, sizeof(GpcBufFileRead));
 sprintf( (void *)readdata, "Frequency1 = %dHz\nFrequency2 = %dHz\nV = %fmv\n", Frequency1,Frequency2,V);
 //tcp_write(pcb, GpcBufFileRead, strlen((void *)readdata), 1);
 tcp_write(cpcb,GpcBufFileRead,strlen((void *)readdata),TCP_WRITE_FLAG_COPY);
 tcp_output(cpcb);
}
}

这样就可以简单实现LWIP TCP数据传输了,主要是这几个地方注意一下,很快可以实现网口数据的发送与接收。

在调试以太网的时候应该注意的问题如下:

电脑IP地址一定要和开发板的IP地址在一个网络内,例如开发板IP是:192.168.1.118,子网掩码:255.255.255.0,默认网关:192.168.1.1。需要断开电脑网络,连接网线,设置电脑IP地址为192.168.1.X(2-254), 子网掩码:255.255.255.0,默认网关:192.168.1.1,首选DNS服务器:192.168.1.1。即可实现路由器或者上位机与开发板建立连接。


分享到:


相關文章: