The HardwareSerial requires to run an interrupt to store received bytes in the buffer. Look at this file from the Arduino Core. If I am now constantly receiving and writing on the Software Serial, the data from the HardwareSerial cannot be buffered, since external interrupts are higher priority.
In my application, I need to constantly listen to the SoftwareSerial and respond immediately to incoming data (actually inside the receive interrupt of the SoftwareSerial, so its modified), so the SoftwareSerial has higher priority. But when I do this, I lose data on the HardwareSerial.
Is there any way to circumvent this limitation, without disabling any interrupts or getting a chip with more UART ports?
EDIT: the actual interrupt vector is here
EDIT2: This is question is not answered by the answers in my previous question. It is clearly a fact that HardwareSerial needs its receive interrupt to store data in a buffer. My question now is how I can still use the SoftwareSerial while it is constantly receiving.
1 Answer 1
If you are sending something in SoftwareSerial ISR handler that means ISRs are disabled and you are blocking other ISRs being called (if needed).
If you're using some crazy slow baudrate like 9600 then it means over 1 millisecond per character sent and during that time you might be missing other interrupts calls (millis, HW serials). If there is only one character, it's not problem, it'll be handled after interrupts are enabled again, but if there will be second or more character, previous are lost, as the've never been readed out from the UDR register (that should set flag for buffer overrun or how it's called).
If your Hw serial has some nice speed like 115200, you can lose up to 12 characters during sending single characted on software serial with speed 9600
The cli() in software serial library but as you wrote it's called from ISR handler and the ISRs are disabled anyway already during whole ISR handler
-
"If your Hw serial has some nice speed like 115200, you can loose up to 12 characters during sending single characted on software serial with speed 9600" This is exactly the situation I'm in, which is unfortunately required. Thanks for your Info. I'm currently looking into AltSoftSerial, which doesn't block as much.RenX– RenX07/11/2023 20:06:13Commented Jul 11, 2023 at 20:06
-
Go for something like Nano Every (note that Every), that has several USARTs and one connected to the USB is different from second connected to pins 0 and 1KIIV– KIIV07/11/2023 20:11:20Commented Jul 11, 2023 at 20:11
-
Another solution would probably be to implement some sort of hardware flowcontrol, since the device on the HW serial supports it. This would require a modification to the receive isr of the HW serial AND a new PCB. We'll seeRenX– RenX07/11/2023 20:12:48Commented Jul 11, 2023 at 20:12
-
@LiessJemai Another solution is to swap the interfaces. Use the software serial with the higher speed (it can handle up to 115200 bits per second) and the hardware serial with the lower speed. Or, if you can choose the speeds, adjust them.the busybee– the busybee07/12/2023 06:26:56Commented Jul 12, 2023 at 6:26
This is question is not answered by the answers in my previous question
... look again ... that question does not have any answers