FreeRTOS + LWIP -UDP Data Transfer

4.6k views Asked by At

I am kinda new to the lwip stack. I'm trying to send some data over UDP protocol from my development board to my pc. And using ethernet cable between the two of them.

I gave an ip address to my server(source-board), which is 192.168.1.75:88. And the ip address of my computer is 192.168.1.2:90. When I set this configuration and run the program, I can sniff nothing with wireshark, I mean there is no udp package exchange at all. But when I change all destination adress to 255.255.255.255 or 0.0.0.0, I can sniff some packages.

Why can't I send udp packages to the ip address that I want?

Main.c

int main(void)
{
   #define dst_port 88
   #define src_port 90

  #ifdef SERIAL_DEBUG
    DebugComPort_Init();
  #endif

  LCD_LED_Init();
  ETH_BSP_Config();
  LwIP_Init();
  IP4_ADDR(&dstaddr,     0, 0, 0, 0);
  IP4_ADDR(&srcaddr,     192, 168, 1, 75);

  pcb = udp_new();

  udp_bind(pcb, &dstaddr, src_port);
  udp_recv(pcb, RecvUTPCallBack, NULL);
  udp_connect(pcb, &dstaddr, dst_port);

  #ifdef USE_DHCP
  /* Start DHCPClient */
  xTaskCreate(LwIP_DHCP_task, "DHCPClient", configMINIMAL_STACK_SIZE * 2, NULL,DHCP_TASK_PRIO, NULL);
  #endif

  /* Start toogleLed4 task : Toggle LED4  every 250ms */
  xTaskCreate(ToggleLed4, "LED4", configMINIMAL_STACK_SIZE, NULL, LED_TASK_PRIO, NULL);
  xTaskCreate(SendUDP, "UDP", configMINIMAL_STACK_SIZE, NULL, LED_TASK_PRIO, NULL);

  /* Start scheduler */
  vTaskStartScheduler();
  for( ;; );
}

SendUDP Task

void SendUDP(void * pvParameters)
{
  while(1)
  {     
    pcb = udp_new();
    udp_bind(pcb, &dstaddr, src_port);
    udp_recv(pcb, RecvUTPCallBack, NULL);
    udp_connect(pcb, &dstaddr, dst_port);

    pb = pbuf_alloc(PBUF_TRANSPORT, sizeof((str)), PBUF_REF);
    pb->payload = str;
    pb->len = pb->tot_len = sizeof((str));

    udp_sendto(pcb, pb, &dstaddr, dst_port);
    udp_disconnect(pcb);
    udp_remove(pcb);
    pbuf_free(pb);  
    vTaskDelay(1000);
  }
}
1

There are 1 answers

0
Ceyhan ILERI On BEST ANSWER

I figured this out about a week ago, but I couldn't post the answer to here.

First of all, there is an ip defining in main.h, like

/*Static IP ADDRESS*/
#define IP_ADDR0   192
#define IP_ADDR1   168
#define IP_ADDR2   1
#define IP_ADDR3   15

and this configurations being use in netconf.h

IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);

that's why server's ip adress is always 192.168.1.15.

And the second, I just started to use netconn API instead of raw API, this is way much easier than raw API. And this is my new SendwithUDP function, which is working perfect.

void SendwithUDP(uint16_t *veri, uint8_t length)                                                                    
{
  while(1)
    {
        if(((EventFlags.udp) && (1<<0)) == (1<<0)) 
        {
                        STM_EVAL_LEDToggle(LED3);

                        sendconn = netconn_new( NETCONN_UDP ); 
                        netconn_bind(sendconn, IP_ADDR_ANY, src_port );
                        netconn_connect(sendconn, &clientAddr, 150);                                
                        sendbuf = netbuf_new();
                        data =netbuf_alloc(sendbuf, 2*length);
                        memcpy(data, veri, 2*length);
                        netconn_send(sendconn, sendbuf);    
                        netbuf_free(sendbuf);
                        netbuf_delete(sendbuf);
                        netconn_disconnect(sendconn);
                        netconn_delete(sendconn);       
                        vTaskDelay(10);
            }
      } 
}