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?
- 45
- 3
-
AltSoftSerial has availableForWrite() – Juraj Dec 29 '20 at 16:33
-
What's your use case for Software Serial? – Gabriel Staples Dec 30 '20 at 06:34
1 Answers
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.
- 5,181
- 1
- 13
- 25
-
This means I can call Write whenever I want without a risk of blocking my main program? – firendlyQuestion Dec 29 '20 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. – firendlyQuestion Dec 29 '20 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? – firendlyQuestion Dec 29 '20 at 16:13
-
For Serial (HardwareSerial) on UNO (you have tagged), there's also no flow control between the ATMega328P chip and the serial transceiver (ATMega#U2 or FTDI, CH340, etc). So it will not block owing to flow control. You can fill the transmit ring buffer and have to wait for the USART transmit interrupt to drain that ring buffer. But it will not deadlock. – timemage Dec 29 '20 at 16:17
-