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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章