Page 1 of 1

How can I flush UART of ESP32 each time I receive a new message?

Posted: Mon Jun 18, 2018 7:11 pm
by mcsteve237
char *readData(){
const int RX_BUF_SIZE = 1024;
static const char *RX_TASK_TAG = "RX_TASK";
esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE+1);

static char message[RX_BUF_SIZE];
const int rxBytes = uart_read_bytes(UART_NUM_0, data, RX_BUF_SIZE, 10 / portTICK_RATE_MS);
if (rxBytes > 0) {
data[rxBytes] = 0;
for(size_t i = 0; i < rxBytes; i++){
message = data;
}
ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data);
ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO);
}
free(data);
return message;
}

Re: How can I flush UART of ESP32 each time I receive a new message?

Posted: Tue Jun 19, 2018 1:57 am
by kolban
Can you elaborate on what you mean by "flush"?

Re: How can I flush UART of ESP32 each time I receive a new message?

Posted: Tue Jun 19, 2018 4:44 am
by mcsteve237
Yes. My wish is to empty the rx buffer so that the next incoming data will be read properly. The problem I have now is that when I read from rx buffer I can see part of the previous data that was read. For Example

Hallo World // "Hallo World" is my first data in the rx buffer
byelo World //"bye" is my second data that was read, but as you can see it contains part of the first data.

My wish is to clear the buffer after recceiving "Hallo World" by so doing the next incoming data will be free from errors. Thanks for helping

Re: How can I flush UART of ESP32 each time I receive a new message?

Posted: Tue Jun 19, 2018 1:55 pm
by WiFive
message variable is not cleared and you are returning a pointer to a function internal static variable

Re: How can I flush UART of ESP32 each time I receive a new message?

Posted: Tue Jun 19, 2018 2:44 pm
by martinayotte
Also, you are doing a "data[rxBytes] = 0;" to end the string, which is good, but you never copying this null character into "message[rxBytes]".

Re: How can I flush UART of ESP32 each time I receive a new message?

Posted: Tue Jun 19, 2018 3:41 pm
by fly135
Along the lines of what Martin said...

replace this....

for(size_t i = 0; i < rxBytes; i++){
message = data;

with this....

strcpy(message, data);

Re: How can I flush UART of ESP32 each time I receive a new message?

Posted: Tue Jun 19, 2018 5:18 pm
by mcsteve237
[quote="fly135"]Along the lines of what Martin said...

replace this....

for(size_t i = 0; i < rxBytes; i++){
message = data;

with this....

strcpy(message, data);[Its not working since data is unsigned]

Re: How can I flush UART of ESP32 each time I receive a new message?

Posted: Wed Jun 20, 2018 1:01 am
by fly135
working or compiling?

strcpy(message, (char*)data);

Now that I think about it I don't know what strcpy would do with char above 0x7F. I thought it only looked for a terminating zero. But the loop is fine except your concerns are not warranted because you don't need a terminating zero on non string data and you shouldn't complain that data in the uint8_t buffer that you left there is still there. Binary arrays should have a length parameter associated with them. If you know the length of data in the buffer then you don't care about the last xfer still being in there.

IOW your program is fine but you are printing binary data as ascii, which you should expect to not work right. However in the context of the example the data is ascii and strcpy will work.

John A