AnsweredAssumed Answered

Working with pointers to send Lan Data

Question asked by delos_reyes.kristian on Apr 16, 2013
Latest reply on Apr 16, 2013 by Andrew Neil
Hi Guys,

I'm a bit confused with pointers..

I have a global variable named uint8_t DATAS [130]

and I have a function that sends data through lan (note: Iam using stm32f4Discovery and DISC-BB board) my first data send OK... but on suceeding data the data doesnot change..

details:
my setup is using a peer to peer connection laptop -> unit
using stm32f4Discovery
using Disc-BB expansion board (LAN, SD, LCD, RS232)

for example..
(expected data:1234567890ASDFGHJKL:)
RECIEVED DATA: 1234567890ASDFGHJKL:
(expected data:ZXCVBNM)
RECIEVED DATA: 1234567890ASDFGHJKL:
(expected data:ZXCVBNM)
RECIEVED DATA: 1234567890ASDFGHJKL:
(expected data:ZXCVBNM)
RECIEVED DATA: 1234567890ASDFGHJKL:



void udp_echoclient_connect(void)
{
//   struct udp_pcb *upcb;
  struct pbuf *p;
  struct ip_addr DestIPaddr;
  err_t err;
        
   
  if (Flag == 0) {
    Flag = 1;
    /* Create a new UDP control block  */
    upcb = udp_new();
  } else {
    //char *data1= "";
      for (int ctr=0;ctr<130;ctr++)
      {
        dataS[ctr] =(char)0;
      }
      for (int ctr=0;ctr<130;ctr++)
      {
        dataS[ctr]=(char)DATAS[ctr];
      }
 
    strcpy((char*)data,dataS);
    p = pbuf_alloc(PBUF_TRANSPORT,strlen((char*)data), PBUF_POOL);
       
    if (p != NULL) {
      /* copy data to pbuf */
      pbuf_take(p, (char*)data, strlen((char*)data));
           
      /* send udp data */
      udp_send(upcb, p);
         
      /* free pbuf */
      pbuf_free(p);
    }
    return ;   
  }
   
  if (upcb!=NULL) {
    /*assign destination IP address */
    IP4_ADDR( &DestIPaddr, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );
   
    /* configure destination IP address and port */
    err= udp_connect(upcb, &DestIPaddr, UDP_SERVER_PORT);
     
    if (err == ERR_OK) {
      /* Set a receive callback for the upcb */
      udp_recv(upcb, udp_receive_callback, NULL);
       
      //sprintf((char*)data, "sending udp client message %d", (int*)message_count);     
      //sprintf((char*)data, "%d", (int*)DATAS);
      //strcpy(data,(char*)DATAS);
      for (int ctr=0;ctr<130;ctr++)
      {
        dataS[ctr] =(char)0;
      }
      for (int ctr=0;ctr<130;ctr++)
      {
        dataS[ctr]=DATAS[ctr];
      }
   
      /* allocate pbuf from pool*/
      strcpy((char*)data,dataS);
      p = pbuf_alloc(PBUF_TRANSPORT,strlen((char*)data), PBUF_POOL);
       
      strlen((char*)data);
       
      if (p != NULL) {
        /* copy data to pbuf */
        pbuf_take(p, (char*)data, strlen((char*)data));
           
        /* send udp data */
        udp_send(upcb, p);
         
        /* free pbuf */
        pbuf_free(p);
      } else {
        #ifdef SERIAL_DEBUG
         printf("\n\r can not allocate pbuf ");
        #endif
      }
    } else {
      #ifdef SERIAL_DEBUG
       printf("\n\r can not connect udp pcb");
      #endif
    }
  } else {
    #ifdef SERIAL_DEBUG
     printf("\n\r can not create udp pcb");
    #endif
  }
}


err_t
pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
{
  struct pbuf *p;
  u16_t buf_copy_len;
  u16_t total_copy_len = len;
  u16_t copied_total = 0;
 
  LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return 0;);
  LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return 0;);
 
  if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
    return ERR_ARG;
  }
 
  /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
  for(p = buf; total_copy_len != 0; p = p->next) {
    LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
    buf_copy_len = total_copy_len;
    if (buf_copy_len > p->len) {
      /* this pbuf cannot hold all remaining data */
      buf_copy_len = p->len;
    }
    /* copy the necessary parts of the buffer */
    MEMCPY(p->payload, &((char*)dataptr)[copied_total], buf_copy_len);
    total_copy_len -= buf_copy_len;
    copied_total += buf_copy_len;
  }
  LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
  return ERR_OK;
}

Outcomes