Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

HardwareSerial delivering stray data on serial.read() #6757

Answered by Miq1
Miq1 asked this question in Q&A
Discussion options

We have a couple of users of our eModbus library that are experiencing additional data being delivered to serial.read(). A logic analyzer or scope will see the correct data on the RX and TX wires, while serial.read() will bring more than are seen on the outside.

The data looks like remainders from previous transmissions and is one to about 250 bytes long. The read loop is basically

int bufptr = 0;
uint8_t buffer[512];
if (serial.available()) {
 int b = serial.read();
 while (b >= 0) {
 buffer[bufptr++] = b;
 if (bufptr >=511) break;
 b = serial.read();
 }
}

(shortened to the essential)

To get the data as soon it arrives, we do a

UART1.conf1.rxfifo_full_thrhd = 1;

(or UART0, UART2) upon initialization, if that matters.

Is something known to be causing such effects?

You must be logged in to vote

Looks like it was something else, the issues reportedly are gone for now.

Thanks for the insights anyway!

Replies: 3 comments 4 replies

Comment options

Interesting. Perhaps you should create an issue for this? What version of arduino-esp32 are you targeting?

You must be logged in to vote
1 reply
Comment options

2.0.2 is the version AFAIK. I am reluctant opening an issue as I am not able to reproduce it myself yet and have only the users' reports (and logs of course) so far. But it were reports of unrelated users, so I tend to believe the effect does exist in fact.
The logs are showing the data must have come from the serial.read(), while the logic analyzer dumps taken at the same time on the RX pin do not have it.

Comment options

There was an issue in IDF (this one) resulting in wrong buffer sizes being returned by uart_get_buffered_data_len() (hence: available()) in multithreaded applications. It was fixed mid-October 2021. Does the arduino-esp32 core have that fix?

You must be logged in to vote
3 replies
Comment options

v2.0.3 is based on IDF 4.4.1. https://github.com/espressif/arduino-esp32/releases/tag/2.0.3
Looking at the local copy of IDF on my computer: C:\Users\David\.platformio\packages\framework-espidf\components\driver\uart.c.
uart.c:258:

esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t *stop_bit)
{
 ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
 UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
 uart_hal_get_stop_bits(&(uart_context[uart_num].hal), stop_bit);
 UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
 return ESP_OK;
}

Yes, it contains the critical wrapper fix. Compare this to commit espressif/esp-idf@6c363a4

esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t* stop_bit)
{
 UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL);
 UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
 uart_hal_get_stop_bits(&(uart_context[uart_num].hal), stop_bit);
 UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
 return ESP_OK;
}
Comment options

You can download the archive of 2.0.0 and see if that version has the fix, if you wish: https://github.com/espressif/arduino-esp32/releases/tag/2.0.0

Comment options

Thanks, good to see it has been incorporated. Unfortunately that would have been handy to explain the things our users are experiencing. Well, continue hunting it is again...

Comment options

Looks like it was something else, the issues reportedly are gone for now.

Thanks for the insights anyway!

You must be logged in to vote
0 replies
Answer selected by Miq1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /