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;
}
How can I flush UART of ESP32 each time I receive a new message?
-
- 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?
Can you elaborate on what you mean by "flush"?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
-
- 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?
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
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?
message variable is not cleared and you are returning a pointer to a function internal static variable
- martinayotte
- Posts: 141
- Joined: Fri Nov 13, 2015 4:27 pm
Re: How can I flush UART of ESP32 each time I receive a new message?
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?
Along the lines of what Martin said...
replace this....
for(size_t i = 0; i < rxBytes; i++){
message = data;
with this....
strcpy(message, data);
replace this....
for(size_t i = 0; i < rxBytes; i++){
message = data;
with this....
strcpy(message, data);
-
- 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?
[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]
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?
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
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 114 guests