How to use SoftwareSerial.Write
without blocking if buffer is full? Serial.write
blocks if the buffer is full. So I need to check Serial.availableforwrite
before calling write to prevent blocking. But there is no SoftwareSerial.availableforwrite
. How can I use SoftwareSerial.Write
without blocking?
-
AltSoftSerial has availableForWrite()– Juraj ♦Commented Dec 29, 2020 at 16:33
-
What's your use case for Software Serial?– Gabriel StaplesCommented Dec 30, 2020 at 6:34
1 Answer
In short, you don't.
The SoftwareSerial implementation for AVR doesn't have an outbound buffer at all.
It just turns the interrupts off during each character outbound and it sends them all immediately.
This means a call to SoftwareSerial's write simply will block until all of the data you've tried to send has in fact been sent. If you can't afford to wait for your data to go out on the TX line now, then it's up to you to send it later when you can afford to wait.
SoftwareSerial has no flow control. When you write(), the data is sent out on the TX pin regardless of whether anything is present on the other side of the connection. And when present, the data is still transmitted regardless of whether other side considers itself "ready" to receive. There's simply no feedback from the receiver to tell the sender to stop. So SoftwareSerial itself will never block owing to these things.
-
This means I can call Write whenever I want without a risk of blocking my main program? Commented Dec 29, 2020 at 15:45
-
-
My problem is I can't make sure someone is listening or not. For the program it is not important if someone is listening, but ending in a deadlock is inacceptable. Commented Dec 29, 2020 at 16:01
-
-
1Is the last part (no flowcontrol) identical to the normal Serial.write? So I don't have to check Serial.availableforwrite for Serial.write too? Commented Dec 29, 2020 at 16:13