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("V0=%fmv\r\n",V0); 
       printf("V1=%fmv\r\n",V1); 
       printf("V2=%fmv\r\n",V2); 
       printf("V3=%fmv\r\n",V3);  
         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, &ethernetif_init, &ethernet_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\nV0 = %fmv\nV1 = %fmv\nV2 = %fmv\nV3 = %fmv\n", Frequency1,Frequency2,V0,V1,V2,V3);
   //tcp_write(pcb, GpcBufFileRead, strlen((void *)readdata), 1); 
   tcp_write(cpcb,GpcBufFileRead,strlen((void *)readdata),TCP_WRITE_FLAG_COPY);
   tcp_output(cpcb);
  }
}

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

 

更多技术文章浏览请关注:

百家号:
https://author.baidu.com/home?context=%7B%22app_id%22%3A%221646108714303504%22%7D&wfr=bjh

头条号:
https://www.toutiao.com/c/user/8115738721/#mid=1646025109246987


 



发布了39 篇原创文章 · 获赞 20 · 访问量 19万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章