This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123G UART buffer problem

Hi,

I,m doing project on ESP8266 using TM4C123G. I,m sending AT command (AT+CWLAP) for the ESP so i got the output on UART (Putty). But that output i like to store into buffer and then process it further. But the output value is not store in buffer.

This is a code which i store value in Charbuffer[i]  that was get into UART5 which i get all the values.  But when i see to the Charbuffer only OK comes.

so what is the problem?


Charbuffer[i]=UARTCharGet(UART1_BASE);

UARTCharPut(UART5_BASE,Charbuffer[i]);

Regards,

Vikas

  • Hello Vikas

    A two line code does not show us the entire picture. How deep is the variable array, how the string is being stored in it and how buffer management is being done?
  • This is my whole code which Initialization done in .c file

    #include <stdint.h>
    #include <stdio.h>
    #include <stdbool.h>
    #include <string.h>
    #include <stdlib.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_ints.h"
    #include "driverlib/gpio.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/uart.h"
    #include "inc/hw_gpio.h"
    #include "inc/hw_sysctl.h"
    #include "utils/ustdlib.h"

    #include "esp8266.h"
    bool boolresponse = false;



    void processcommand (void);
    volatile bool boolNewCommand;
    void UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count);
    void UARTIntHandler(void);
    char Charbuffer[500]={0};
    char CharString_Rest[100]={0};
    char *token1,*token2,*token3,*token4;
    char cmd;

    void main(void)
    {

    //Set the System clock control
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    ESP8266Init(); // configuration Initialization

    UARTSend("AT+CWLAP\r\n",10);
    SysCtlDelay(2000000);

    while(1)
    {
    //processcommand();
    }
    }

    void UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count)
    {
    //
    // Loop while there are more characters to send.
    //
    while (ui32Count--)
    {
    //
    // Write the next character to the UART.

    UARTCharPut(UART1_BASE, *pui8Buffer++);

    }
    }
    void UARTIntHandler(void)
    {
    unsigned long i;
    uint32_t ui32Status;
    boolresponse = true;

    //
    // Get the interrrupt status.
    //
    ui32Status = UARTIntStatus(UART1_BASE, true);

    //
    // Clear the asserted interrupts.
    //
    UARTIntClear(UART1_BASE, ui32Status);

    //
    // Loop while there are characters in the receive FIFO.
    //
    i=0;
    while (UARTCharsAvail(UART1_BASE))
    {
    // Read the next character from the UART and write it back to the UART.
    Charbuffer[i]=UARTCharGet(UART1_BASE);

    UARTCharPut(UART5_BASE,Charbuffer[i]); //UART 5 is being used only to display onn the Hyperterminal.

    if((Charbuffer[i]=='\n') || (Charbuffer[i]=='\r'))
    {
    Charbuffer[i]='\0';
    boolNewCommand=true;
    }
    i++;
    }
    }

    void processcommand (void)
    {

    if(boolNewCommand)
    {
    cmd = Charbuffer[0];
    strcpy(CharString_Rest,(const char*) Charbuffer+1);
    token1 = strtok(CharString_Rest,",");
    token2 = strtok(NULL,",");
    token3 = strtok(NULL,",");
    token4= strtok(NULL,",");
    }
    }

    In the code when i put the break point on while loop only last value store in buffer.
  • For instance, because you never increment the array index (i) in your read-out loop (in the interrupt handler).
  • Already I increment array index (i) after Uartput command see the code.
  • I've seen this - but since you did not use the code formatting tool (the "[</>]" icon), your code is difficult to read - no indentations visible.

  • BTW, the array index is local to the interrupt handler.
    What happens if there are arriving single characters, or just parts of your string ?
  • Hello Vikas,

    You i variable is declared inside the interrupt handler. Everytime a new interrupt occurs, it is initialized back to 0 - it does not matter if you incremented it on the previous run of that function.

    Read and understand about static variables inside a function, that will solve your issue. However, it is not the best solution to keep two variables that relate to the same subject - in this case, the buffer and the buffer index - in two different scopes.

  • You i variable is declared inside the interrupt handler. Everytime a new interrupt occurs, it is initialized back to 0 - it does not matter if you incremented it on the previous run of that function.

    Now you spoiled that precious task for the O.P. ... ;-)

  • I keep soaring above the line between giving OP's a fish or teaching them that, to begin with, they need to dig for worms as bait...
    Still, I tried to just suggest "read about static variables"... So that this does not become c_for_dummies forum!
  • IMHO finding a bug themselves use to have a larger impact on memory (mind) ...

    ... So that this does not become c_for_dummies forum!

    Isn't it already ? ;-)

  • Thank you for response i got it my mistake and i am new for all this so i take care next time.
  • The intermediate is to use Socratic techniques. Ask questions to guide them to the answer/

    Robert
  • True....
    By the way, Amit is an expert on that...
    Sometimes I read his posts and feel like this is a psychologist session, not an engineer forum... ;)
  • Hello Bruno

    I do not believe that with a cause there can be an action. It is easy to ask for a solution, ask others to help but developing an approach to logical debug is not that easy (for some engineers I have worked with). May be we can take this topic of the forum....
  • ... not an engineer forum... ;)

    That is an interesting topic.

    The word "engineer" does not come from "engine" (machine) as one might believe at first. The french/german "ingenieur" is closer to the root - the latin "gignere" or "ingenium". Or the latin "genius", for "skill" or "talent".

    Quite often, that "genius spirit" is totally absent in posts - not even a willingness to learn.

    BTW, not really related to this thread/O.P., just want to mention it ...

  • Sure is interesting!

    I also come from a more Latin mother tongue (as compared to English), and believe that from the languages I know, English is the one that most corrupted the term "engine". It is almost like the Brits got so amazed by the brilliance of the reciprocating motors in the past, that they accepted that such machine was entitled the term Engine and nothing after that would be. Not many languages use two different words for combustion motors as opposed to the "other" kinds, i.e. hydraulic and electric.

    Amit, that's true about what is easier and what is more enlightening... ask or research? Some posts I read give me the chills, and yet you come out of them with elegance when someone demands "a prompt explanation to why is this stupid MCU not working!?".

    Then again, ain't REALLY ingenious those carreers that make a wealthy living without ever creating anything in life? Making a 20% cut out of every legal demand on the planet??? More than us, might it be...
  • Then again, ain't REALLY ingenious those carreers that make a wealthy living without ever creating anything in life? Making a 20% cut out of every legal demand on the planet??? More than us, might it be...

    IMHO this has to do with the difference between "money" and "legal tender" - which is not the same. The former is mainly driven out of existence, while the latter is created by unproductive entities out of thin air. But there is nothing to see here, even for engineers, so just go on ...

    Again digressed too far ...