cancel
Showing results for 
Search instead for 
Did you mean: 

Working with pointers to send Lan Data

xtian
Associate II
Posted on April 16, 2013 at 08:39

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;
}

#pointers #c-basics
1 REPLY 1
Posted on April 16, 2013 at 10:37

''I'm a bit confused with pointers.''

 

 

Well, pointers are pretty fundamental to any  form of 'C' programming - not least embedded 'C' programming. So now would be a really good time to pause to brush-up before moving on to more advanced topics like networking!

Here's some 'C' learning & reference resources:

http://blog.antronics.co.uk/2010/12/12/things-you-shouldve-learned-in-c-class-0-introduction/

The debugger can be really helpful in understanding pointers: you can use it to see the value of the pointer (the address that it contains) and, from that, go to the pointed-to  location;

The debugger will also let you see the effects of incrementing & decrementing a pointer;

The debugger will also let you see that the pointer itself has an address...

You can use the debugger to see where you're accidentally using the value of the pointer itself instead of the value which it points to.

etc, etc,...

This is entirely general 'C' stuff - nothing specifically to do with the STM32 or embedded.