-
-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add flushReceiveBuffer() to HardwareSerial #89
Comments
Processing has this function, named "clear". https://processing.org/reference/libraries/serial/Serial_clear_.html Dunno if following Processing's conventions is still a thing? |
Good point, question If we follow the P convention I propose to add the buffer name so |
(1) makes sense, however nowhere in the current HardwareSerial (AVR) I see a guard like you mention. (2) would this be solved by assigning the tail iso the head? a new incoming char would move head 1 position, so at most one char is in the buffer now
I will check the discussion (later) |
Rob - my earlier message was about flushing the transmit buffer, but later realised that function is already there and you are talking about flushing the receive buffer. So I deleted the message - sorry about the confusion. A guard might be needed here to prevent the rx_complete_irq function moving the head pointer after read of the tail pointer but before write of the head pointer in your function. I'm not sure whether this would actually cause a problem, but it does need consideration. If so, something like You are right that there are no such guards presently, but there are in the upcoming version in the separate AVR core repository (#6855) to fix a couple of race conditions in the current code. |
Thanks John, (read the discussion) if we do not use ATOMIC and use _rx_buffer_head = _rx_buffer_tail; Three cases - assuming single byte indices
in case of multi byte indices ATOMIC is needed (no doubt about that) ATOMIC could be added conditionally depending on So the state of the proposal is now:
|
Rob - your analysis suggests to me that the race condition is harmless in the default case of 256 byte buffers, so protection isn't needed here. To support larger buffers the function could be void HardwareSerial::clearReceiveBuffer() The TX_BUFFER_ATOMIC macro (defined in the new version of HardwareSerial.cpp) adds a guard Note that there are other unfixed race conditions that can still cause problems for large buffers, but it could be argued that new code at least shouldn't add new ones. |
Drat - I did it again, confusing transmit and receive buffers. But an RX_BUFFER_ATOMIC macro So now I'd suggest // macro to guard critical sections when needed for large RX buffer sizes void HardwareSerial::clearReceiveBuffer() |
There are other places in HWSerial where RX_BUFFER_ATOMIC should be used too. |
There are indeed. Those are addressed in PR arduino/Arduino#5 in the AVR core repository (which also defines the RX_BUFFER_ATOMIC macro suggested above). That PR hasn't received much interest - perhaps because there would still be race problems for the transmit case that it doesn't address and because large buffers is a corner case that is not fully supported (e.g. you have to edit HardwareSerial.h to implement them). |
All bugs are corner case :) |
Open for too long => not relevant anymore for me |
It recently became relevant to me though. Currently I'm emptying the buffer beforehand by reading until there's nothing available anymore, but that takes a considerable amount of time. |
@OParczyk et al? |
I, for one, would prefer it to be. |
In Serial communication sometimes one wants to flush all data in the receive buffer at once. E.g. because data has become invalid. - https://forum.arduino.cc/index.php?topic=530650 -
The code to do this very fast in HardwareSerial is straightforward,
The text was updated successfully, but these errors were encountered: