-1

I have a uint8_t rxBuffer[200] = {0}; array.

The buffer receives messages from UART USART1.Receive(rxBuffer, sizeof(rxBuffer));

I want to receive UART responses in this rxBuffer, parse them and then reuse this array to parse further responses. How do I clear this buffer and reuse it ?

I tried using memset(rxBuffer, 0, sizeof rxBuffer); but nothing prints on the debug console when i try to print out the buffers contents

What is that i am doing incorrect here ?

I will provide a sudo code to what i am trying to do:

uint8_t rxBuffer[200] = {0};
uint8_t at[] = "AT\r\n";
uint8_t atSetSTA[] = "AT+CWMODE=1\r\n";

DEMO_USART1.Send(at, sizeof(at) - 1);
delay(1000);
DEMO_USART1.Receive(rxBuffer, sizeof(rxBuffer));
print(rxBuffer);
memset(rxBuffer, 0, sizeof rxBuffer)


DEMO_USART1.Send(atSetSTA, sizeof(atSetSTA) - 1);
delay(1000);
DEMO_USART1.Receive(rxBuffer, sizeof(rxBuffer));
print(rxBuffer);
memset(rxBuffer, 0, sizeof rxBuffer)

When i use memset, Nothing prints to the console. When i dont use memset, in this case it will print the previous responses as well.

  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/226336/discussion-on-question-by-omkar-joglekar-reuse-a-uint8-t-buffer-to-store-uart-re). – Samuel Liew Dec 23 '20 at 02:30

1 Answers1

0

How are you printing to the console?

If you are calling printf(rxBuffer), the printf will print nothing, since the first value of the buffer is 0.

I assume this is because the compiler is interpreting rxBuffer as a char* (a.k.a. c string) which is null terminated. This means that the printf will print values starting at address rxBuffer until a value of 0 is reached.

You might want to print out the values of the buffer with something like

printf("Buffer = [");
for (int i = 0; i < sizeof(rxBuffer); i++) {
   printf("%d", rxBuffer[i]);
}
printf("]\n");

instead

John King
  • 3
  • 1