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

mcsteve237
Posts: 3
Joined: Mon Jun 18, 2018 6:40 pm

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

Postby mcsteve237 » Mon Jun 18, 2018 7:11 pm

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[i] = data[i];
}
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;
}

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

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

Postby kolban » Tue Jun 19, 2018 1:57 am

Can you elaborate on what you mean by "flush"?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Available for ESP32 consulting.

mcsteve237
Posts: 3
Joined: Mon Jun 18, 2018 6:40 pm

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

Postby mcsteve237 » Tue Jun 19, 2018 4:44 am

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

WiFive
Posts: 1914
Joined: Tue Dec 01, 2015 7:35 am

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

Postby WiFive » Tue Jun 19, 2018 1:55 pm

message variable is not cleared and you are returning a pointer to a function internal static variable

User avatar
martinayotte
Posts: 111
Joined: Fri Nov 13, 2015 4:27 pm

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

Postby martinayotte » Tue Jun 19, 2018 2:44 pm

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]".

User avatar
fly135
Posts: 363
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

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

Postby fly135 » Tue Jun 19, 2018 3:41 pm

Along the lines of what Martin said...

replace this....

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

with this....

strcpy(message, data);

mcsteve237
Posts: 3
Joined: Mon Jun 18, 2018 6:40 pm

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

Postby mcsteve237 » Tue Jun 19, 2018 5:18 pm

[quote="fly135"]Along the lines of what Martin said...

replace this....

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

with this....

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

User avatar
fly135
Posts: 363
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

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

Postby fly135 » Wed Jun 20, 2018 1:01 am

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

Who is online

Users browsing this forum: No registered users and 4 guests