cancel
Showing results for 
Search instead for 
Did you mean: 

why lwip udp server does not send big data correctly?!

Saghi.1
Associate II

lwip udp server does not send big data correctly

hello

I have implemented a udp server on stm32h7 using raw lwip.my function is reading data from sd memory and sending to pc, so I read a block(512bytes) of data from sd memory by f_read function of fatfs and send it using udp_send to remote host.everything is OK with reading data from sd memory, but when I want to send for example a 2kbyte file from sd to PC, I receive only 1636bytes on pc. I use udp terminal of matlab. when I check the received data it shows that only firs 512 received bytes are correct and other received bytes seems to be a peice of next 512byte read from sd memory.here is the code I wrote for

///////////////////////////////////////////////////////////////

#define SD_read_buffer 512

udp_server_send.

 struct pbuf *p_tx;

struct pbuf *sb;

char *c;

int i;

char data_read[10]={0};

  

 /* allocate pbuf from RAM*/

 p_tx = pbuf_alloc(PBUF_TRANSPORT,p->len, PBUF_RAM);

sb = pbuf_alloc(PBUF_TRANSPORT,512, PBUF_RAM);

 if(p_tx != NULL)

 {

  pbuf_take(p_tx, (char*)p->payload, p->len);

  /* Connect to the remote client */

  udp_connect(upcb, addr, 4023);

   

  /* Tell the client that we have accepted it */

c = p->payload;

for( i = 0 ; i < p->len ; i++)

{

data_read[i] = c[i];

}

if(data_read[0]=='9')

{

HAL_Delay(2000);

// f_open(&SDFile, "REC_2K.DAT", FA_READ); //640kb file

f_open(&SDFile, "RECORD_1.DAT", FA_READ);//5kb file

for (;;) 

{

f_read(&SDFile, SD_read_buffer, sizeof SD_read_buffer, &br); /* Read a chunk of data from the source file */

if (br == 0) break; /* error or eof */

memcpy(sb->payload,SD_read_buffer,sizeof SD_read_buffer);

udp_send(upcb, sb);

HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_8);

}

f_close(&SDFile);

data_read[0]=0;

}

   

  /* free the UDP connection, so we can accept new clients */

  udp_disconnect(upcb);

   

  /* Free the p_tx buffer */

  pbuf_free(p_tx);

pbuf_free(sb);

  /* Free the p buffer */

  pbuf_free(p);

//////////////////////////////////////////////////////////////

I added 100ms delay before and after udp_send, but it did not fixed the problem, so the problem is not about time. but when I add pbuf_alloc of sb pbuf before f_read the received data have the same size of sent.but it allocate extra memory in every loop and fill the ram, so its not the solution.

would someone give me a hint or a solution about this problem?

thank u before

1 REPLY 1
Saghi.1
Associate II

hi

I solve the problem by moving​ below lines to a function, also redefinition of sb pbuf on top of that function and feeding at the end.

memcpy(sb->payload,SD_read_buffer,sizeof SD_read_buffer);

udp_send(upcb, sb);

in this way my problem solved, but a new problem is occurred. now it seems I have a udp_send speed problem.when I send big data, after and receiving on pc I see that only the first bytes are correctly received and the last bytes are a little different. I added a 10ms delay after udp_send and the problem solved,but this time must be different for various files with different size.

is there any way to check the udp_send finishing time?​e.g a flag or a callback function?

or any other way?

thanks​