How to get a char from a buffer?

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
728
Hi,

How can I create a char getchar(void) function that reads a character from a buffer? The following code is how the buffer gets the data.

Code:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    uint8_t i;
    if (huart->Instance == USART3) //is uart3 active?
    {
        if (rx_index == 0)   //if data is no being received
        {
            for (i=0;i<30;i++)
            {
                rx_buffer[I]=0;    //clean the buffer
            }
        }

        else if(rx_index<30) //if there is space in the buffer
        {
            rx_buffer[rx_index++]= rx_data;       // store data in the buffer
        }

        else
        {
          //ignore data
        }

        HAL_UART_Receive_IT (&huart3, rx_data, 1); //activate reception
    }
}
So the buffer in question is rx_buffer

Edit: inserted code tags like this [code] your code [/code]
 

WBahn

Joined Mar 31, 2012
32,823
Thanks for formatting it. See how much easier it is to read?

I see you also learned about the [plain] tags. So few people do.

It appears that your buffer and your rx_index variable are globally declared, right?

If so, then you need another variable for your readout index. When you read, you check to see if your readout index is less than your readin index (rx_index) so that you don't read passed the end of the data in the buffer. You also need to reset it to zero when you clear the buffer.

At some point, you might consider looking at a circular buffer to simplify things.
 
Top