Skip to main content
Explorer
May 10, 2024
Solved

Wrong values getting assigned when same structure assignment is done.

  • May 10, 2024
  • 4 replies
  • 2869 views

I am developing an TCP/IP Server Client application on my NUCLEO-F207ZG  development board.I am implementing a function to send ADC values on Ethernet.

void Client_Data_Send(void)
{
char temp_buff[TEMP_BUFF_SIZE];
volatile uint32_t buff_len;
volatile uint16_t Temperature = 0;
struct pbuf *p = NULL;

Temperature = TEMP;

buff_len = sprintf(temp_buff,"DHT11 Sensor value from TCP Client : %u \n\r",Temperature);

/*allocate pbuf from ram*/
p = (pbuf_alloc(PBUF_TRANSPORT,buff_len,PBUF_POOL));

(tcp_client->p)= (p);

if(tcp_client->p != NULL)
{
/*copy the data to send into the allocated pbuf*/
pbuf_take( tcp_client->p ,(char *)temp_buff,buff_len);

/*send data*/
tcp_client_send(tx_pcb, tcp_client);

/*free mem*/
pbuf_free( tcp_client->p);
}

}

Above is the function I implemented.

At line (tcp_client->p)= (p); when I debuged I came to know that values are not getting assinged.

Below are the screen shot.

pradeepwagre_0-1715354230350.png

 

pradeepwagre_1-1715354230596.png

 

since wrong values for all members getting assigned I am facing Hard fault issue. Is it structure alignment issue? I am using LwIP library for Tcp/Ip application.

    This topic has been closed for replies.
    Best answer by pradeepwagre

    Thank you for replying,I added this and then the similar structure assignment issue got solved.

    pradeepwagre_0-1715782240650.png

     

    4 replies

    Super User
    May 10, 2024

    pbuf_alloc() can return NULL when there are no buffers available.  You don't check for that.  Try increasing the number of pbufs.

    Explorer
    May 10, 2024

    can you elaborate "increase number of pbufs"?Sorry I am a bit new to LwIP library.

    Super User
    May 10, 2024

    I think my initial post was not on the right track.  The code does indeed check for NULL in the pbuf pointer.  But to answer your follow-up question, there are a couple of #define values related to this.  PBUF_POOL_SIZE is probably the one you want IF you wanted to change that.  But I don't think that is your issue.

    The first image you posted is too low-res to read.  It looks like it is showing the contents of "p" and might be OK if I read the fuzzy pixels right.  The second shows "tcp_client", I presume BEFORE it gets "p" assigned to it.  That looks odd, specially the pointers.  Where is "tcp_client" declared/defined?  It looks to be a global variable in the code you posted above.

    Standard debugging applies here.  Does this fail on the 1st time through?  20th time?  Only after hours of running?  What do your other functions do with "tcp_client"?  And as a good programming practice, after you call pbuf_free() set tcp_client->p = NULL to help with future debugging and make it obvious if you try to use tcp_client->p again without allocating a new buffer.

    Graduate II
    May 10, 2024

    Not sure which image you're complaining about. The resolution of the Yellow screen shot makes it unreadable.

    What does the black one represent vs the yellow one. Problem is poorly expressed.

    >>Sorry I am a bit new to LwIP library.

    Perhaps read the documentation and review a broader group of examples.

    Is there a #define for buffer counts? Does the buffer need to be persistent?

    Explorer
    May 10, 2024

    Appologise for given lack of information.

    Buffer count is 100.

    After executing the below statement

    p = (pbuf_alloc(PBUF_TRANSPORT,buff_len,PBUF_POOL));

    Yellow image is shown by debugger which is expected value,basically it's showing string length which is to be sent on ethernet.

    pradeepwagre_0-1715360942174.png

     

    But after that

    (tcp_client->p)= (p); Is making problem.

    The black screen image is showing garbage values even after executing it,I am complaining below black image.

     

    pradeepwagre_1-1715361234307.png

    typedef struct
    {
    uint8_t retries;
    uint8_t state;
    struct pbuf *p;
    struct tcp_pcb *pcb;

    }Tcp_Client_Type;

    Above is my structure defined.

     

    Explorer
    May 11, 2024

    I did it in above comments.

    pradeepwagreAuthorAnswer
    Explorer
    May 15, 2024

    Thank you for replying,I added this and then the similar structure assignment issue got solved.

    pradeepwagre_0-1715782240650.png